forked from asjoyner/shade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
throw.go
110 lines (92 loc) · 2.38 KB
/
throw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// throw stores a file in the cloud, encrypted.
package main
import (
"crypto/sha256"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"path"
"github.com/asjoyner/shade"
"github.com/asjoyner/shade/config"
"github.com/asjoyner/shade/drive"
_ "github.com/asjoyner/shade/drive/amazon"
_ "github.com/asjoyner/shade/drive/cache"
_ "github.com/asjoyner/shade/drive/encrypt"
_ "github.com/asjoyner/shade/drive/google"
_ "github.com/asjoyner/shade/drive/local"
_ "github.com/asjoyner/shade/drive/memory"
)
var (
defaultConfig = path.Join(shade.ConfigDir(), "config.json")
configPath = flag.String("config", defaultConfig, "shade config file")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\nusage: %s [flags] <filename> <destination filename>\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 2 {
flag.Usage()
os.Exit(2)
}
// read in the config
config, err := config.Read(*configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "could not initialize clients: %s\n", err)
os.Exit(1)
}
// initialize client
client, err := drive.NewClient(config)
if err != nil {
log.Fatalf("could not initialize client: %s\n", err)
}
filename := flag.Arg(0)
manifest := shade.NewFile(flag.Arg(1))
fh, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(3)
}
chunk := shade.NewChunk()
chunkbytes := make([]byte, manifest.Chunksize)
for {
// Read a chunk
len, err := fh.Read(chunkbytes)
if err == io.EOF {
break
} else if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(3)
}
manifest.Filesize += int64(len)
// truncate the chunkbytes array on the last read
if len < manifest.Chunksize {
chunkbytes = chunkbytes[:len]
manifest.LastChunksize = len
}
a := sha256.Sum256(chunkbytes)
chunk.Sha256 = a[:]
manifest.Chunks = append(manifest.Chunks, chunk)
// upload the chunk
if err := client.PutChunk(chunk.Sha256, chunkbytes, manifest); err != nil {
fmt.Fprintf(os.Stderr, "chunk upload failed: %s\n", err)
os.Exit(1)
}
chunk.Index++
}
jm, err := json.Marshal(manifest)
if err != nil {
fmt.Fprintf(os.Stderr, "could not marshal file manifest: %s\n", err)
os.Exit(1)
}
// upload the manifest
a := sha256.Sum256(jm)
if err := client.PutFile(a[:], jm); err != nil {
fmt.Fprintf(os.Stderr, "manifest upload failed: %s\n", err)
os.Exit(1)
}
}