瀏覽代碼

core: set file size from progress data when null (#2778)

* core: set file size from progress data when null

* core: improve size condition

Co-authored-by: Renée Kooi <renee@kooi.me>

* Move remote file size calculation to uppload success

* Fix file size in test now that we set the size in upload success

* Send bytesUploaded in upload success

* Prioritize bytesUploaded coming from companion

* Format

* Update index.js

Co-authored-by: Renée Kooi <renee@kooi.me>
Co-authored-by: Artur Paikin <artur@arturpaikin.com>
Enrique Mejia 3 年之前
父節點
當前提交
d9431fc035

+ 1 - 0
packages/@uppy/aws-s3/src/MiniXHRUpload.js

@@ -331,6 +331,7 @@ module.exports = class MiniXHRUpload {
             status: data.response.status,
             body,
             uploadURL,
+            bytesUploaded: data.bytesUploaded,
           }
 
           this.uppy.emit('upload-success', file, uploadResp)

+ 1 - 1
packages/@uppy/companion/src/server/Uploader.js

@@ -543,7 +543,7 @@ class Uploader {
       logger.error(errMsg, 'upload.multipart.mismatch.error')
       this.emitError(new Error(errMsg))
     } else {
-      this.emitSuccess(null, { response: respObj })
+      this.emitSuccess(null, { response: respObj, bytesUploaded })
     }
 
     this.cleanUp()

+ 8 - 0
packages/@uppy/core/src/index.js

@@ -1170,6 +1170,14 @@ class Uppy {
         isPaused: false,
       })
 
+      // Remote providers sometimes don't tell us the file size,
+      // but we can know how many bytes we uploaded once the upload is complete.
+      if (file.size == null) {
+        this.setFileState(file.id, {
+          size: uploadResp.bytesUploaded || currentProgress.bytesTotal,
+        })
+      }
+
       this.calculateTotalProgress()
     })
 

+ 1 - 1
packages/@uppy/core/src/index.test.js

@@ -1362,7 +1362,7 @@ describe('src/Core', () => {
       // wait for success event
       await finishPromise
 
-      expect(core.getFiles()[0].size).toBeNull()
+      expect(core.getFiles()[0].size).toBe(3456)
       expect(core.getFiles()[0].progress).toMatchObject({
         bytesUploaded: 3456,
         bytesTotal: 3456,