|
@@ -30,7 +30,6 @@ class Uploader {
|
|
|
* @property {number} size
|
|
|
* @property {string=} fieldname
|
|
|
* @property {string} pathPrefix
|
|
|
- * @property {string=} path
|
|
|
* @property {any=} s3
|
|
|
* @property {any} metadata
|
|
|
* @property {any} uppyOptions
|
|
@@ -47,7 +46,10 @@ class Uploader {
|
|
|
|
|
|
this.options = options
|
|
|
this.token = uuid.v4()
|
|
|
- this.options.path = `${this.options.pathPrefix}/${Uploader.FILE_NAME_PREFIX}-${this.token}`
|
|
|
+ this.path = `${this.options.pathPrefix}/${Uploader.FILE_NAME_PREFIX}-${this.token}`
|
|
|
+ this.metadata = Object.assign({}, this.options.metadata || {})
|
|
|
+ this.metadata.filename = this.metadata.name || path.basename(this.path)
|
|
|
+ this.metadata.filetype = this.metadata.type
|
|
|
this.streamsEnded = false
|
|
|
this.duplexStream = null
|
|
|
|
|
@@ -55,7 +57,7 @@ class Uploader {
|
|
|
|
|
|
|
|
|
|
|
|
- this.writeStream = fs.createWriteStream(this.options.path, { mode: 0o666 })
|
|
|
+ this.writeStream = fs.createWriteStream(this.path, { mode: 0o666 })
|
|
|
.on('error', (err) => logger.error(`${this.shortToken} ${err}`, 'uploader.write.error'))
|
|
|
|
|
|
this.emittedProgress = 0
|
|
@@ -143,9 +145,9 @@ class Uploader {
|
|
|
}
|
|
|
|
|
|
cleanUp () {
|
|
|
- fs.unlink(this.options.path, (err) => {
|
|
|
+ fs.unlink(this.path, (err) => {
|
|
|
if (err) {
|
|
|
- logger.error(`cleanup failed for: ${this.options.path} err: ${err}`, 'uploader.cleanup.error')
|
|
|
+ logger.error(`cleanup failed for: ${this.path} err: ${err}`, 'uploader.cleanup.error')
|
|
|
}
|
|
|
})
|
|
|
emitter().removeAllListeners(`pause:${this.token}`)
|
|
@@ -329,10 +331,7 @@ class Uploader {
|
|
|
* start the tus upload
|
|
|
*/
|
|
|
uploadTus () {
|
|
|
- const fname = path.basename(this.options.path)
|
|
|
- const ftype = this.options.metadata.type
|
|
|
- const metadata = Object.assign({ filename: fname, filetype: ftype }, this.options.metadata || {})
|
|
|
- const file = fs.createReadStream(this.options.path)
|
|
|
+ const file = fs.createReadStream(this.path)
|
|
|
const uploader = this
|
|
|
|
|
|
|
|
@@ -344,7 +343,7 @@ class Uploader {
|
|
|
resume: true,
|
|
|
retryDelays: [0, 1000, 3000, 5000],
|
|
|
uploadSize: this.bytesWritten,
|
|
|
- metadata,
|
|
|
+ metadata: this.metadata,
|
|
|
|
|
|
*
|
|
|
* @param {Error} error
|
|
@@ -373,7 +372,7 @@ class Uploader {
|
|
|
}
|
|
|
|
|
|
uploadMultipart () {
|
|
|
- const file = fs.createReadStream(this.options.path)
|
|
|
+ const file = fs.createReadStream(this.path)
|
|
|
|
|
|
|
|
|
let bytesUploaded = 0
|
|
@@ -385,7 +384,15 @@ class Uploader {
|
|
|
const formData = Object.assign(
|
|
|
{},
|
|
|
this.options.metadata,
|
|
|
- { [this.options.fieldname]: file }
|
|
|
+ {
|
|
|
+ [this.options.fieldname]: {
|
|
|
+ value: file,
|
|
|
+ options: {
|
|
|
+ filename: this.metadata.filename,
|
|
|
+ filetype: this.metadata.filetype
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
)
|
|
|
const headers = headerSanitize(this.options.headers)
|
|
|
request.post({ url: this.options.endpoint, headers, formData, encoding: null }, (error, response, body) => {
|
|
@@ -425,7 +432,7 @@ class Uploader {
|
|
|
* Upload the file to S3 while it is still being downloaded.
|
|
|
*/
|
|
|
uploadS3 () {
|
|
|
- const file = createTailReadStream(this.options.path, {
|
|
|
+ const file = createTailReadStream(this.path, {
|
|
|
tail: true
|
|
|
})
|
|
|
|
|
@@ -445,7 +452,7 @@ class Uploader {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- const filename = this.options.metadata.filename || path.basename(this.options.path)
|
|
|
+ const filename = this.options.metadata.filename || path.basename(this.path)
|
|
|
const { client, options } = this.options.s3
|
|
|
|
|
|
const upload = client.upload({
|