From c8133c1250fe31a20c0c66918a20fb37419297ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 28 Sep 2017 11:20:36 +0200 Subject: [PATCH 1/5] xhrupload: Merge headers from different options locations --- src/plugins/XHRUpload.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/plugins/XHRUpload.js b/src/plugins/XHRUpload.js index e4a3e3fdb..8f6962b3f 100644 --- a/src/plugins/XHRUpload.js +++ b/src/plugins/XHRUpload.js @@ -62,6 +62,14 @@ module.exports = class XHRUpload extends Plugin { this.core.state.xhrUpload || {}, file.xhrUpload || {} ) + opts.headers = {} + Object.assign(opts.headers, this.opts.headers) + if (this.core.state.xhrUpload) { + Object.assign(opts.headers, this.core.state.xhrUpload.headers) + } + if (file.xhrUpload) { + Object.assign(opts.headers, file.xhrUpload.headers) + } this.core.log(`uploading ${current} of ${total}`) return new Promise((resolve, reject) => { @@ -100,14 +108,6 @@ module.exports = class XHRUpload extends Plugin { this.core.emit('core:upload-error', file.id, error) return reject(error) } - - // var upload = {} - // - // if (opts.bundle) { - // upload = {files: files} - // } else { - // upload = {file: files[current]} - // } }) xhr.addEventListener('error', (ev) => { From eee3142d4280a4cda6230814bd6ee84c34d0c4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 28 Sep 2017 11:21:12 +0200 Subject: [PATCH 2/5] s3: Allow request headers to be returned from getUploadParameters Closes #243 This way headers like `Content-Disposition` can be added without requiring support for that on the signing server side. ```js uppy.use(AwsS3, { getUploadParameters (file) { return fetch(`/sign?name=${file.name}&type=${file.type.mime}`) .then((response) => response.json()) .then((data) => Object.assign(data, { 'Content-Disposition': file.meta.contentDisposition })) } }) // later uppy.addFile({ name: 'xyz.jpg', mime: 'image/jpeg', data: someArrayBuffer, meta: { contentDisposition: 'inline' } }) ``` --- src/plugins/AwsS3/index.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/plugins/AwsS3/index.js b/src/plugins/AwsS3/index.js index 31f6af444..aab3b442e 100644 --- a/src/plugins/AwsS3/index.js +++ b/src/plugins/AwsS3/index.js @@ -99,17 +99,24 @@ module.exports = class AwsS3 extends Plugin { const { method = 'post', url, - fields + fields, + headers } = responses[index] + const xhrOpts = { + method, + formData: method.toLowerCase() === 'post', + endpoint: url, + fieldName: 'file', + metaFields: Object.keys(fields) + } + + if (headers) { + xhrOpts.headers = headers + } + const updatedFile = Object.assign({}, file, { meta: Object.assign({}, file.meta, fields), - xhrUpload: { - method, - formData: method.toLowerCase() === 'post', - endpoint: url, - fieldName: 'file', - metaFields: Object.keys(fields) - } + xhrUpload: xhrOpts }) updatedFiles[id] = updatedFile From df6d4f06c10b481ad4cff6bd164da938234eb7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 28 Sep 2017 11:26:24 +0200 Subject: [PATCH 3/5] xhrupload: DRY options merging --- src/plugins/XHRUpload.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/plugins/XHRUpload.js b/src/plugins/XHRUpload.js index 8f6962b3f..3c34c6ad3 100644 --- a/src/plugins/XHRUpload.js +++ b/src/plugins/XHRUpload.js @@ -36,6 +36,24 @@ module.exports = class XHRUpload extends Plugin { this.handleUpload = this.handleUpload.bind(this) } + getOptions (file) { + const opts = Object.assign({}, + this.opts, + this.core.state.xhrUpload || {}, + file.xhrUpload || {} + ) + opts.headers = {} + Object.assign(opts.headers, this.opts.headers) + if (this.core.state.xhrUpload) { + Object.assign(opts.headers, this.core.state.xhrUpload.headers) + } + if (file.xhrUpload) { + Object.assign(opts.headers, file.xhrUpload.headers) + } + + return opts + } + createFormDataUpload (file, opts) { const formPost = new FormData() @@ -57,19 +75,7 @@ module.exports = class XHRUpload extends Plugin { } upload (file, current, total) { - const opts = Object.assign({}, - this.opts, - this.core.state.xhrUpload || {}, - file.xhrUpload || {} - ) - opts.headers = {} - Object.assign(opts.headers, this.opts.headers) - if (this.core.state.xhrUpload) { - Object.assign(opts.headers, this.core.state.xhrUpload.headers) - } - if (file.xhrUpload) { - Object.assign(opts.headers, file.xhrUpload.headers) - } + const opts = this.getOptions(file) this.core.log(`uploading ${current} of ${total}`) return new Promise((resolve, reject) => { @@ -140,7 +146,7 @@ module.exports = class XHRUpload extends Plugin { } uploadRemote (file, current, total) { - const opts = Object.assign({}, this.opts, file.xhrUpload || {}) + const opts = this.getOptions(file) return new Promise((resolve, reject) => { this.core.emit('core:upload-started', file.id) From f65c899562e5354d75f166bb471654c889f8b45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 28 Sep 2017 11:31:19 +0200 Subject: [PATCH 4/5] =?UTF-8?q?changelog:=20=E2=98=91=20s3=20headers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 252707acd..1193f17b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,7 +115,7 @@ Theme: React and Retry - [x] goldenretriever: Omit completed uploads from saved file state—previously, when an upload was finished and the user refreshed the page, all the finished files would still be there because we saved the entire list of files. Changed this to only store files that are part of an in-progress upload, or that have yet to be uploaded (#358, #324 / @goto-bus-stop) - [x] goldenretriever: Remove files from cache when upload finished—this uses the deleteBlobs function when core:success fires (#358, #324 / @goto-bus-stop) - [ ] goldenretriever: add a timestamp to cached blobs, and to delete old blobs on boot (#358, #324 / @goto-bus-stop) -- [ ] s3: have some way to configure content-disposition for uploads, see #243 (@goto-bus-stop) +- [x] s3: have some way to configure content-disposition for uploads, see #243 (@goto-bus-stop) - [x] core: move `setPluginState` and add `getPluginState` to `Plugin` class (#363 / @goto-bus-stop) ## 0.19.1 From d16a7d8dbbe18fb4bf3b642e4c3156db3f40d6aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Mon, 2 Oct 2017 17:37:19 +0200 Subject: [PATCH 5/5] xhrupload: add `fields` and `headers` to uppy-server request --- src/plugins/XHRUpload.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/plugins/XHRUpload.js b/src/plugins/XHRUpload.js index 3c34c6ad3..1f83abfda 100644 --- a/src/plugins/XHRUpload.js +++ b/src/plugins/XHRUpload.js @@ -150,6 +150,16 @@ module.exports = class XHRUpload extends Plugin { return new Promise((resolve, reject) => { this.core.emit('core:upload-started', file.id) + const fields = {} + const metaFields = Array.isArray(opts.metaFields) + ? opts.metaFields + // Send along all fields by default. + : Object.keys(file.meta) + + metaFields.forEach((name) => { + fields[name] = file.meta.name + }) + fetch(file.remote.url, { method: 'post', credentials: 'include', @@ -160,7 +170,9 @@ module.exports = class XHRUpload extends Plugin { body: JSON.stringify(Object.assign({}, file.remote.body, { endpoint: opts.endpoint, size: file.data.size, - fieldname: opts.fieldName + fieldname: opts.fieldName, + fields, + headers: opts.headers })) }) .then((res) => {