Pārlūkot izejas kodu

transloadit: Fix assembly error handling

The `afterUpload` handler wasn't returning a Promise anymore after I
changed it to support multiple simultaneous assemblies. Now it works
again, and `core.upload()` will reject if the assembly failed.
Renée Kooi 7 gadi atpakaļ
vecāks
revīzija
d0813241a5
1 mainītis faili ar 46 papildinājumiem un 27 dzēšanām
  1. 46 27
      src/plugins/Transloadit/index.js

+ 46 - 27
src/plugins/Transloadit/index.js

@@ -168,6 +168,9 @@ module.exports = class Transloadit extends Plugin {
     this.sockets[assembly.assembly_id] = socket
     this.sockets[assembly.assembly_id] = socket
 
 
     socket.on('upload', this.onFileUploadComplete.bind(this))
     socket.on('upload', this.onFileUploadComplete.bind(this))
+    socket.on('error', (error) => {
+      this.core.emit('transloadit:assembly-error', assembly, error)
+    })
 
 
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
       socket.on('result', this.onResult.bind(this))
       socket.on('result', this.onResult.bind(this))
@@ -227,40 +230,56 @@ module.exports = class Transloadit extends Plugin {
       return
       return
     }
     }
 
 
-    fileIDs.forEach((fileID) => {
-      this.core.emit('core:postprocess-progress', fileID, {
-        mode: 'indeterminate',
-        message: this.opts.locale.strings.encoding
+    return new Promise((resolve, reject) => {
+      fileIDs.forEach((fileID) => {
+        this.core.emit('core:postprocess-progress', fileID, {
+          mode: 'indeterminate',
+          message: this.opts.locale.strings.encoding
+        })
       })
       })
-    })
 
 
-    const onAssemblyFinished = (assembly) => {
-      const file = this.core.state.files[fileID]
-      // An assembly for a different upload just finished. We can ignore it.
-      if (assembly.assembly_id !== file.transloadit.assembly) {
-        return
-      }
-      // Remove this handler once we find the assembly we needed.
-      this.core.emitter.off('transloadit:complete', onAssemblyFinished)
+      const onAssemblyFinished = (assembly) => {
+        const file = this.core.state.files[fileID]
+        // An assembly for a different upload just finished. We can ignore it.
+        if (assembly.assembly_id !== file.transloadit.assembly) {
+          return
+        }
+        // Remove this handler once we find the assembly we needed.
+        this.core.emitter.off('transloadit:complete', onAssemblyFinished)
+
+        this.client.getAssemblyStatus(assembly.assembly_ssl_url).then((assembly) => {
+          this.updateState({
+            assemblies: Object.assign({}, this.state.assemblies, {
+              [assembly.assembly_id]: assembly
+            })
+          })
 
 
-      return this.client.getAssemblyStatus(assembly.assembly_ssl_url).then((assembly) => {
-        this.updateState({
-          assemblies: Object.assign({}, this.state.assemblies, {
-            [assembly.assembly_id]: assembly
+          // TODO set the `file.uploadURL` to a result?
+          // We will probably need an option here so the plugin user can tell us
+          // which result to pick…?
+
+          fileIDs.forEach((fileID) => {
+            this.core.emit('core:postprocess-complete', fileID)
           })
           })
-        })
+        }).then(resolve, reject)
+      }
 
 
-        // TODO set the `file.uploadURL` to a result?
-        // We will probably need an option here so the plugin user can tell us
-        // which result to pick…?
+      const onAssemblyError = (assembly, error) => {
+        const file = this.core.state.files[fileID]
+        // An assembly for a different upload just errored. We can ignore it.
+        if (assembly.assembly_id !== file.transloadit.assembly) {
+          return
+        }
+        // Remove this handler once we find the assembly we needed.
+        this.core.emitter.off('transloadit:assembly-error', onAssemblyError)
 
 
-        fileIDs.forEach((fileID) => {
-          this.core.emit('core:postprocess-complete', fileID)
-        })
-      })
-    }
+        // Reject the `afterUpload()` promise.
+        reject(error)
+      }
 
 
-    this.core.on('transloadit:complete', onAssemblyFinished)
+      this.core.on('transloadit:complete', onAssemblyFinished)
+      this.core.on('transloadit:assembly-error', onAssemblyError)
+    })
   }
   }
 
 
   install () {
   install () {