Uploader.js 18 KB

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