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

companion: add calculated content-length for formdata and body cases #2466

Merged
merged 4 commits into from
Aug 28, 2020
Merged
Changes from 3 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
93 changes: 52 additions & 41 deletions packages/@uppy/companion/src/server/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const uuid = require('uuid')
const isObject = require('isobject')
const validator = require('validator')
const request = require('request')
const FormData = require('form-data')
const emitter = require('./emitter')
const serializeError = require('serialize-error')
const { jsonStringify, hasMatch } = require('./helpers/utils')
Expand Down Expand Up @@ -471,54 +472,64 @@ class Uploader {
const headers = headerSanitize(this.options.headers)
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
}
}
const form = new FormData()
for (const property in this.options.metadata) {
form.append(property, this.options.metadata[property])
}
form.append(this.options.fieldname, file, {
filename: this.uploadFileName,
contentType: this.options.metadata.type
})
form.getLength((error, length) => {
if (error) {
logger.error(error, 'upload.multipart.size.error')
this.emitError(error)
return
}
)
reqOptions.headers['content-length'] = length
const req = request[httpMethod](reqOptions, (error, response, body) => this._onMultipartComplete(error, response, body, bytesUploaded))
// @ts-ignore
req._form = form
})
} else {
const stats = fs.statSync(this.path)
const fileSizeInBytes = stats.size
reqOptions.headers['content-length'] = fileSizeInBytes
ifedapoolarewaju marked this conversation as resolved.
Show resolved Hide resolved
reqOptions.body = file
request[httpMethod](reqOptions, (error, response, body) => this._onMultipartComplete(error, response, body, bytesUploaded))
ifedapoolarewaju marked this conversation as resolved.
Show resolved Hide resolved
}
}

request[httpMethod](reqOptions, (error, response, body) => {
if (error) {
logger.error(error, 'upload.multipart.error')
this.emitError(error)
return
}
const headers = response.headers
// remove browser forbidden headers
delete headers['set-cookie']
delete headers['set-cookie2']

const respObj = {
responseText: body.toString(),
status: response.statusCode,
statusText: response.statusMessage,
headers
}
_onMultipartComplete (error, response, body, bytesUploaded) {
if (error) {
logger.error(error, 'upload.multipart.error')
this.emitError(error)
return
}
const headers = response.headers
// remove browser forbidden headers
delete headers['set-cookie']
delete headers['set-cookie2']

const respObj = {
responseText: body.toString(),
status: response.statusCode,
statusText: response.statusMessage,
headers
}

if (response.statusCode >= 400) {
logger.error(`upload failed with status: ${response.statusCode}`, 'upload.multipart.error')
this.emitError(new Error(response.statusMessage), respObj)
} else if (bytesUploaded !== this.bytesWritten && bytesUploaded !== this.options.size) {
const errMsg = `uploaded only ${bytesUploaded} of ${this.bytesWritten} with status: ${response.statusCode}`
logger.error(errMsg, 'upload.multipart.mismatch.error')
this.emitError(new Error(errMsg))
} else {
this.emitSuccess(null, { response: respObj })
}
if (response.statusCode >= 400) {
logger.error(`upload failed with status: ${response.statusCode}`, 'upload.multipart.error')
this.emitError(new Error(response.statusMessage), respObj)
} else if (bytesUploaded !== this.bytesWritten && bytesUploaded !== this.options.size) {
const errMsg = `uploaded only ${bytesUploaded} of ${this.bytesWritten} with status: ${response.statusCode}`
logger.error(errMsg, 'upload.multipart.mismatch.error')
this.emitError(new Error(errMsg))
} else {
this.emitSuccess(null, { response: respObj })
}

this.cleanUp()
})
this.cleanUp()
}

/**
Expand Down