Jelajahi Sumber

Merge pull request #191 from goto-bus-stop/tl-upload-event

Transloadit: emit an upload event w/ tl data when a file upload is complete
Artur Paikin 8 tahun lalu
induk
melakukan
b353e2eeba
2 mengubah file dengan 50 tambahan dan 3 penghapusan
  1. 4 0
      src/plugins/Transloadit/Socket.js
  2. 46 3
      src/plugins/Transloadit/index.js

+ 4 - 0
src/plugins/Transloadit/Socket.js

@@ -37,6 +37,10 @@ module.exports = class TransloaditSocket {
       this.close()
       this.close()
     })
     })
 
 
+    this.socket.on('assembly_upload_finished', (file) => {
+      this.emit('upload', file)
+    })
+
     this.socket.on('assembly_upload_meta_data_extracted', () => {
     this.socket.on('assembly_upload_meta_data_extracted', () => {
       this.emit('metadata')
       this.emit('metadata')
     })
     })

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

@@ -113,16 +113,53 @@ module.exports = class Transloadit extends Plugin {
     return this.opts.waitForEncoding || this.opts.waitForMetadata
     return this.opts.waitForEncoding || this.opts.waitForMetadata
   }
   }
 
 
+  // TODO if/when the transloadit API returns tus upload metadata in the
+  // file objects in the assembly status, change this to use a unique ID
+  // instead of checking the file name and size.
+  findFile ({ name, size }) {
+    const files = this.core.state.files
+    for (const id in files) {
+      if (!files.hasOwnProperty(id)) {
+        continue
+      }
+      if (files[id].name === name && files[id].size === size) {
+        return files[id]
+      }
+    }
+  }
+
+  onFileUploadComplete (uploadedFile) {
+    const file = this.findFile(uploadedFile)
+    this.updateState({
+      files: Object.assign({}, this.state.files, {
+        [uploadedFile.id]: {
+          id: file.id,
+          uploadedFile
+        }
+      })
+    })
+    this.core.bus.emit('transloadit:upload', uploadedFile)
+  }
+
+  onResult (stepName, result) {
+    const file = this.state.files[result.original_id]
+    result.localId = file.id
+    this.updateState({
+      results: this.state.results.concat(result)
+    })
+    this.core.bus.emit('transloadit:result', stepName, result)
+  }
+
   connectSocket () {
   connectSocket () {
     this.socket = new StatusSocket(
     this.socket = new StatusSocket(
       this.state.assembly.websocket_url,
       this.state.assembly.websocket_url,
       this.state.assembly
       this.state.assembly
     )
     )
 
 
+    this.socket.on('upload', this.onFileUploadComplete.bind(this))
+
     if (this.opts.waitForEncoding) {
     if (this.opts.waitForEncoding) {
-      this.socket.on('result', (stepName, result) => {
-        this.core.bus.emit('transloadit:result', stepName, result)
-      })
+      this.socket.on('result', this.onResult.bind(this))
     }
     }
 
 
     this.assemblyReady = new Promise((resolve, reject) => {
     this.assemblyReady = new Promise((resolve, reject) => {
@@ -179,6 +216,12 @@ module.exports = class Transloadit extends Plugin {
   install () {
   install () {
     this.core.addPreProcessor(this.prepareUpload)
     this.core.addPreProcessor(this.prepareUpload)
     this.core.addPostProcessor(this.afterUpload)
     this.core.addPostProcessor(this.afterUpload)
+
+    this.updateState({
+      assembly: null,
+      files: {},
+      results: []
+    })
   }
   }
 
 
   uninstall () {
   uninstall () {