From f1f0d63a24035227f469cb31cb4ebe5d6b418bd0 Mon Sep 17 00:00:00 2001 From: Radek Pazdera Date: Thu, 9 Jan 2020 18:09:08 +0000 Subject: [PATCH] feat: Allow overriding metadata when deploying to s3 Sometimes, the ContentType detection fails for certain files when uploading. This adds the option to set metadata in project config. --- lib/deploy-methods/s3.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/deploy-methods/s3.js b/lib/deploy-methods/s3.js index 06573f5..d308790 100644 --- a/lib/deploy-methods/s3.js +++ b/lib/deploy-methods/s3.js @@ -11,7 +11,7 @@ function _updateProgress(reporter, message) { } } -function _doRelease(build, target, algorithm, reporter) { +function _doRelease(build, target, algorithm, fileOptions, reporter) { return new Promise((resolve, reject) => { const bucketMap = {}; const extract = tarStream.extract(); @@ -52,6 +52,11 @@ function _doRelease(build, target, algorithm, reporter) { _updateProgress(reporter, `Uploading ${header.name}`); } + let contentType = mime.lookup(header.name) || 'application/octet-stream'; + if (fileOptions[header.name] && fileOptions[header.name].ContentType) { + contentType = fileOptions[header.name].ContentType; + } + s3.getInstance().upload( { Bucket: target, @@ -59,7 +64,7 @@ function _doRelease(build, target, algorithm, reporter) { Body: stream, ACL: 'public-read', CacheControl: 'max-age=600', - ContentType: mime.lookup(header.name) || 'application/octet-stream', + ContentType: contentType, }, (err) => { if (err) { @@ -144,18 +149,19 @@ function _downloadKartFile(project, channel) { * * overwrite: Writes all the files into the bucked without clearing it out first. * * sync: Uses the `aws sync` command to minimise bandwith usage. * - * @param {Object} build The build object to be released. - * @param {Object} opts Options. - * @param {String} opts.bucket Name of the bucket to deploy to. - * @param {String} opts.algorithm Either 'clear', 'overwrite' or 'sync'. - * @param {String} opts.reporter An optional event listener to report events. + * @param {Object} build The build object to be released. + * @param {Object} opts Options. + * @param {String} opts.bucket Name of the bucket to deploy to. + * @param {String} opts.algorithm Either 'clear', 'overwrite' or 'sync'. + * @param {Object} opts.fileOptions Per-file options that will be applied when uploading to S3 (metadata, etc). + * @param {String} opts.reporter An optional event listener to report events. */ function release(build, opts) { - const { bucket, algorithm = 'clear' } = opts; + const { bucket, algorithm = 'clear', fileOptions = {} } = opts; let releaseObject; return build.fetchMetadata() - .then(() => (algorithm === 'clear' ? s3.clearBucket(bucket) : Promise.resolve())).then(() => _doRelease(build, bucket, algorithm, opts.reporter)).then((r) => { + .then(() => (algorithm === 'clear' ? s3.clearBucket(bucket) : Promise.resolve())).then(() => _doRelease(build, bucket, algorithm, fileOptions, opts.reporter)).then((r) => { releaseObject = r; return _uploadKartFile(releaseObject, bucket); })