Uploader.js 17 KB

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