-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
263 lines (214 loc) · 6.25 KB
/
upload.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package drive
import (
"fmt"
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
"io"
"mime"
"os"
"path/filepath"
"time"
)
type UploadArgs struct {
Out io.Writer
Progress io.Writer
Path string
Name string
Description string
Parents []string
Mime string
Recursive bool
Share bool
Delete bool
ChunkSize int64
Timeout time.Duration
}
func (self *Drive) Upload(args UploadArgs) error {
if args.ChunkSize > intMax()-1 {
return fmt.Errorf("Chunk size is to big, max chunk size for this computer is %d", intMax()-1)
}
// Ensure that none of the parents are sync dirs
for _, parent := range args.Parents {
isSyncDir, err := self.isSyncFile(parent)
if err != nil {
return err
}
if isSyncDir {
return fmt.Errorf("%s is a sync directory, use 'sync upload' instead", parent)
}
}
if args.Recursive {
return self.uploadRecursive(args)
}
info, err := os.Stat(args.Path)
if err != nil {
return fmt.Errorf("Failed stat file: %s", err)
}
if info.IsDir() {
return fmt.Errorf("'%s' is a directory, use --recursive to upload directories", info.Name())
}
f, rate, err := self.uploadFile(args)
if err != nil {
return err
}
fmt.Fprintf(args.Out, "Uploaded %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(f.Size, false))
if args.Share {
err = self.shareAnyoneReader(f.Id)
if err != nil {
return err
}
fmt.Fprintf(args.Out, "File is readable by anyone at %s\n", f.WebContentLink)
}
if args.Delete {
err = os.Remove(args.Path)
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
fmt.Fprintf(args.Out, "Removed %s\n", args.Path)
}
return nil
}
func (self *Drive) uploadRecursive(args UploadArgs) error {
info, err := os.Stat(args.Path)
if err != nil {
return fmt.Errorf("Failed stat file: %s", err)
}
if info.IsDir() {
args.Name = ""
return self.uploadDirectory(args)
} else if info.Mode().IsRegular() {
_, _, err := self.uploadFile(args)
return err
}
return nil
}
func (self *Drive) uploadDirectory(args UploadArgs) error {
srcFile, srcFileInfo, err := openFile(args.Path)
if err != nil {
return err
}
// Close file on function exit
defer srcFile.Close()
fmt.Fprintf(args.Out, "Creating directory %s\n", srcFileInfo.Name())
// Make directory on drive
f, err := self.mkdir(MkdirArgs{
Out: args.Out,
Name: srcFileInfo.Name(),
Parents: args.Parents,
Description: args.Description,
})
if err != nil {
return err
}
// Read files from directory
names, err := srcFile.Readdirnames(0)
if err != nil && err != io.EOF {
return fmt.Errorf("Failed reading directory: %s", err)
}
for _, name := range names {
// Copy args and set new path and parents
newArgs := args
newArgs.Path = filepath.Join(args.Path, name)
newArgs.Parents = []string{f.Id}
newArgs.Description = ""
// Upload
err = self.uploadRecursive(newArgs)
if err != nil {
return err
}
}
return nil
}
func (self *Drive) uploadFile(args UploadArgs) (*drive.File, int64, error) {
srcFile, srcFileInfo, err := openFile(args.Path)
if err != nil {
return nil, 0, err
}
// Close file on function exit
defer srcFile.Close()
// Instantiate empty drive file
dstFile := &drive.File{Description: args.Description}
// Use provided file name or use filename
if args.Name == "" {
dstFile.Name = filepath.Base(srcFileInfo.Name())
} else {
dstFile.Name = args.Name
}
// Set provided mime type or get type based on file extension
if args.Mime == "" {
dstFile.MimeType = mime.TypeByExtension(filepath.Ext(dstFile.Name))
} else {
dstFile.MimeType = args.Mime
}
// Set parent folders
dstFile.Parents = args.Parents
// Chunk size option
chunkSize := googleapi.ChunkSize(int(args.ChunkSize))
// Wrap file in progress reader
progressReader := getProgressReader(srcFile, args.Progress, srcFileInfo.Size())
// Wrap reader in timeout reader
reader, ctx := getTimeoutReaderContext(progressReader, args.Timeout)
fmt.Fprintf(args.Out, "Uploading %s\n", args.Path)
started := time.Now()
f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "md5Checksum", "webContentLink").Context(ctx).Media(reader, chunkSize).Do()
if err != nil {
if isTimeoutError(err) {
return nil, 0, fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
}
return nil, 0, fmt.Errorf("Failed to upload file: %s", err)
}
// Calculate average upload rate
rate := calcRate(f.Size, started, time.Now())
return f, rate, nil
}
type UploadStreamArgs struct {
Out io.Writer
In io.Reader
Name string
Description string
Parents []string
Mime string
Share bool
ChunkSize int64
Progress io.Writer
Timeout time.Duration
}
func (self *Drive) UploadStream(args UploadStreamArgs) error {
if args.ChunkSize > intMax()-1 {
return fmt.Errorf("Chunk size is to big, max chunk size for this computer is %d", intMax()-1)
}
// Instantiate empty drive file
dstFile := &drive.File{Name: args.Name, Description: args.Description}
// Set mime type if provided
if args.Mime != "" {
dstFile.MimeType = args.Mime
}
// Set parent folders
dstFile.Parents = args.Parents
// Chunk size option
chunkSize := googleapi.ChunkSize(int(args.ChunkSize))
// Wrap file in progress reader
progressReader := getProgressReader(args.In, args.Progress, 0)
// Wrap reader in timeout reader
reader, ctx := getTimeoutReaderContext(progressReader, args.Timeout)
fmt.Fprintf(args.Out, "Uploading %s\n", dstFile.Name)
started := time.Now()
f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "webContentLink").Context(ctx).Media(reader, chunkSize).Do()
if err != nil {
if isTimeoutError(err) {
return fmt.Errorf("Failed to upload file: timeout, no data was transferred for %v", args.Timeout)
}
return fmt.Errorf("Failed to upload file: %s", err)
}
// Calculate average upload rate
rate := calcRate(f.Size, started, time.Now())
fmt.Fprintf(args.Out, "Uploaded %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(f.Size, false))
if args.Share {
err = self.shareAnyoneReader(f.Id)
if err != nil {
return err
}
fmt.Fprintf(args.Out, "File is readable by anyone at %s\n", f.WebContentLink)
}
return nil
}