瀏覽代碼

Merge pull request #638 from transloadit/fix/progress-for-removed

Fix progress events causing errors for removed files
Artur Paikin 7 年之前
父節點
當前提交
8ae88ef334
共有 2 個文件被更改,包括 48 次插入1 次删除
  1. 30 1
      src/core/Core.js
  2. 18 0
      src/plugins/Transloadit/index.js

+ 30 - 1
src/core/Core.js

@@ -557,7 +557,7 @@ class Uppy {
 
     // skip progress event for a file that’s been removed
     if (!this.getFile(fileID)) {
-      this.log('Trying to set progress for a file that’s been removed: ', fileID)
+      this.log(`Not setting progress for a file that has been removed: ${fileID}`)
       return
     }
 
@@ -639,6 +639,10 @@ class Uppy {
     })
 
     this.on('upload-started', (fileID, upload) => {
+      if (!this.getFile(fileID)) {
+        this.log(`Not setting progress for a file that has been removed: ${fileID}`)
+        return
+      }
       const file = this.getFile(fileID)
       this.setFileState(fileID, {
         progress: Object.assign({}, file.progress, {
@@ -673,6 +677,10 @@ class Uppy {
     })
 
     this.on('preprocess-progress', (fileID, progress) => {
+      if (!this.getFile(fileID)) {
+        this.log(`Not setting progress for a file that has been removed: ${fileID}`)
+        return
+      }
       this.setFileState(fileID, {
         progress: Object.assign({}, this.getState().files[fileID].progress, {
           preprocess: progress
@@ -681,6 +689,10 @@ class Uppy {
     })
 
     this.on('preprocess-complete', (fileID) => {
+      if (!this.getFile(fileID)) {
+        this.log(`Not setting progress for a file that has been removed: ${fileID}`)
+        return
+      }
       const files = Object.assign({}, this.getState().files)
       files[fileID] = Object.assign({}, files[fileID], {
         progress: Object.assign({}, files[fileID].progress)
@@ -691,6 +703,10 @@ class Uppy {
     })
 
     this.on('postprocess-progress', (fileID, progress) => {
+      if (!this.getFile(fileID)) {
+        this.log(`Not setting progress for a file that has been removed: ${fileID}`)
+        return
+      }
       this.setFileState(fileID, {
         progress: Object.assign({}, this.getState().files[fileID].progress, {
           postprocess: progress
@@ -699,6 +715,10 @@ class Uppy {
     })
 
     this.on('postprocess-complete', (fileID) => {
+      if (!this.getFile(fileID)) {
+        this.log(`Not setting progress for a file that has been removed: ${fileID}`)
+        return
+      }
       const files = Object.assign({}, this.getState().files)
       files[fileID] = Object.assign({}, files[fileID], {
         progress: Object.assign({}, files[fileID].progress)
@@ -988,6 +1008,10 @@ class Uppy {
    * @param {object} data Data properties to add to the result object.
    */
   addResultData (uploadID, data) {
+    if (!this._getUpload(uploadID)) {
+      this.log(`Not setting result for an upload that has been removed: ${uploadID}`)
+      return
+    }
     const currentUploads = this.getState().currentUploads
     const currentUpload = Object.assign({}, currentUploads[uploadID], {
       result: Object.assign({}, currentUploads[uploadID].result, data)
@@ -1068,6 +1092,11 @@ class Uppy {
       this.addResultData(uploadID, { successful, failed, uploadID })
 
       const { currentUploads } = this.getState()
+      if (!currentUploads[uploadID]) {
+        this.log(`Not setting result for an upload that has been removed: ${uploadID}`)
+        return
+      }
+
       const result = currentUploads[uploadID].result
       this.emit('complete', result)
       // Compatibility with pre-0.21

+ 18 - 0
src/plugins/Transloadit/index.js

@@ -56,6 +56,7 @@ module.exports = class Transloadit extends Plugin {
     this.onFileUploadURLAvailable = this.onFileUploadURLAvailable.bind(this)
     this.onRestored = this.onRestored.bind(this)
     this.getPersistentData = this.getPersistentData.bind(this)
+    this.removeFileFromPluginState = this.removeFileFromPluginState.bind(this)
 
     if (this.opts.params) {
       this.validateParams(this.opts.params)
@@ -297,6 +298,10 @@ module.exports = class Transloadit extends Plugin {
   onFileUploadComplete (assemblyId, uploadedFile) {
     const state = this.getPluginState()
     const file = this.findFile(uploadedFile)
+    if (!file) {
+      this.uppy.log('[Transloadit] Couldn’t file the file, it was likely removed in the process')
+      return
+    }
     this.setPluginState({
       files: Object.assign({}, state.files, {
         [uploadedFile.id]: {
@@ -764,6 +769,15 @@ module.exports = class Transloadit extends Plugin {
     })
   }
 
+  removeFileFromPluginState (fileID) {
+    const updatedFiles = this.getPluginState().files
+    delete updatedFiles[fileID]
+
+    this.setPluginState({
+      files: updatedFiles
+    })
+  }
+
   install () {
     this.uppy.addPreProcessor(this.prepareUpload)
     this.uppy.addPostProcessor(this.afterUpload)
@@ -784,6 +798,8 @@ module.exports = class Transloadit extends Plugin {
     this.uppy.on('restore:get-data', this.getPersistentData)
     this.uppy.on('restored', this.onRestored)
 
+    // this.uppy.on('file-removed', this.removeFileFromPluginState)
+
     this.setPluginState({
       // Contains assembly status objects, indexed by their ID.
       assemblies: {},
@@ -800,6 +816,8 @@ module.exports = class Transloadit extends Plugin {
     this.uppy.removePreProcessor(this.prepareUpload)
     this.uppy.removePostProcessor(this.afterUpload)
 
+    // this.uppy.off('file-removed', this.removeFileFromPluginState)
+
     if (this.opts.importFromUploadURLs) {
       this.uppy.off('upload-success', this.onFileUploadURLAvailable)
     }