Explorar o código

aws-s3-multipart: Proper cleanup on cancellation, fixes #992 (#1021)

Previously, cancelling an upload would not reset all upload-related
objects used by the S3 Multipart plugin. With this patch, cancellation
behaves similarly to files being removed (they are conceptually similar).
Renée Kooi %!s(int64=6) %!d(string=hai) anos
pai
achega
d5de77d03b

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

@@ -254,6 +254,7 @@ class MultipartUploader {
       key: this.key,
       key: this.key,
       uploadId: this.uploadId
       uploadId: this.uploadId
     })
     })
+    this.uploading = []
   }
   }
 
 
   _onError (err) {
   _onError (err) {

+ 14 - 19
packages/@uppy/aws-s3-multipart/src/index.js

@@ -70,9 +70,9 @@ module.exports = class AwsS3Multipart extends Plugin {
    * Clean up all references for a file's upload: the MultipartUploader instance,
    * Clean up all references for a file's upload: the MultipartUploader instance,
    * any events related to the file, and the uppy-server WebSocket connection.
    * any events related to the file, and the uppy-server WebSocket connection.
    */
    */
-  resetUploaderReferences (fileID) {
+  resetUploaderReferences (fileID, opts = {}) {
     if (this.uploaders[fileID]) {
     if (this.uploaders[fileID]) {
-      this.uploaders[fileID].abort()
+      this.uploaders[fileID].abort({ really: opts.abort || false })
       this.uploaders[fileID] = null
       this.uploaders[fileID] = null
     }
     }
     if (this.uploaderEvents[fileID]) {
     if (this.uploaderEvents[fileID]) {
@@ -203,7 +203,7 @@ module.exports = class AwsS3Multipart extends Plugin {
       this.uploaderEvents[file.id] = createEventTracker(this.uppy)
       this.uploaderEvents[file.id] = createEventTracker(this.uppy)
 
 
       this.onFileRemove(file.id, (removed) => {
       this.onFileRemove(file.id, (removed) => {
-        this.resetUploaderReferences(file.id)
+        this.resetUploaderReferences(file.id, { abort: true })
         resolve(`upload ${removed.id} was removed`)
         resolve(`upload ${removed.id} was removed`)
       })
       })
 
 
@@ -219,10 +219,6 @@ module.exports = class AwsS3Multipart extends Plugin {
         upload.pause()
         upload.pause()
       })
       })
 
 
-      this.onCancelAll(file.id, () => {
-        upload.abort({ really: true })
-      })
-
       this.onResumeAll(file.id, () => {
       this.onResumeAll(file.id, () => {
         upload.start()
         upload.start()
       })
       })
@@ -293,7 +289,7 @@ module.exports = class AwsS3Multipart extends Plugin {
       this.uploaderEvents[file.id] = createEventTracker(this.uppy)
       this.uploaderEvents[file.id] = createEventTracker(this.uppy)
 
 
       this.onFileRemove(file.id, (removed) => {
       this.onFileRemove(file.id, (removed) => {
-        socket.send('pause', {})
+        this.resetUploaderReferences(file.id, { abort: true })
         resolve(`upload ${file.id} was removed`)
         resolve(`upload ${file.id} was removed`)
       })
       })
 
 
@@ -303,8 +299,6 @@ module.exports = class AwsS3Multipart extends Plugin {
 
 
       this.onPauseAll(file.id, () => socket.send('pause', {}))
       this.onPauseAll(file.id, () => socket.send('pause', {}))
 
 
-      this.onCancelAll(file.id, () => socket.send('pause', {}))
-
       this.onResumeAll(file.id, () => {
       this.onResumeAll(file.id, () => {
         if (file.error) {
         if (file.error) {
           socket.send('pause', {})
           socket.send('pause', {})
@@ -391,13 +385,6 @@ module.exports = class AwsS3Multipart extends Plugin {
     })
     })
   }
   }
 
 
-  onCancelAll (fileID, cb) {
-    this.uploaderEvents[fileID].on('cancel-all', () => {
-      if (!this.uppy.getFile(fileID)) return
-      cb()
-    })
-  }
-
   onResumeAll (fileID, cb) {
   onResumeAll (fileID, cb) {
     this.uploaderEvents[fileID].on('resume-all', () => {
     this.uploaderEvents[fileID].on('resume-all', () => {
       if (!this.uppy.getFile(fileID)) return
       if (!this.uppy.getFile(fileID)) return
@@ -406,12 +393,20 @@ module.exports = class AwsS3Multipart extends Plugin {
   }
   }
 
 
   install () {
   install () {
+    const { capabilities } = this.uppy.getState()
     this.uppy.setState({
     this.uppy.setState({
-      capabilities: Object.assign({}, this.uppy.getState().capabilities, {
+      capabilities: {
+        ...capabilities,
         resumableUploads: true
         resumableUploads: true
-      })
+      }
     })
     })
     this.uppy.addUploader(this.upload)
     this.uppy.addUploader(this.upload)
+
+    this.uppy.on('cancel-all', () => {
+      this.uppy.getFiles().forEach((file) => {
+        this.resetUploaderReferences(file.id, { abort: true })
+      })
+    })
   }
   }
 
 
   uninstall () {
   uninstall () {