Parcourir la source

transloadit: Retrieve completed assembly for `complete` event

Previously, the initial assembly data was reused in the complete event…
:see_no_evil:
This patch fetches the assembly status again when the socket emits
`assembly_finished`, so we're always up to date.
Renée Kooi il y a 7 ans
Parent
commit
c524b10cbf
1 fichiers modifiés avec 22 ajouts et 16 suppressions
  1. 22 16
      src/plugins/Transloadit/index.js

+ 22 - 16
src/plugins/Transloadit/index.js

@@ -162,6 +162,17 @@ module.exports = class Transloadit extends Plugin {
     this.core.bus.emit('transloadit:result', stepName, result, this.getAssembly(assemblyId))
     this.core.bus.emit('transloadit:result', stepName, result, this.getAssembly(assemblyId))
   }
   }
 
 
+  onAssemblyFinished (url) {
+    this.client.getAssemblyStatus(url).then((assembly) => {
+      this.updateState({
+        assemblies: Object.assign({}, this.state.assemblies, {
+          [assembly.assembly_id]: assembly
+        })
+      })
+      this.core.emit('transloadit:complete', assembly)
+    })
+  }
+
   connectSocket (assembly) {
   connectSocket (assembly) {
     const socket = new StatusSocket(
     const socket = new StatusSocket(
       assembly.websocket_url,
       assembly.websocket_url,
@@ -180,10 +191,11 @@ module.exports = class Transloadit extends Plugin {
 
 
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
       socket.on('finished', () => {
       socket.on('finished', () => {
-        this.core.emit('transloadit:complete', assembly)
+        this.onAssemblyFinished(assembly.assembly_ssl_url)
       })
       })
     } else if (this.opts.waitForMetadata) {
     } else if (this.opts.waitForMetadata) {
       socket.on('metadata', () => {
       socket.on('metadata', () => {
+        this.onAssemblyFinished(assembly.assembly_ssl_url)
         this.core.emit('transloadit:complete', assembly)
         this.core.emit('transloadit:complete', assembly)
       })
       })
     }
     }
@@ -249,21 +261,15 @@ module.exports = class Transloadit extends Plugin {
         // Remove this handler once we find the assembly we needed.
         // Remove this handler once we find the assembly we needed.
         this.core.emitter.off('transloadit:complete', onAssemblyFinished)
         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
-            })
-          })
-
-          // 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…?
+
+        fileIDs.forEach((fileID) => {
+          this.core.emit('core:postprocess-complete', fileID)
+        })
+
+        resolve()
       }
       }
 
 
       const onAssemblyError = (assembly, error) => {
       const onAssemblyError = (assembly, error) => {