Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/@uppy/companion/src/server/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

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)

Expand All @@ -22,7 +24,7 @@
// @ts-ignore - typescript resolves this this to a hoisted version of
// serialize-error that ships with a declaration file, we are using a version
// here that does not have a declaration file
const serializeError = require('serialize-error')

Check warning on line 27 in packages/@uppy/companion/src/server/Uploader.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript/TypeScript

`serialize-error` import should occur before import of `./helpers/utils`
const emitter = require('./emitter')
const { jsonStringify, hasMatch } = require('./helpers/utils')
const logger = require('./logger')
Expand Down Expand Up @@ -654,7 +656,7 @@
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,
}

Expand Down
10 changes: 2 additions & 8 deletions packages/@uppy/companion/src/server/controllers/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions packages/@uppy/companion/src/server/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)