Browse Source

transloadit: Begin work on importing uploaded URLs into assembly.

Support uploading to elsewhere, then importing URLs into assembly.
Renée Kooi 7 years ago
parent
commit
cde56fa491
2 changed files with 47 additions and 2 deletions
  1. 18 0
      src/plugins/Transloadit/Client.js
  2. 29 2
      src/plugins/Transloadit/index.js

+ 18 - 0
src/plugins/Transloadit/Client.js

@@ -47,6 +47,24 @@ module.exports = class Client {
     })
   }
 
+  reserveFile (assembly, file) {
+    const size = encodeURIComponent(file.size)
+    return fetch(`${assembly.assembly_ssl_url}/reserve_file?size=${size}`)
+      .then((response) => response.json())
+  }
+
+  addFile (assembly, file) {
+    if (!file.uploadURL) {
+      return Promise.reject(new Error('File does not have an `uploadURL`.'))
+    }
+    const size = encodeURIComponent(file.size)
+    const url = encodeURIComponent(file.uploadURL)
+    const filename = encodeURIComponent(file.name)
+    const fieldname = 'file'
+    return fetch(`${assembly.assembly_ssl_url}/add_file?size=${size}&filename=${filename}&fieldname=${fieldname}&s3Url=${url}`)
+      .then((response) => response.json())
+  }
+
   /**
    * Get the current status for an assembly.
    *

+ 29 - 2
src/plugins/Transloadit/index.js

@@ -24,6 +24,7 @@ module.exports = class Transloadit extends Plugin {
       waitForEncoding: false,
       waitForMetadata: false,
       alwaysRunAssembly: false, // TODO name
+      importFromUploadURLs: false,
       signature: null,
       params: null,
       fields: {},
@@ -44,6 +45,7 @@ module.exports = class Transloadit extends Plugin {
 
     this.prepareUpload = this.prepareUpload.bind(this)
     this.afterUpload = this.afterUpload.bind(this)
+    this.onFileUploaded = this.onFileUploaded.bind(this)
 
     if (this.opts.params) {
       this.validateParams(this.opts.params)
@@ -145,7 +147,8 @@ module.exports = class Transloadit extends Plugin {
         })
         // Add assembly-specific Tus endpoint.
         const tus = Object.assign({}, file.tus, {
-          endpoint: assembly.tus_url
+          endpoint: assembly.tus_url,
+          metaFields: ['assembly_url', 'filename', 'fieldname']
         })
         const transloadit = {
           assembly: assembly.assembly_id
@@ -183,6 +186,19 @@ module.exports = class Transloadit extends Plugin {
     return this.opts.waitForEncoding || this.opts.waitForMetadata
   }
 
+  onFileUploaded (fileID) {
+    const file = this.core.getState().files[fileID]
+    if (!file || !file.transloadit || !file.transloadit.assembly) {
+      return
+    }
+
+    const assembly = this.state.assemblies[file.transloadit.assembly]
+
+    this.client.addFile(assembly, file).catch((err) => {
+      console.error('ignoring', err)
+    })
+  }
+
   findFile (uploadedFile) {
     const files = this.core.state.files
     for (const id in files) {
@@ -274,7 +290,14 @@ module.exports = class Transloadit extends Plugin {
     })
 
     const createAssembly = ({ fileIDs, options }) => {
-      return this.createAssembly(fileIDs, uploadID, options).then(() => {
+      return this.createAssembly(fileIDs, uploadID, options).then((assembly) => {
+        if (this.opts.importFromUploadURLs) {
+          return Promise.all(fileIDs.map((fileID) => {
+            const file = this.core.getFile(fileID)
+            return this.client.reserveFile(assembly, file)
+          }))
+        }
+      }).then(() => {
         fileIDs.forEach((fileID) => {
           this.core.emit('core:preprocess-complete', fileID)
         })
@@ -403,6 +426,10 @@ module.exports = class Transloadit extends Plugin {
     this.core.addPreProcessor(this.prepareUpload)
     this.core.addPostProcessor(this.afterUpload)
 
+    if (this.opts.importFromUploadURLs) {
+      this.core.on('file:upload-success', this.onFileUploaded)
+    }
+
     this.updateState({
       // Contains assembly status objects, indexed by their ID.
       assemblies: {},