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

xhr-upload: send global metadata when bundle: true #1677

Merged
merged 2 commits into from
Jul 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/xhr-bundle/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const Dashboard = require('@uppy/dashboard')
const XHRUpload = require('@uppy/xhr-upload')

const uppy = Uppy({
debug: true
debug: true,
meta: { something: 'xyz' }
})

uppy.use(Dashboard, {
Expand All @@ -16,5 +17,6 @@ uppy.use(Dashboard, {
uppy.use(XHRUpload, {
bundle: true,
endpoint: 'http://localhost:9967/upload',
metaFields: ['something'],
fieldName: 'files'
})
62 changes: 40 additions & 22 deletions packages/@uppy/xhr-upload/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ module.exports = class XHRUpload extends Plugin {

getOptions (file) {
const overrides = this.uppy.getState().xhrUpload
const opts = Object.assign({},
this.opts,
overrides || {},
file.xhrUpload || {}
)
opts.headers = {}
const opts = {
...this.opts,
...(overrides || {}),
...(file.xhrUpload || {}),
headers: {}
}
Object.assign(opts.headers, this.opts.headers)
if (overrides) {
Object.assign(opts.headers, overrides.headers)
Expand Down Expand Up @@ -184,16 +184,20 @@ module.exports = class XHRUpload extends Plugin {
}
}

createFormDataUpload (file, opts) {
const formPost = new FormData()

addMetadata (formData, meta, opts) {
const metaFields = Array.isArray(opts.metaFields)
? opts.metaFields
// Send along all fields by default.
: Object.keys(file.meta)
: Object.keys(meta)
metaFields.forEach((item) => {
formPost.append(item, file.meta[item])
formData.append(item, meta[item])
})
}

createFormDataUpload (file, opts) {
const formPost = new FormData()

this.addMetadata(formPost, file.meta, opts)

const dataWithUpdatedType = setTypeInBlob(file)

Expand All @@ -206,6 +210,27 @@ module.exports = class XHRUpload extends Plugin {
return formPost
}

createBundledUpload (files, opts) {
const formPost = new FormData()

const { meta } = this.uppy.getState()
this.addMetadata(formPost, meta, opts)

files.forEach((file) => {
const opts = this.getOptions(file)

const dataWithUpdatedType = setTypeInBlob(file)

if (file.name) {
formPost.append(opts.fieldName, dataWithUpdatedType, file.name)
} else {
formPost.append(opts.fieldName, dataWithUpdatedType)
}
})

return formPost
}

createBareUpload (file, opts) {
return file.data
}
Expand Down Expand Up @@ -383,17 +408,10 @@ module.exports = class XHRUpload extends Plugin {
const endpoint = this.opts.endpoint
const method = this.opts.method

const formData = new FormData()
files.forEach((file, i) => {
const opts = this.getOptions(file)

const dataWithUpdatedType = setTypeInBlob(file)

if (file.name) {
formData.append(opts.fieldName, dataWithUpdatedType, file.meta.name)
} else {
formData.append(opts.fieldName, dataWithUpdatedType)
}
const optsFromState = this.uppy.getState().xhrUpload
const formData = this.createBundledUpload(files, {
...this.opts,
...(optsFromState || {})
})

const xhr = new XMLHttpRequest()
Expand Down