/
Uploader.js
611 lines (546 loc) · 18.1 KB
/
Uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
const fs = require('fs')
const path = require('path')
const tus = require('tus-js-client')
const uuid = require('uuid')
const isObject = require('isobject')
const validator = require('validator')
const request = require('request')
/** @type {any} */
// @ts-ignore - typescript resolves this this to a hoisted version of
// serialize-error that ships with a declaration file, we are using a version
// here that does not have a declaration file
const serializeError = require('serialize-error')
const emitter = require('./emitter')
const { jsonStringify, hasMatch } = require('./helpers/utils')
const logger = require('./logger')
const headerSanitize = require('./header-blacklist')
const redis = require('./redis')
const DEFAULT_FIELD_NAME = 'files[]'
const PROTOCOLS = Object.freeze({
multipart: 'multipart',
s3Multipart: 's3-multipart',
tus: 'tus',
})
class Uploader {
/**
* Uploads file to destination based on the supplied protocol (tus, s3-multipart, multipart)
* For tus uploads, the deferredLength option is enabled, because file size value can be unreliable
* for some providers (Instagram particularly)
*
* @typedef {object} UploaderOptions
* @property {string} endpoint
* @property {string=} uploadUrl
* @property {string} protocol
* @property {number} size
* @property {string=} fieldname
* @property {string} pathPrefix
* @property {any=} s3
* @property {any} metadata
* @property {any} companionOptions
* @property {any=} storage
* @property {any=} headers
* @property {string=} httpMethod
* @property {boolean=} useFormData
* @property {number=} chunkSize
*
* @param {UploaderOptions} options
*/
constructor (options) {
if (!this.validateOptions(options)) {
logger.debug(this._errRespMessage, 'uploader.validator.fail')
return
}
this.options = options
this.token = uuid.v4()
this.path = `${this.options.pathPrefix}/${Uploader.FILE_NAME_PREFIX}-${this.token}`
this.options.metadata = this.options.metadata || {}
this.options.fieldname = this.options.fieldname || DEFAULT_FIELD_NAME
this.uploadFileName = this.options.metadata.name || path.basename(this.path)
this.streamsEnded = false
this.uploadStopped = false
this.writeStream = fs.createWriteStream(this.path, { mode: 0o666 }) // no executable files
.on('error', (err) => logger.error(`${err}`, 'uploader.write.error', this.shortToken))
/** @type {number} */
this.emittedProgress = 0
this.storage = options.storage
this._paused = false
if (this.options.protocol === PROTOCOLS.tus) {
emitter().on(`pause:${this.token}`, () => {
this._paused = true
if (this.tus) {
this.tus.abort()
}
})
emitter().on(`resume:${this.token}`, () => {
this._paused = false
if (this.tus) {
this.tus.start()
}
})
emitter().on(`cancel:${this.token}`, () => {
this._paused = true
if (this.tus) {
const shouldTerminate = !!this.tus.url
this.tus.abort(shouldTerminate).catch(() => {})
}
this.cleanUp()
})
}
}
/**
* returns a substring of the token. Used as traceId for logging
* we avoid using the entire token because this is meant to be a short term
* access token between uppy client and companion websocket
*
* @param {string} token the token to Shorten
* @returns {string}
*/
static shortenToken (token) {
return token.substring(0, 8)
}
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,
useFormData,
size,
fieldname: req.body.fieldname,
pathPrefix: `${req.companion.options.filePath}`,
storage: redis.client(),
s3: req.companion.s3Client ? {
client: req.companion.s3Client,
options: req.companion.options.providerOptions.s3,
} : null,
headers: req.body.headers,
chunkSize: req.companion.options.chunkSize,
}
}
/**
* the number of bytes written into the streams
*/
get bytesWritten () {
return this.writeStream.bytesWritten
}
/**
* Validate the options passed down to the uplaoder
*
* @param {UploaderOptions} options
* @returns {boolean}
*/
validateOptions (options) {
// validate HTTP Method
if (options.httpMethod) {
if (typeof options.httpMethod !== 'string') {
this._errRespMessage = 'unsupported HTTP METHOD specified'
return false
}
const method = options.httpMethod.toLowerCase()
if (method !== 'put' && method !== 'post') {
this._errRespMessage = 'unsupported HTTP METHOD specified'
return false
}
}
// validate fieldname
if (options.fieldname && typeof options.fieldname !== 'string') {
this._errRespMessage = 'fieldname must be a string'
return false
}
// validate metadata
if (options.metadata && !isObject(options.metadata)) {
this._errRespMessage = 'metadata must be an object'
return false
}
// validate headers
if (options.headers && !isObject(options.headers)) {
this._errRespMessage = 'headers must be an object'
return false
}
// validate protocol
// @todo this validation should not be conditional once the protocol field is mandatory
if (options.protocol && !Object.keys(PROTOCOLS).some((key) => PROTOCOLS[key] === options.protocol)) {
this._errRespMessage = 'unsupported protocol specified'
return false
}
// s3 uploads don't require upload destination
// validation, because the destination is determined
// by the server's s3 config
if (options.protocol === PROTOCOLS.s3Multipart) {
return true
}
if (!options.endpoint && !options.uploadUrl) {
this._errRespMessage = 'no destination specified'
return false
}
if (options.chunkSize != null && typeof options.chunkSize !== 'number') {
this._errRespMessage = 'incorrect chunkSize'
return false
}
const validatorOpts = { require_protocol: true, require_tld: false }
return [options.endpoint, options.uploadUrl].every((url) => {
if (url && !validator.isURL(url, validatorOpts)) {
this._errRespMessage = 'invalid destination url'
return false
}
const allowedUrls = options.companionOptions.uploadUrls
if (allowedUrls && url && !hasMatch(url, allowedUrls)) {
this._errRespMessage = 'upload destination does not match any allowed destinations'
return false
}
return true
})
}
hasError () {
return this._errRespMessage != null
}
/**
* returns a substring of the token. Used as traceId for logging
* we avoid using the entire token because this is meant to be a short term
* access token between uppy client and companion websocket
*/
get shortToken () {
return Uploader.shortenToken(this.token)
}
/**
*
* @param {Function} callback
*/
onSocketReady (callback) {
emitter().once(`connection:${this.token}`, () => callback())
logger.debug('waiting for connection', 'uploader.socket.wait', this.shortToken)
}
cleanUp () {
fs.unlink(this.path, (err) => {
if (err) {
logger.error(`cleanup failed for: ${this.path} err: ${err}`, 'uploader.cleanup.error')
}
})
emitter().removeAllListeners(`pause:${this.token}`)
emitter().removeAllListeners(`resume:${this.token}`)
emitter().removeAllListeners(`cancel:${this.token}`)
this.uploadStopped = true
}
/**
*
* @param {Error} err
* @param {string | Buffer | Buffer[]} chunk
*/
handleChunk (err, chunk) {
if (this.uploadStopped) {
return
}
if (err) {
logger.error(err, 'uploader.download.error', this.shortToken)
this.emitError(err)
this.cleanUp()
return
}
// @todo a default protocol should not be set. We should ensure that the user specifies their protocol.
const protocol = this.options.protocol || PROTOCOLS.multipart
// The download has completed; close the file and start an upload if necessary.
if (chunk === null) {
this.writeStream.on('finish', () => {
this.streamsEnded = true
switch (protocol) {
case PROTOCOLS.multipart:
if (this.options.endpoint) {
this.uploadMultipart()
}
break
case PROTOCOLS.s3Multipart:
if (!this.s3Upload) {
this.uploadS3Multipart()
} else {
logger.warn('handleChunk() called multiple times', 'uploader.s3.duplicate', this.shortToken)
}
break
case PROTOCOLS.tus:
if (!this.tus) {
this.uploadTus()
} else {
logger.warn('handleChunk() called multiple times', 'uploader.tus.duplicate', this.shortToken)
}
break
}
})
return this.endStreams()
}
this.writeStream.write(chunk, () => {
logger.debug(`${this.bytesWritten} bytes`, 'uploader.download.progress', this.shortToken)
return this.emitIllusiveProgress()
})
}
endStreams () {
this.writeStream.end()
}
getResponse () {
if (this._errRespMessage) {
return { body: { message: this._errRespMessage }, status: 400 }
}
return { body: { token: this.token }, status: 200 }
}
/**
* @typedef {{action: string, payload: object}} State
* @param {State} state
*/
saveState (state) {
if (!this.storage) return
this.storage.set(`${Uploader.STORAGE_PREFIX}:${this.token}`, jsonStringify(state))
}
/**
* This method emits upload progress but also creates an "upload progress" illusion
* for the waiting period while only download is happening. Hence, it combines both
* download and upload into an upload progress.
*
* @see emitProgress
* @param {number=} bytesUploaded the bytes actually Uploaded so far
*/
emitIllusiveProgress (bytesUploaded = 0) {
if (this._paused) {
return
}
let bytesTotal = this.streamsEnded ? this.bytesWritten : this.options.size
if (!this.streamsEnded) {
bytesTotal = Math.max(bytesTotal, this.bytesWritten)
}
// for a 10MB file, 10MB of download will account for 5MB upload progress
// and 10MB of actual upload will account for the other 5MB upload progress.
const illusiveBytesUploaded = (this.bytesWritten / 2) + (bytesUploaded / 2)
logger.debug(
`${bytesUploaded} ${illusiveBytesUploaded} ${bytesTotal}`,
'uploader.illusive.progress',
this.shortToken
)
this.emitProgress(illusiveBytesUploaded, bytesTotal)
}
/**
*
* @param {number} bytesUploaded
* @param {number | null} bytesTotal
*/
emitProgress (bytesUploaded, bytesTotal) {
bytesTotal = bytesTotal || this.options.size
if (this.tus && this.tus.options.uploadLengthDeferred && this.streamsEnded) {
bytesTotal = this.bytesWritten
}
const percentage = (bytesUploaded / bytesTotal * 100)
const formatPercentage = percentage.toFixed(2)
logger.debug(
`${bytesUploaded} ${bytesTotal} ${formatPercentage}%`,
'uploader.upload.progress',
this.shortToken
)
const dataToEmit = {
action: 'progress',
payload: { progress: formatPercentage, bytesUploaded, bytesTotal },
}
this.saveState(dataToEmit)
// avoid flooding the client with progress events.
const roundedPercentage = Math.floor(percentage)
if (this.emittedProgress !== roundedPercentage) {
this.emittedProgress = roundedPercentage
emitter().emit(this.token, dataToEmit)
}
}
/**
*
* @param {string} url
* @param {object} extraData
*/
emitSuccess (url, extraData = {}) {
const emitData = {
action: 'success',
payload: Object.assign(extraData, { complete: true, url }),
}
this.saveState(emitData)
emitter().emit(this.token, emitData)
}
/**
*
* @param {Error} err
* @param {object=} extraData
*/
emitError (err, extraData = {}) {
const serializedErr = serializeError(err)
// delete stack to avoid sending server info to client
delete serializedErr.stack
const dataToEmit = {
action: 'error',
payload: Object.assign(extraData, { error: serializedErr }),
}
this.saveState(dataToEmit)
emitter().emit(this.token, dataToEmit)
}
/**
* start the tus upload
*/
uploadTus () {
const file = fs.createReadStream(this.path)
const uploader = this
this.tus = new tus.Upload(file, {
endpoint: this.options.endpoint,
uploadUrl: this.options.uploadUrl,
uploadLengthDeferred: false,
retryDelays: [0, 1000, 3000, 5000],
uploadSize: this.bytesWritten,
chunkSize: this.options.chunkSize || Infinity,
headers: headerSanitize(this.options.headers),
addRequestId: true,
metadata: {
// file name and type as required by the tusd tus server
// https://github.com/tus/tusd/blob/5b376141903c1fd64480c06dde3dfe61d191e53d/unrouted_handler.go#L614-L646
filename: this.uploadFileName,
filetype: this.options.metadata.type,
...this.options.metadata,
},
/**
*
* @param {Error} error
*/
onError (error) {
logger.error(error, 'uploader.tus.error')
// deleting tus originalRequest field because it uses the same http-agent
// as companion, and this agent may contain sensitive request details (e.g headers)
// previously made to providers. Deleting the field would prevent it from getting leaked
// to the frontend etc.
// @ts-ignore
delete error.originalRequest
// @ts-ignore
delete error.originalResponse
uploader.emitError(error)
},
/**
*
* @param {number} bytesUploaded
* @param {number} bytesTotal
*/
onProgress (bytesUploaded, bytesTotal) { // eslint-disable-line no-unused-vars
uploader.emitIllusiveProgress(bytesUploaded)
},
onSuccess () {
uploader.emitSuccess(uploader.tus.url)
uploader.cleanUp()
},
})
if (!this._paused) {
this.tus.start()
}
}
uploadMultipart () {
const file = fs.createReadStream(this.path)
// upload progress
let bytesUploaded = 0
file.on('data', (data) => {
bytesUploaded += data.length
this.emitIllusiveProgress(bytesUploaded)
})
const httpMethod = (this.options.httpMethod || '').toLowerCase() === 'put' ? 'put' : 'post'
const headers = headerSanitize(this.options.headers)
const reqOptions = { url: this.options.endpoint, headers, encoding: null }
const httpRequest = request[httpMethod]
if (this.options.useFormData) {
reqOptions.formData = {
...this.options.metadata,
[this.options.fieldname]: {
value: file,
options: {
filename: this.uploadFileName,
contentType: this.options.metadata.type,
},
},
}
httpRequest(reqOptions, (error, response, body) => {
this._onMultipartComplete(error, response, body, bytesUploaded)
})
} else {
reqOptions.headers['content-length'] = this.bytesWritten
reqOptions.body = file
httpRequest(reqOptions, (error, response, body) => {
this._onMultipartComplete(error, response, body, bytesUploaded)
})
}
}
_onMultipartComplete (error, response, body, bytesUploaded) {
if (error) {
logger.error(error, 'upload.multipart.error')
this.emitError(error)
return
}
const { headers } = response
// 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, bytesUploaded })
}
this.cleanUp()
}
/**
* Upload the file to S3 using a Multipart upload.
*/
uploadS3Multipart () {
const file = fs.createReadStream(this.path)
return this._uploadS3MultipartStream(file)
}
/**
* Upload a stream to S3.
*/
_uploadS3MultipartStream (stream) {
if (!this.options.s3) {
this.emitError(new Error('The S3 client is not configured on this companion instance.'))
return
}
const filename = this.options.metadata.name || path.basename(this.path)
const { client, options } = this.options.s3
const upload = client.upload({
Bucket: options.bucket,
Key: options.getKey(null, filename, this.options.metadata),
ACL: options.acl,
ContentType: this.options.metadata.type,
Metadata: this.options.metadata,
Body: stream,
})
this.s3Upload = upload
upload.on('httpUploadProgress', ({ loaded, total }) => {
this.emitProgress(loaded, total)
})
upload.send((error, data) => {
this.s3Upload = null
if (error) {
this.emitError(error)
} else {
const url = data && data.Location ? data.Location : null
this.emitSuccess(url, {
response: {
responseText: JSON.stringify(data),
headers: {
'content-type': 'application/json',
},
},
})
}
this.cleanUp()
})
}
}
Uploader.FILE_NAME_PREFIX = 'uppy-file'
Uploader.STORAGE_PREFIX = 'companion'
module.exports = Uploader