Skip to content

Commit

Permalink
companion,xhr-upload,aws-s3: send useFormData option in companion (tr…
Browse files Browse the repository at this point in the history
  • Loading branch information
ifedapoolarewaju committed Apr 20, 2020
1 parent 2765650 commit 6fb5d86
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/server/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Uploader {
* @property {any=} storage
* @property {any=} headers
* @property {string=} httpMethod
* @property {boolean=} useFormData
*
* @param {UploaderOptions} options
*/
Expand Down Expand Up @@ -106,14 +107,18 @@ class Uploader {
}

static reqToOptions (req, size) {
const useFormDataIsSet = Object.prototype.hasOwnProperty.call(req.body, 'useFormData')
const useFormData = useFormDataIsSet ? req.body.useFormData : true

return {
companionOptions: req.companion.options,
endpoint: req.body.endpoint,
uploadUrl: req.body.uploadUrl,
protocol: req.body.protocol,
metadata: req.body.metadata,
httpMethod: req.body.httpMethod,
size: size,
useFormData,
size,
fieldname: req.body.fieldname,
pathPrefix: `${req.companion.options.filePath}`,
storage: redis.client(),
Expand Down Expand Up @@ -492,22 +497,28 @@ class Uploader {
this.emitIllusiveProgress(bytesUploaded)
})

const formData = Object.assign(
{},
this.options.metadata,
{
[this.options.fieldname]: {
value: file,
options: {
filename: this.uploadFileName,
contentType: this.options.metadata.type
}
}
}
)
const httpMethod = (this.options.httpMethod || '').toLowerCase() === 'put' ? 'put' : 'post'
const headers = headerSanitize(this.options.headers)
request[httpMethod]({ url: this.options.endpoint, headers, formData, encoding: null }, (error, response, body) => {
const reqOptions = { url: this.options.endpoint, headers, encoding: null }
if (this.options.useFormData) {
reqOptions.formData = Object.assign(
{},
this.options.metadata,
{
[this.options.fieldname]: {
value: file,
options: {
filename: this.uploadFileName,
contentType: this.options.metadata.type
}
}
}
)
} else {
reqOptions.body = file
}

request[httpMethod](reqOptions, (error, response, body) => {
if (error) {
logger.error(error, 'upload.multipart.error')
this.emitError(error)
Expand Down

0 comments on commit 6fb5d86

Please sign in to comment.