Quellcode durchsuchen

Progress fixes for unsized files (#1610)

* core: consistently use round() for progress calculations

* core: fix the estimated size for unsized files
Renée Kooi vor 5 Jahren
Ursprung
Commit
8485fec03a

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

@@ -245,7 +245,7 @@ class Uploader {
    * @see emitProgress
    * @param {number=} bytesUploaded the bytes actually Uploaded so far
    */
-  emitIllusiveProgress (bytesUploaded) {
+  emitIllusiveProgress (bytesUploaded = 0) {
     if (this._paused) {
       return
     }
@@ -254,7 +254,6 @@ class Uploader {
     if (!this.streamsEnded) {
       bytesTotal = Math.max(bytesTotal, this.bytesWritten)
     }
-    bytesUploaded = bytesUploaded || 0
     // for a 10MB file, 10MB of download will account for 5MB upload progress
     // and 10MB of actual upload will account for the other 5MB upload progress.
     const illusiveBytesUploaded = (this.bytesWritten / 2) + (bytesUploaded / 2)

+ 3 - 3
packages/@uppy/core/src/index.js

@@ -674,7 +674,7 @@ class Uppy {
         percentage: canHavePercentage
           // TODO(goto-bus-stop) flooring this should probably be the choice of the UI?
           // we get more accurate calculations if we don't round this at all.
-          ? Math.floor(data.bytesUploaded / data.bytesTotal * 100)
+          ? Math.round(data.bytesUploaded / data.bytesTotal * 100)
           : 0
       })
     })
@@ -701,7 +701,7 @@ class Uppy {
     const unsizedFiles = inProgress.filter((file) => file.progress.bytesTotal == null)
 
     if (sizedFiles.length === 0) {
-      const progressMax = inProgress.length
+      const progressMax = inProgress.length * 100
       const currentProgress = unsizedFiles.reduce((acc, file) => {
         return acc + file.progress.percentage
       }, 0)
@@ -721,7 +721,7 @@ class Uppy {
       uploadedSize += file.progress.bytesUploaded
     })
     unsizedFiles.forEach((file) => {
-      uploadedSize += averageSize * (file.progress.percentage || 0)
+      uploadedSize += averageSize * (file.progress.percentage || 0) / 100
     })
 
     let totalProgress = totalSize === 0

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

@@ -987,7 +987,7 @@ describe('src/Core', () => {
         bytesTotal: 17175
       })
       expect(core.getFile(fileId).progress).toEqual({
-        percentage: 71,
+        percentage: 72,
         bytesUploaded: 12345,
         bytesTotal: 17175,
         uploadComplete: false,
@@ -1055,9 +1055,11 @@ describe('src/Core', () => {
       expect(core.getFiles()[0].progress).toMatchObject({
         bytesUploaded: 1234,
         bytesTotal: 3456,
-        percentage: 35
+        percentage: 36
       })
 
+      expect(core.getState().totalProgress).toBe(36)
+
       finishUpload()
       // wait for success event
       await finishPromise
@@ -1070,6 +1072,47 @@ describe('src/Core', () => {
       })
 
       await uploadPromise
+
+      core.close()
+    })
+
+    it('should estimate progress for unsized files', () => {
+      const core = new Core()
+
+      core.once('file-added', (file) => {
+        core.emit('upload-started', file)
+        core.emit('upload-progress', file, {
+          bytesTotal: 3456,
+          bytesUploaded: 1234
+        })
+      })
+      core.addFile({
+        source: 'instagram',
+        name: 'foo.jpg',
+        type: 'image/jpeg',
+        data: {}
+      })
+
+      core.once('file-added', (file) => {
+        core.emit('upload-started', file)
+        core.emit('upload-progress', file, {
+          bytesTotal: null,
+          bytesUploaded: null
+        })
+      })
+      core.addFile({
+        source: 'instagram',
+        name: 'bar.jpg',
+        type: 'image/jpeg',
+        data: {}
+      })
+
+      core._calculateTotalProgress()
+
+      // foo.jpg at 35%, bar.jpg at 0%
+      expect(core.getState().totalProgress).toBe(18)
+
+      core.close()
     })
 
     it('should calculate the total progress of all file uploads', () => {