-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.go
348 lines (286 loc) · 8.64 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package transfer
import (
"bytes"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/danlamanna/rivet/girder"
"github.com/danlamanna/rivet/util"
)
const maxChunkSize = 1024 * 1024 * 16
// build these synchronously or use a better data structure for determining when parents are created
func buildGirderDirs(ctx *girder.Context, baseDir string) {
// get directories to build in sorted order (to avoid extraneous girder POST requests)
dirsToBuild := make([]string, 0)
for k, v := range ctx.ResourceMap {
if v.Type == "directory" {
dirsToBuild = append(dirsToBuild, k)
}
}
sort.Slice(dirsToBuild, func(i, j int) bool { return dirsToBuild[i] < dirsToBuild[j] })
for _, v := range dirsToBuild {
girder.GetOrCreateFolderRecursive(ctx, v)
}
}
func _uploadBytes(ctx *girder.Context, upload girder.GirderID, fullPath string, fi os.FileInfo) {
file, err := os.Open(fullPath)
if err != nil {
ctx.Logger.Warnf("failed to access %s, skipping. err: %s", fullPath, err)
return
}
defer file.Close()
totalChunks := util.Max(0, fi.Size()/maxChunkSize) + 1
var offset int64
i := 1
for {
bufSize := util.Min(maxChunkSize, fi.Size()-offset)
buffer := make([]byte, bufSize)
_, err := file.Read(buffer)
if err != nil {
if err != io.EOF {
fmt.Println(err)
}
break
}
r := bytes.NewReader(buffer)
chunkOrFile := new(girder.GirderObject)
gerr := new(girder.GirderError)
if totalChunks > 1 {
ctx.Logger.Debugf("%s - uploading chunk %d/%d", fullPath, i, totalChunks)
}
_, err = girder.Post(ctx, fmt.Sprintf("file/chunk?uploadId=%s&offset=%d", upload, offset), r, chunkOrFile, gerr)
offset, err = file.Seek(0, os.SEEK_CUR)
if err != nil {
// handle error
}
i++
if offset >= fi.Size() {
break
}
}
}
func uploadFile(ctx *girder.Context, parentID girder.GirderID, fullPath string, name string) int {
files := girder.ItemFiles(ctx, parentID)
upload := new(girder.GirderObject)
gerr := new(girder.GirderError)
fi, err := os.Stat(fullPath)
if err != nil {
ctx.Logger.Warnf("couldn't stat %s, skipping. err: %s", fullPath, err)
return 0
}
if len(files) == 0 {
ctx.Logger.Debugf("detected new file %s\n", fullPath)
ctx.Logger.Infof("uploading: %s\n", fullPath)
// creating a new file
girder.Post(ctx, fmt.Sprintf("file?parentId=%s&name=%s&parentType=item&size=%d", parentID, url.QueryEscape(name), fi.Size()), nil, upload, nil)
_uploadBytes(ctx, upload.ID, fullPath, fi)
} else if len(files) == 1 {
// potentially updating the contents of an existing file, or no-oping
if files[0].Size != fi.Size() {
ctx.Logger.Debugf("file sizes differ for %s\n", fullPath)
ctx.Logger.Infof("uploading: %s\n", fullPath)
// change file contents
girder.Put(ctx, fmt.Sprintf("file/%s/contents?size=%d",
files[0].ID, fi.Size()), nil, upload, gerr)
_uploadBytes(ctx, upload.ID, fullPath, fi)
}
} else {
fmt.Println("item has > 1 file.. not doing anything")
}
return 1
}
func shouldSkip(ctx *girder.Context, info os.FileInfo) bool {
// TODO skip symlinks
return false
}
func collectResources(ctx *girder.Context, localResources ...string) chan *girder.Resource {
ch := make(chan *girder.Resource)
go func() {
for _, localResource := range localResources {
stat, err := os.Stat(localResource)
if err != nil {
ctx.Logger.Warnf("failed to stat %s, skipping", localResource)
continue
} else if shouldSkip(ctx, stat) {
continue
}
if !stat.IsDir() {
ch <- &girder.Resource{
Path: localResource,
Type: "file",
Size: stat.Size(),
}
} else {
err := filepath.Walk(localResource, func(filepath string, info os.FileInfo, err error) error {
if err != nil {
ctx.Logger.Warnf("failed to access %s, skipping", filepath)
return nil
} else if shouldSkip(ctx, info) {
return nil
} else if filepath == "" || filepath == "." {
return nil
}
var fileType string
if info.IsDir() {
fileType = "directory"
} else {
fileType = "file"
}
ch <- &girder.Resource{
Path: filepath,
Type: fileType,
Size: info.Size(),
}
return nil
})
if err != nil {
ctx.Logger.Warnf("failed to walk the %s directory", localResource)
}
}
}
close(ch)
}()
return ch
}
func buildResourceMap(ctx *girder.Context, baseDir string) (int, int) {
numDirs, numFiles := 0, 0
for resource := range collectResources(ctx, baseDir) {
ctx.ResourceMap[resource.Path] = resource
if resource.Type == "directory" {
numDirs++
} else if resource.Type == "file" {
numFiles++
}
}
return numDirs, numFiles
}
func Upload(ctx *girder.Context, source string, destination girder.GirderID) {
ctx.Logger.Debugf("scanning %s for syncable items", source)
absSource, _ := filepath.Abs(source)
os.Chdir(absSource)
source = "."
numDirs, numFiles := buildResourceMap(ctx, source)
ctx.Logger.Infof("found %d dirs, %d files to potentially sync", numDirs, numFiles)
destFolder := new(girder.GirderObject)
httpErr := new(girder.GirderError)
resp, err := girder.Get(ctx, fmt.Sprintf("folder/%s", ctx.Destination), destFolder, httpErr)
if err != nil {
ctx.Logger.Fatalf("failed to retrieve destination folder, err: %s", err)
} else if resp.StatusCode != 200 {
ctx.Logger.Fatalf("failed to retrieve destination folder, err: %s", httpErr.Message)
}
ctx.Logger.Info("building remote girder directories")
buildGirderDirs(ctx, source)
ctx.Logger.Info("building remote girder items")
numJobs := 0
var mutex sync.Mutex
itemsToUpload := make(chan *girder.PathAndResource, numFiles)
results := make(chan bool, numFiles)
// spawn 10 workers for building items
for w := 1; w <= 10; w++ {
go func() {
for pathAndResource := range itemsToUpload {
parent := ctx.ResourceMap.Parent(pathAndResource.Resource)
// default to the root sync dest, override if there's a parent
parentID := girder.GirderID(strings.TrimPrefix(string(destination), "girder://"))
if parent != nil {
parentID = parent.GirderID
}
if parent != nil && parent.SkipSync {
mutex.Lock()
ctx.ResourceMap[pathAndResource.Path].SkipSync = true
ctx.ResourceMap[pathAndResource.Path].SkipReason = parent.SkipReason
mutex.Unlock()
ctx.Logger.Warnf("skipping sync of %s because parent failed to be created", parent.Path)
results <- true
continue
}
itemID, err := girder.GetOrCreateItem(ctx, parentID, filepath.Base(pathAndResource.Path))
mutex.Lock()
if err != nil {
ctx.ResourceMap[pathAndResource.Path].SkipSync = true
ctx.ResourceMap[pathAndResource.Path].SkipReason = err.Error()
ctx.Logger.Error(err)
} else {
ctx.ResourceMap[pathAndResource.Path].GirderID = itemID
}
mutex.Unlock()
results <- true
}
}()
}
for filepath, resource := range ctx.ResourceMap {
if resource.Type == "file" {
f := new(girder.PathAndResource)
f.Path = filepath
f.Resource = resource
numJobs++
itemsToUpload <- f
}
}
close(itemsToUpload)
for a := 0; a < numJobs; a++ {
<-results
}
ctx.Logger.Info("syncing blobs")
numJobs = 0
filesToUpload := make(chan *girder.PathAndResource, numFiles)
results = make(chan bool, numFiles)
// spawn 10 workers for uploading files
for w := 1; w <= 10; w++ {
go func() {
for pathAndResource := range filesToUpload {
if pathAndResource != nil {
uploadFile(ctx, pathAndResource.Resource.GirderID, pathAndResource.Path, path.Base(pathAndResource.Path))
}
results <- true
}
}()
}
for filepath, resource := range ctx.ResourceMap {
if resource.Type == "file" && resource.GirderID != "" {
f := new(girder.PathAndResource)
f.Path = filepath
f.Resource = resource
numJobs++
filesToUpload <- f
} else if resource.Type == "file" && resource.GirderID == "" {
// it was printed as an error above
ctx.Logger.Infof("skipping sync of %s because parent item creation failed.", filepath)
numJobs++
filesToUpload <- nil
}
}
close(filesToUpload)
for a := 0; a < numJobs; a++ {
<-results
}
ctx.Logger.Info("")
ctx.Logger.Info("summary:")
var numSucceeded int
var numFailed int
failureSummary := make([]string, 0)
for k, v := range ctx.ResourceMap {
if v.SkipSync {
numFailed++
failureSummary = append(failureSummary, fmt.Sprintf("%s: %s", k, v.SkipReason))
} else {
numSucceeded++
}
}
sort.Slice(failureSummary, func(i, j int) bool { return failureSummary[i] < failureSummary[j] })
ctx.Logger.Infof("successfully synced %d files/folders", numSucceeded)
if numFailed > 0 {
ctx.Logger.Infof("failed to sync %d files/folders:", numFailed)
for _, failure := range failureSummary {
ctx.Logger.Info(failure)
}
}
return
}