Uploader.js 18 KB

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