Browse Source

@uppy/companion: Fix Uploader.js metadata normalisation (#4608)

* Fix Uploader.js metadata name normalization

* fix indentation issue

* refactor/reuse

exact same code in s3 controller

---------

Co-authored-by: Mikael Finstad <finstaden@gmail.com>
Subha Sarkar 1 year ago
parent
commit
d6e1013b64

+ 3 - 1
packages/@uppy/companion/src/server/Uploader.js

@@ -12,6 +12,8 @@ const throttle = require('lodash/throttle')
 
 const { Upload } = require('@aws-sdk/lib-storage')
 
+const { rfc2047EncodeMetadata } = require('./helpers/utils')
+
 // TODO move to `require('streams/promises').pipeline` when dropping support for Node.js 14.x.
 const pipeline = promisify(pipelineCb)
 
@@ -654,7 +656,7 @@ class Uploader {
       Bucket: options.bucket,
       Key: options.getKey(null, filename, this.options.metadata),
       ContentType: this.options.metadata.type,
-      Metadata: this.options.metadata,
+      Metadata: rfc2047EncodeMetadata(this.options.metadata),
       Body: stream,
     }
 

+ 2 - 8
packages/@uppy/companion/src/server/controllers/s3.js

@@ -14,13 +14,7 @@ const {
 const { createPresignedPost } = require('@aws-sdk/s3-presigned-post')
 const { getSignedUrl } = require('@aws-sdk/s3-request-presigner')
 
-function rfc2047Encode (data) {
-  // eslint-disable-next-line no-param-reassign
-  data = `${data}`
-  // eslint-disable-next-line no-control-regex
-  if (/^[\x00-\x7F]*$/.test(data)) return data // we return ASCII as is
-  return `=?UTF-8?B?${Buffer.from(data).toString('base64')}?=` // We encode non-ASCII strings
-}
+const { rfc2047EncodeMetadata } = require('../helpers/utils')
 
 module.exports = function s3 (config) {
   if (typeof config.acl !== 'string' && config.acl != null) {
@@ -138,7 +132,7 @@ module.exports = function s3 (config) {
       Bucket: bucket,
       Key: key,
       ContentType: type,
-      Metadata: Object.fromEntries(Object.entries(metadata).map(entry => entry.map(rfc2047Encode))),
+      Metadata: rfc2047EncodeMetadata(metadata),
     }
 
     if (config.acl != null) params.ACL = config.acl

+ 11 - 0
packages/@uppy/companion/src/server/helpers/utils.js

@@ -174,3 +174,14 @@ module.exports.getBasicAuthHeader = (key, secret) => {
   const base64 = Buffer.from(`${key}:${secret}`, 'binary').toString('base64')
   return `Basic ${base64}`
 }
+
+const rfc2047Encode = (dataIn) => {
+  const data = `${dataIn}`
+  // eslint-disable-next-line no-control-regex
+  if (/^[\x00-\x7F]*$/.test(data)) return data // we return ASCII as is
+  return `=?UTF-8?B?${Buffer.from(data).toString('base64')}?=` // We encode non-ASCII strings
+}
+
+module.exports.rfc2047EncodeMetadata = (metadata) => (
+  Object.fromEntries(Object.entries(metadata).map((entry) => entry.map(rfc2047Encode)))
+)