-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
182 lines (143 loc) · 3.52 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
package main
import (
"encoding/base64"
"fmt"
"google.golang.org/api/drive/v3"
"log"
"math"
"math/rand"
"os"
"time"
)
const (
UploadTimeout = 1 * time.Second
BackoffMax = 1 * time.Hour // max time to wait between retries
RetryMax = 12 // max count of retries at BackoffMax
)
func waitAndUpload() {
files := make(chan FileID)
ticker := time.Tick(UploadTimeout)
defer close(files)
for i := 0; i < settings.UploadMaxWorkers; i++ {
go doUpload(i, files)
}
for {
<-ticker
fileID, err := claimFileForUpload()
if err == nil {
files <- fileID
}
}
}
func doUpload(id int, in <-chan FileID) {
for fileID := range in {
if fileID == "" {
continue
}
workerPrefix := fmt.Sprintf("[Worker %v] [[", id)
fileIDarr := make([]FileID, 1)
fileIDarr[0] = fileID
bytes, err := base64.RawURLEncoding.DecodeString(string(fileID))
if err != nil {
log.Printf("%v%s]] Error decoding fileID: %v", workerPrefix, fileID, err)
updateFileStatus(fileIDarr, StatusError)
continue
}
relPath := string(bytes)
path, err := getPath(relPath)
if err != nil {
log.Printf("%v%s]]Error finding fileID: %v", workerPrefix, fileID, err)
updateFileStatus(fileIDarr, StatusError)
continue
}
updateFileStatus(fileIDarr, StatusInProgress)
err = uploadFileOrFolder(path, settings.DriveRoot, workerPrefix)
if err != nil {
updateFileStatus(fileIDarr, StatusError)
continue
}
updateFileStatus(fileIDarr, StatusDone)
}
}
func uploadFileOrFolder(path *Path, parentId string, parentLog string) error {
if parentLog == "" {
parentLog = "[["
}
if path.IsDir != true {
return uploadFile(path, parentId, parentLog)
}
parentLog = fmt.Sprintf("%s/%s", parentLog, path.Name)
parentIds := make([]string, 1)
parentIds[0] = parentId
// make the folder
file := drive.File{
Name: path.Name,
Parents: parentIds,
MimeType: "application/vnd.google-apps.folder",
}
r, err := getDrive().Files.Create(&file).Fields("id").Do()
if err != nil {
return err
}
subPaths, err := listPaths(path.Path)
if err != nil {
return err
}
for _, subPath := range subPaths {
err = uploadFileOrFolder(subPath, r.Id, parentLog)
if err != nil {
return err
}
}
return nil
}
func uploadFile(path *Path, parentId string, parentLog string) error {
b, err := os.Open(path.Path)
if err != nil {
return err
}
parentIds := make([]string, 1)
parentIds[0] = parentId
file := drive.File{
Name: path.Name,
Parents: parentIds,
}
log.Printf("%s/%s]] START", parentLog, path.Name)
var success = false
var breakOut = false
var tries = 0
var maxTries = 0
var delay = 0 * time.Second
for success == false && breakOut == false {
if delay > 0 {
log.Printf("%s/%s]] TOTAL TRIES: %v", parentLog, path.Name, tries)
log.Printf("%s/%s]] RETRY TIMEOUT: %s", parentLog, path.Name, delay)
}
time.Sleep(delay)
_, err = getDrive().Files.Create(&file).Media(b).Do()
if err != nil {
log.Printf("%s/%s]] ERROR: %v", parentLog, path.Name, err)
delay = calcBackoff(tries)
if delay == BackoffMax {
maxTries++
if maxTries > RetryMax {
breakOut = true
log.Printf("%s/%s]] MAX RETRIES REACHED", parentLog, path.Name)
}
}
tries++
} else {
success = true
}
}
log.Printf("%s/%s]] DONE", parentLog, path.Name)
return err
}
func calcBackoff(n int) time.Duration {
backoff := time.Duration(math.Pow(2, float64(n))) * time.Second
backoff += time.Duration(rand.Int31n(1000)) * time.Millisecond
if backoff > BackoffMax {
return BackoffMax
}
return backoff
}