瀏覽代碼

Merge pull request #291 from goto-bus-stop/bugfix/tl-alwaysrunassembly

transloadit: Fix crash when no files are being uploaded
Artur Paikin 7 年之前
父節點
當前提交
28ee03fead
共有 2 個文件被更改,包括 37 次插入4 次删除
  1. 18 4
      src/plugins/Transloadit/index.js
  2. 19 0
      test/unit/Transloadit.spec.js

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

@@ -120,8 +120,10 @@ module.exports = class Transloadit extends Plugin {
       signature: options.signature
     }).then((assembly) => {
       // Store the list of assemblies related to this upload.
-      const uploadsAssemblies = Object.assign({}, this.state.uploadsAssemblies)
-      uploadsAssemblies[uploadID] = (uploadsAssemblies[uploadID] || []).concat([ assembly.assembly_id ])
+      const assemblyList = this.state.uploadsAssemblies[uploadID]
+      const uploadsAssemblies = Object.assign({}, this.state.uploadsAssemblies, {
+        [uploadID]: assemblyList.concat([ assembly.assembly_id ])
+      })
 
       this.updateState({
         assemblies: Object.assign(this.state.assemblies, {
@@ -277,13 +279,19 @@ module.exports = class Transloadit extends Plugin {
       })
     }
 
+    const uploadsAssemblies = Object.assign({},
+      this.state.uploadsAssemblies,
+      { [uploadID]: [] })
+    this.updateState({ uploadsAssemblies })
+
     let optionsPromise
     if (fileIDs.length > 0) {
       optionsPromise = this.getAssemblyOptions(fileIDs)
         .then((allOptions) => this.dedupeAssemblyOptions(allOptions))
     } else if (this.opts.alwaysRunAssembly) {
-      optionsPromise = Promise.resolve().then(() => {
-        const options = this.opts.getAssemblyOptions(null, this.opts)
+      optionsPromise = Promise.resolve(
+        this.opts.getAssemblyOptions(null, this.opts)
+      ).then((options) => {
         this.validateParams(options.params)
         return [
           { fileIDs, options }
@@ -313,6 +321,12 @@ module.exports = class Transloadit extends Plugin {
       return Promise.resolve()
     }
 
+    // If no assemblies were created for this upload, we also do not have to wait.
+    // There's also no sockets or anything to close, so just return immediately.
+    if (assemblyIDs.length === 0) {
+      return Promise.resolve()
+    }
+
     let finishedAssemblies = 0
 
     return new Promise((resolve, reject) => {

+ 19 - 0
test/unit/Transloadit.spec.js

@@ -165,3 +165,22 @@ test('Does not create an assembly if no files are being uploaded', (t) => {
     t.end()
   }).catch(t.fail)
 })
+
+test('Creates an assembly if no files are being uploaded but `alwaysRunAssembly` is enabled', (t) => {
+  t.plan(1)
+
+  const uppy = new Core()
+  uppy.use(Transloadit, {
+    alwaysRunAssembly: true,
+    getAssemblyOptions (file) {
+      t.equal(file, null, 'should call getAssemblyOptions with `null`')
+      return Promise.reject()
+    }
+  })
+
+  uppy.upload().then(() => {
+    t.fail('should be rejected by `getAssemblyOptions`')
+  }, () => {
+    t.end()
+  })
+})