forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
80 lines (65 loc) · 1.97 KB
/
update.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
package drive
import (
"fmt"
"google.golang.org/api/drive/v3"
"google.golang.org/api/googleapi"
"io"
"mime"
"path/filepath"
"time"
)
type UpdateArgs struct {
Out io.Writer
Progress io.Writer
Id string
Path string
Name string
Description string
Parents []string
Mime string
Recursive bool
ChunkSize int64
Timeout time.Duration
}
func (self *Drive) Update(args UpdateArgs) error {
srcFile, srcFileInfo, err := openFile(args.Path)
if err != nil {
return fmt.Errorf("Failed to open file: %s", err)
}
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.Update(args.Id, dstFile).Fields("id", "name", "size").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, "Updated %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(f.Size, false))
return nil
}