Bladeren bron

Transloadit: keep track of all socket connections when doing multiple uploads

Renée Kooi 8 jaren geleden
bovenliggende
commit
4ce3792c12
1 gewijzigde bestanden met toevoegingen van 16 en 12 verwijderingen
  1. 16 12
      src/plugins/Transloadit/index.js

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

@@ -59,6 +59,7 @@ module.exports = class Transloadit extends Plugin {
     }
     }
 
 
     this.client = new Client()
     this.client = new Client()
+    this.sockets = {}
   }
   }
 
 
   createAssembly (filesToUpload) {
   createAssembly (filesToUpload) {
@@ -160,30 +161,31 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   connectSocket (assembly) {
   connectSocket (assembly) {
-    this.socket = new StatusSocket(
+    const socket = new StatusSocket(
       assembly.websocket_url,
       assembly.websocket_url,
       assembly
       assembly
     )
     )
+    this.sockets[assembly.assembly_id] = socket
 
 
-    this.socket.on('upload', this.onFileUploadComplete.bind(this))
+    socket.on('upload', this.onFileUploadComplete.bind(this))
 
 
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
-      this.socket.on('result', this.onResult.bind(this))
+      socket.on('result', this.onResult.bind(this))
     }
     }
 
 
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
-      this.socket.on('finished', () => {
+      socket.on('finished', () => {
         this.core.emit('transloadit:complete', assembly)
         this.core.emit('transloadit:complete', assembly)
       })
       })
     } else if (this.opts.waitForMetadata) {
     } else if (this.opts.waitForMetadata) {
-      this.socket.on('metadata', () => {
+      socket.on('metadata', () => {
         this.core.emit('transloadit:complete', assembly)
         this.core.emit('transloadit:complete', assembly)
       })
       })
     }
     }
 
 
     return new Promise((resolve, reject) => {
     return new Promise((resolve, reject) => {
-      this.socket.on('connect', resolve)
-      this.socket.on('error', reject)
+      socket.on('connect', resolve)
+      socket.on('error', reject)
     }).then(() => {
     }).then(() => {
       this.core.log('Transloadit: Socket is ready')
       this.core.log('Transloadit: Socket is ready')
     })
     })
@@ -213,17 +215,18 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   afterUpload (fileIDs) {
   afterUpload (fileIDs) {
+    // A file ID that is part of this assembly...
+    const fileID = fileIDs[0]
+
     // If we don't have to wait for encoding metadata or results, we can close
     // If we don't have to wait for encoding metadata or results, we can close
     // the socket immediately and finish the upload.
     // the socket immediately and finish the upload.
     if (!this.shouldWait()) {
     if (!this.shouldWait()) {
-      this.socket.close()
+      const file = this.core.getState().files[fileID]
+      const socket = this.socket[file.assembly]
+      socket.close()
       return
       return
     }
     }
 
 
-    // Pick a file that is part of this assembly...
-    const fileID = fileIDs[0]
-    const file = this.core.state.files[fileID]
-
     fileIDs.forEach((fileID) => {
     fileIDs.forEach((fileID) => {
       this.core.emit('core:postprocess-progress', fileID, {
       this.core.emit('core:postprocess-progress', fileID, {
         mode: 'indeterminate',
         mode: 'indeterminate',
@@ -232,6 +235,7 @@ module.exports = class Transloadit extends Plugin {
     })
     })
 
 
     const onAssemblyFinished = (assembly) => {
     const onAssemblyFinished = (assembly) => {
+      const file = this.core.state.files[fileID]
       // An assembly for a different upload just finished. We can ignore it.
       // An assembly for a different upload just finished. We can ignore it.
       if (assembly.assembly_id !== file.transloadit.assembly) {
       if (assembly.assembly_id !== file.transloadit.assembly) {
         return
         return