Ver código fonte

Merge pull request #1587 from transloadit/companion-filename

companion: set upload  filename from metadata during uploads
Ifedapo .A. Olarewaju 5 anos atrás
pai
commit
3c084862f3
1 arquivos alterados com 27 adições e 14 exclusões
  1. 27 14
      packages/@uppy/companion/src/server/Uploader.js

+ 27 - 14
packages/@uppy/companion/src/server/Uploader.js

@@ -30,7 +30,6 @@ class Uploader {
    * @property {number} size
    * @property {string=} fieldname
    * @property {string} pathPrefix
-   * @property {string=} path
    * @property {any=} s3
    * @property {any} metadata
    * @property {any} uppyOptions
@@ -47,7 +46,9 @@ class Uploader {
 
     this.options = options
     this.token = uuid.v4()
-    this.options.path = `${this.options.pathPrefix}/${Uploader.FILE_NAME_PREFIX}-${this.token}`
+    this.path = `${this.options.pathPrefix}/${Uploader.FILE_NAME_PREFIX}-${this.token}`
+    this.options.metadata = this.options.metadata || {}
+    this.uploadFileName = this.options.metadata.name || path.basename(this.path)
     this.streamsEnded = false
     this.duplexStream = null
     // @TODO disabling parallel uploads and downloads for now
@@ -55,7 +56,7 @@ class Uploader {
     //   this.duplexStream = new stream.PassThrough()
     //     .on('error', (err) => logger.error(`${this.shortToken} ${err}`, 'uploader.duplex.error'))
     // }
-    this.writeStream = fs.createWriteStream(this.options.path, { mode: 0o666 }) // no executable files
+    this.writeStream = fs.createWriteStream(this.path, { mode: 0o666 }) // no executable files
       .on('error', (err) => logger.error(`${this.shortToken} ${err}`, 'uploader.write.error'))
     /** @type {number} */
     this.emittedProgress = 0
@@ -143,9 +144,9 @@ class Uploader {
   }
 
   cleanUp () {
-    fs.unlink(this.options.path, (err) => {
+    fs.unlink(this.path, (err) => {
       if (err) {
-        logger.error(`cleanup failed for: ${this.options.path} err: ${err}`, 'uploader.cleanup.error')
+        logger.error(`cleanup failed for: ${this.path} err: ${err}`, 'uploader.cleanup.error')
       }
     })
     emitter().removeAllListeners(`pause:${this.token}`)
@@ -329,10 +330,7 @@ class Uploader {
    * start the tus upload
    */
   uploadTus () {
-    const fname = path.basename(this.options.path)
-    const ftype = this.options.metadata.type
-    const metadata = Object.assign({ filename: fname, filetype: ftype }, this.options.metadata || {})
-    const file = fs.createReadStream(this.options.path)
+    const file = fs.createReadStream(this.path)
     const uploader = this
 
     // @ts-ignore
@@ -344,7 +342,14 @@ class Uploader {
       resume: true,
       retryDelays: [0, 1000, 3000, 5000],
       uploadSize: this.bytesWritten,
-      metadata,
+      metadata: Object.assign(
+        {
+          // file name and type as required by the tusd tus server
+          // https://github.com/tus/tusd/blob/5b376141903c1fd64480c06dde3dfe61d191e53d/unrouted_handler.go#L614-L646
+          filename: this.uploadFileName,
+          filetype: this.options.metadata.type
+        }, this.options.metadata
+      ),
       /**
        *
        * @param {Error} error
@@ -373,7 +378,7 @@ class Uploader {
   }
 
   uploadMultipart () {
-    const file = fs.createReadStream(this.options.path)
+    const file = fs.createReadStream(this.path)
 
     // upload progress
     let bytesUploaded = 0
@@ -385,7 +390,15 @@ class Uploader {
     const formData = Object.assign(
       {},
       this.options.metadata,
-      { [this.options.fieldname]: file }
+      {
+        [this.options.fieldname]: {
+          value: file,
+          options: {
+            filename: this.uploadFileName,
+            contentType: this.options.metadata.type
+          }
+        }
+      }
     )
     const headers = headerSanitize(this.options.headers)
     request.post({ url: this.options.endpoint, headers, formData, encoding: null }, (error, response, body) => {
@@ -425,7 +438,7 @@ class Uploader {
    * Upload the file to S3 while it is still being downloaded.
    */
   uploadS3 () {
-    const file = createTailReadStream(this.options.path, {
+    const file = createTailReadStream(this.path, {
       tail: true
     })
 
@@ -445,7 +458,7 @@ class Uploader {
       return
     }
 
-    const filename = this.options.metadata.filename || path.basename(this.options.path)
+    const filename = this.options.metadata.filename || path.basename(this.path)
     const { client, options } = this.options.s3
 
     const upload = client.upload({