@@ -247,21 +247,21 @@ func uploadPayload(service *update.Service, file string) error {
247
247
}
248
248
defer f .Close ()
249
249
250
- body := & bytes. Buffer {}
251
- writer := multipart . NewWriter ( body )
252
-
253
- fname := filepath . Base ( fpath )
254
- part , err := writer . CreateFormFile ( "file" , fname )
255
- if err != nil {
256
- return err
257
- }
258
-
259
- _ , err = io . Copy ( part , f )
260
- if err = writer . Close (); err != nil {
261
- return err
262
- }
250
+ pipeOut , pipeIn := io . Pipe ()
251
+
252
+ writer := multipart . NewWriter ( pipeIn )
253
+ errChan := make ( chan error , 1 )
254
+ go func () {
255
+ defer pipeIn . Close ()
256
+ part , _ := writer . CreateFormFile ( "file" , filepath . Base ( fpath ))
257
+ if _ , err := io . Copy ( part , f ); err != nil {
258
+ errChan <- err
259
+ return
260
+ }
261
+ errChan <- writer . Close ()
262
+ }()
263
263
264
- req , err := http .NewRequest ("POST" , globalFlags .Server + "/package-upload" , body )
264
+ req , err := http .NewRequest ("POST" , globalFlags .Server + "/package-upload" , pipeOut )
265
265
if err != nil {
266
266
return err
267
267
}
@@ -289,7 +289,7 @@ func uploadPayload(service *update.Service, file string) error {
289
289
return fmt .Errorf ("error uploading file %s" , string (respBody ))
290
290
}
291
291
292
- return nil
292
+ return <- errChan
293
293
}
294
294
295
295
func packageUploadPayload (args []string , service * update.Service , out * tabwriter.Writer ) int {
@@ -322,35 +322,19 @@ func packageUploadPayloadBulk(args []string, service *update.Service, out *tabwr
322
322
return ERROR_USAGE
323
323
}
324
324
325
- var wg sync.WaitGroup
326
- var total , errorCount , totalSize int64
327
- var bar * pb.ProgressBar
328
- ready := make (chan struct {})
325
+ var total , errorCount int
329
326
for _ , file := range files {
330
327
if file .Mode ().IsRegular () {
331
328
total ++
332
- totalSize += file .Size ()
333
- wg .Add (1 )
334
- go func (file string , size int64 ) {
335
- // block until ready (wati for progress bar to initialize)
336
- <- ready
337
- err := uploadPayload (service , file )
338
- if err != nil {
339
- errorCount ++
340
- fmt .Print (err )
341
- }
342
- bar .Add (int (size ))
343
- wg .Done ()
344
- }(path .Join (absDir , file .Name ()), file .Size ())
329
+ err := uploadPayload (service , path .Join (absDir , file .Name ()))
330
+ if err != nil {
331
+ errorCount ++
332
+ fmt .Print (err )
333
+ }
334
+ fmt .Printf ("uploaded %s\n " , file .Name ())
345
335
}
346
336
}
347
337
348
- bar = pb .New64 (totalSize ).SetUnits (pb .U_BYTES )
349
- bar .Start ()
350
- close (ready )
351
- wg .Wait ()
352
- bar .Finish ()
353
-
354
338
log .Printf ("Package payloads uploaded. Total=%d Errors=%d" , total , errorCount )
355
339
return OK
356
340
}
0 commit comments