Selaa lähdekoodia

Merge pull request #552 from richardwillars/multiple-uploads

Prevents files from being uploaded multiple times in separate uploads

Fixes #551
Renée Kooi 7 vuotta sitten
vanhempi
commit
9460a8caa0
3 muutettua tiedostoa jossa 82 lisäystä ja 2 poistoa
  1. 6 2
      src/core/Core.js
  2. 22 0
      src/core/Core.test.js
  3. 54 0
      src/core/__snapshots__/Core.test.js.snap

+ 6 - 2
src/core/Core.js

@@ -1103,11 +1103,15 @@ class Uppy {
       this.info(message, 'error', 5000)
       this.info(message, 'error', 5000)
       return Promise.reject(new Error(`onBeforeUpload: ${message}`))
       return Promise.reject(new Error(`onBeforeUpload: ${message}`))
     }).then(() => {
     }).then(() => {
+      const { currentUploads } = this.getState()
+      // get a list of files that are currently assigned to uploads
+      const currentlyUploadingFiles = Object.keys(currentUploads).reduce((prev, curr) => prev.concat(currentUploads[curr].fileIDs), [])
+
       const waitingFileIDs = []
       const waitingFileIDs = []
       Object.keys(this.getState().files).forEach((fileID) => {
       Object.keys(this.getState().files).forEach((fileID) => {
         const file = this.getFile(fileID)
         const file = this.getFile(fileID)
-
-        if (!file.progress.uploadStarted) {
+        // if the file hasn't started uploading and hasn't already been assigned to an upload..
+        if ((!file.progress.uploadStarted) && (currentlyUploadingFiles.indexOf(fileID) === -1)) {
           waitingFileIDs.push(file.id)
           waitingFileIDs.push(file.id)
         }
         }
       })
       })

+ 22 - 0
src/core/Core.test.js

@@ -668,6 +668,28 @@ describe('src/Core', () => {
         })
         })
       })
       })
     })
     })
+
+    it('should only upload files that are not already assigned to another upload id', () => {
+      const core = new Core().run()
+      core.store.state.currentUploads = {
+        upload1: {
+          fileIDs: ['uppy-file1jpg-image/jpeg', 'uppy-file2jpg-image/jpeg', 'uppy-file3jpg-image/jpeg']
+        },
+        upload2: {
+          fileIDs: ['uppy-file4jpg-image/jpeg', 'uppy-file5jpg-image/jpeg', 'uppy-file6jpg-image/jpeg']
+        }
+      }
+      core.addUploader((fileIDs) => Promise.resolve())
+      return Promise.all([
+        core.addFile({ source: 'jest', name: 'foo.jpg', type: 'image/jpeg', data: new Uint8Array() }),
+        core.addFile({ source: 'jest', name: 'bar.jpg', type: 'image/jpeg', data: new Uint8Array() }),
+        core.addFile({ source: 'file3', name: 'file3.jpg', type: 'image/jpeg', data: new Uint8Array() })
+      ]).then(() => {
+        return core.upload()
+      }).then((result) => {
+        expect(result).toMatchSnapshot()
+      })
+    })
   })
   })
 
 
   describe('removing a file', () => {
   describe('removing a file', () => {

+ 54 - 0
src/core/__snapshots__/Core.test.js.snap

@@ -14,3 +14,57 @@ exports[`src/Core plugins should prevent the same plugin from being added more t
         https://github.com/transloadit/uppy/issues/
         https://github.com/transloadit/uppy/issues/
         if you want us to reconsider."
         if you want us to reconsider."
 `;
 `;
+
+exports[`src/Core uploading a file should only upload files that are not already assigned to another upload id 1`] = `
+Object {
+  "failed": Array [],
+  "successful": Array [
+    Object {
+      "data": Uint8Array [],
+      "extension": "jpg",
+      "id": "uppy-foojpg-image/jpeg",
+      "isRemote": false,
+      "meta": Object {
+        "name": "foo.jpg",
+        "type": null,
+      },
+      "name": "foo.jpg",
+      "preview": undefined,
+      "progress": Object {
+        "bytesTotal": 0,
+        "bytesUploaded": 0,
+        "percentage": 0,
+        "uploadComplete": false,
+        "uploadStarted": false,
+      },
+      "remote": "",
+      "size": 0,
+      "source": "jest",
+      "type": null,
+    },
+    Object {
+      "data": Uint8Array [],
+      "extension": "jpg",
+      "id": "uppy-barjpg-image/jpeg",
+      "isRemote": false,
+      "meta": Object {
+        "name": "bar.jpg",
+        "type": null,
+      },
+      "name": "bar.jpg",
+      "preview": undefined,
+      "progress": Object {
+        "bytesTotal": 0,
+        "bytesUploaded": 0,
+        "percentage": 0,
+        "uploadComplete": false,
+        "uploadStarted": false,
+      },
+      "remote": "",
+      "size": 0,
+      "source": "jest",
+      "type": null,
+    },
+  ],
+}
+`;