Uploader.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. // eslint-disable-next-line max-classes-per-file
  2. const tus = require('tus-js-client')
  3. const { randomUUID } = require('node:crypto')
  4. const validator = require('validator')
  5. const got = require('got').default
  6. const { pipeline: pipelineCb } = require('node:stream')
  7. const { join } = require('node:path')
  8. const fs = require('node:fs')
  9. const { promisify } = require('node:util')
  10. const FormData = require('form-data')
  11. const throttle = require('lodash/throttle')
  12. const { Upload } = require('@aws-sdk/lib-storage')
  13. // TODO move to `require('streams/promises').pipeline` when dropping support for Node.js 14.x.
  14. const pipeline = promisify(pipelineCb)
  15. const { createReadStream, createWriteStream, ReadStream } = fs
  16. const { stat, unlink } = fs.promises
  17. /** @type {any} */
  18. // @ts-ignore - typescript resolves this this to a hoisted version of
  19. // serialize-error that ships with a declaration file, we are using a version
  20. // here that does not have a declaration file
  21. const serializeError = require('serialize-error')
  22. const emitter = require('./emitter')
  23. const { jsonStringify, hasMatch } = require('./helpers/utils')
  24. const logger = require('./logger')
  25. const headerSanitize = require('./header-blacklist')
  26. const redis = require('./redis')
  27. // Need to limit length or we can get
  28. // "MetadataTooLarge: Your metadata headers exceed the maximum allowed metadata size" in tus / S3
  29. const MAX_FILENAME_LENGTH = 500
  30. const DEFAULT_FIELD_NAME = 'files[]'
  31. const PROTOCOLS = Object.freeze({
  32. multipart: 'multipart',
  33. s3Multipart: 's3-multipart',
  34. tus: 'tus',
  35. })
  36. function exceedsMaxFileSize (maxFileSize, size) {
  37. return maxFileSize && size && size > maxFileSize
  38. }
  39. // TODO remove once we migrate away from form-data
  40. function sanitizeMetadata (inputMetadata) {
  41. if (inputMetadata == null) return {}
  42. const outputMetadata = {}
  43. Object.keys(inputMetadata).forEach((key) => {
  44. outputMetadata[key] = String(inputMetadata[key])
  45. })
  46. return outputMetadata
  47. }
  48. class AbortError extends Error {}
  49. class ValidationError extends Error {}
  50. /**
  51. * Validate the options passed down to the uplaoder
  52. *
  53. * @param {UploaderOptions} options
  54. */
  55. function validateOptions (options) {
  56. // validate HTTP Method
  57. if (options.httpMethod) {
  58. if (typeof options.httpMethod !== 'string') {
  59. throw new ValidationError('unsupported HTTP METHOD specified')
  60. }
  61. const method = options.httpMethod.toLowerCase()
  62. if (method !== 'put' && method !== 'post') {
  63. throw new ValidationError('unsupported HTTP METHOD specified')
  64. }
  65. }
  66. if (exceedsMaxFileSize(options.companionOptions.maxFileSize, options.size)) {
  67. throw new ValidationError('maxFileSize exceeded')
  68. }
  69. // validate fieldname
  70. if (options.fieldname != null && typeof options.fieldname !== 'string') {
  71. throw new ValidationError('fieldname must be a string')
  72. }
  73. // validate metadata
  74. if (options.metadata != null && typeof options.metadata !== 'object') {
  75. throw new ValidationError('metadata must be an object')
  76. }
  77. // validate headers
  78. if (options.headers != null && typeof options.headers !== 'object') {
  79. throw new ValidationError('headers must be an object')
  80. }
  81. // validate protocol
  82. // @todo this validation should not be conditional once the protocol field is mandatory
  83. if (options.protocol && !Object.keys(PROTOCOLS).some((key) => PROTOCOLS[key] === options.protocol)) {
  84. throw new ValidationError('unsupported protocol specified')
  85. }
  86. // s3 uploads don't require upload destination
  87. // validation, because the destination is determined
  88. // by the server's s3 config
  89. if (options.protocol !== PROTOCOLS.s3Multipart) {
  90. if (!options.endpoint && !options.uploadUrl) {
  91. throw new ValidationError('no destination specified')
  92. }
  93. const validateUrl = (url) => {
  94. const validatorOpts = { require_protocol: true, require_tld: false }
  95. if (url && !validator.isURL(url, validatorOpts)) {
  96. throw new ValidationError('invalid destination url')
  97. }
  98. const allowedUrls = options.companionOptions.uploadUrls
  99. if (allowedUrls && url && !hasMatch(url, allowedUrls)) {
  100. throw new ValidationError('upload destination does not match any allowed destinations')
  101. }
  102. }
  103. [options.endpoint, options.uploadUrl].forEach(validateUrl)
  104. }
  105. if (options.chunkSize != null && typeof options.chunkSize !== 'number') {
  106. throw new ValidationError('incorrect chunkSize')
  107. }
  108. }
  109. class Uploader {
  110. /**
  111. * Uploads file to destination based on the supplied protocol (tus, s3-multipart, multipart)
  112. * For tus uploads, the deferredLength option is enabled, because file size value can be unreliable
  113. * for some providers (Instagram particularly)
  114. *
  115. * @typedef {object} UploaderOptions
  116. * @property {string} endpoint
  117. * @property {string} [uploadUrl]
  118. * @property {string} protocol
  119. * @property {number} [size]
  120. * @property {string} [fieldname]
  121. * @property {string} pathPrefix
  122. * @property {any} [s3]
  123. * @property {any} metadata
  124. * @property {any} companionOptions
  125. * @property {any} [storage]
  126. * @property {any} [headers]
  127. * @property {string} [httpMethod]
  128. * @property {boolean} [useFormData]
  129. * @property {number} [chunkSize]
  130. *
  131. * @param {UploaderOptions} options
  132. */
  133. constructor (options) {
  134. validateOptions(options)
  135. this.options = options
  136. this.token = randomUUID()
  137. this.fileName = `${Uploader.FILE_NAME_PREFIX}-${this.token}`
  138. this.options.metadata = sanitizeMetadata(this.options.metadata)
  139. this.options.fieldname = this.options.fieldname || DEFAULT_FIELD_NAME
  140. this.size = options.size
  141. this.uploadFileName = this.options.metadata.name
  142. ? this.options.metadata.name.substring(0, MAX_FILENAME_LENGTH)
  143. : this.fileName
  144. this.uploadStopped = false
  145. this.storage = options.storage
  146. this._paused = false
  147. this.downloadedBytes = 0
  148. this.readStream = null
  149. if (this.options.protocol === PROTOCOLS.tus) {
  150. emitter().on(`pause:${this.token}`, () => {
  151. logger.debug('Received from client: pause', 'uploader', this.shortToken)
  152. this._paused = true
  153. if (this.tus) {
  154. this.tus.abort()
  155. }
  156. })
  157. emitter().on(`resume:${this.token}`, () => {
  158. logger.debug('Received from client: resume', 'uploader', this.shortToken)
  159. this._paused = false
  160. if (this.tus) {
  161. this.tus.start()
  162. }
  163. })
  164. }
  165. emitter().on(`cancel:${this.token}`, () => {
  166. logger.debug('Received from client: cancel', 'uploader', this.shortToken)
  167. this._paused = true
  168. if (this.tus) {
  169. const shouldTerminate = !!this.tus.url
  170. this.tus.abort(shouldTerminate).catch(() => {})
  171. }
  172. this.abortReadStream(new AbortError())
  173. })
  174. }
  175. abortReadStream (err) {
  176. this.uploadStopped = true
  177. if (this.readStream) this.readStream.destroy(err)
  178. }
  179. async _uploadByProtocol () {
  180. // todo a default protocol should not be set. We should ensure that the user specifies their protocol.
  181. // after we drop old versions of uppy client we can remove this
  182. const protocol = this.options.protocol || PROTOCOLS.multipart
  183. switch (protocol) {
  184. case PROTOCOLS.multipart:
  185. return this._uploadMultipart(this.readStream)
  186. case PROTOCOLS.s3Multipart:
  187. return this._uploadS3Multipart(this.readStream)
  188. case PROTOCOLS.tus:
  189. return this._uploadTus(this.readStream)
  190. default:
  191. throw new Error('Invalid protocol')
  192. }
  193. }
  194. async _downloadStreamAsFile (stream) {
  195. this.tmpPath = join(this.options.pathPrefix, this.fileName)
  196. logger.debug('fully downloading file', 'uploader.download', this.shortToken)
  197. const writeStream = createWriteStream(this.tmpPath)
  198. const onData = (chunk) => {
  199. this.downloadedBytes += chunk.length
  200. if (exceedsMaxFileSize(this.options.companionOptions.maxFileSize, this.downloadedBytes)) this.abortReadStream(new Error('maxFileSize exceeded'))
  201. this.onProgress(0, undefined)
  202. }
  203. stream.on('data', onData)
  204. await pipeline(stream, writeStream)
  205. logger.debug('finished fully downloading file', 'uploader.download', this.shortToken)
  206. const { size } = await stat(this.tmpPath)
  207. this.size = size
  208. const fileStream = createReadStream(this.tmpPath)
  209. this.readStream = fileStream
  210. }
  211. _needDownloadFirst () {
  212. return !this.options.size || !this.options.companionOptions.streamingUpload
  213. }
  214. /**
  215. *
  216. * @param {import('stream').Readable} stream
  217. */
  218. async uploadStream (stream) {
  219. try {
  220. if (this.uploadStopped) throw new Error('Cannot upload stream after upload stopped')
  221. if (this.readStream) throw new Error('Already uploading')
  222. this.readStream = stream
  223. if (this._needDownloadFirst()) {
  224. logger.debug('need to download the whole file first', 'controller.get.provider.size', this.shortToken)
  225. // Some streams need to be downloaded entirely first, because we don't know their size from the provider
  226. // This is true for zoom and drive (exported files) or some URL downloads.
  227. // The stream will then typically come from a "Transfer-Encoding: chunked" response
  228. await this._downloadStreamAsFile(this.readStream)
  229. }
  230. if (this.uploadStopped) return undefined
  231. const { url, extraData } = await Promise.race([
  232. this._uploadByProtocol(),
  233. // If we don't handle stream errors, we get unhandled error in node.
  234. new Promise((resolve, reject) => this.readStream.on('error', reject)),
  235. ])
  236. return { url, extraData }
  237. } finally {
  238. logger.debug('cleanup', this.shortToken)
  239. if (this.readStream && !this.readStream.destroyed) this.readStream.destroy()
  240. await this.tryDeleteTmpPath()
  241. }
  242. }
  243. tryDeleteTmpPath () {
  244. if (this.tmpPath) unlink(this.tmpPath).catch(() => {})
  245. }
  246. /**
  247. *
  248. * @param {import('stream').Readable} stream
  249. */
  250. async tryUploadStream (stream) {
  251. try {
  252. emitter().emit('upload-start', { token: this.token })
  253. const ret = await this.uploadStream(stream)
  254. if (!ret) return
  255. const { url, extraData } = ret
  256. this.#emitSuccess(url, extraData)
  257. } catch (err) {
  258. if (err instanceof AbortError) {
  259. logger.error('Aborted upload', 'uploader.aborted', this.shortToken)
  260. return
  261. }
  262. // console.log(err)
  263. logger.error(err, 'uploader.error', this.shortToken)
  264. this.#emitError(err)
  265. } finally {
  266. emitter().removeAllListeners(`pause:${this.token}`)
  267. emitter().removeAllListeners(`resume:${this.token}`)
  268. emitter().removeAllListeners(`cancel:${this.token}`)
  269. }
  270. }
  271. /**
  272. * returns a substring of the token. Used as traceId for logging
  273. * we avoid using the entire token because this is meant to be a short term
  274. * access token between uppy client and companion websocket
  275. *
  276. * @param {string} token the token to Shorten
  277. * @returns {string}
  278. */
  279. static shortenToken (token) {
  280. return token.substring(0, 8)
  281. }
  282. static reqToOptions (req, size) {
  283. const useFormDataIsSet = Object.prototype.hasOwnProperty.call(req.body, 'useFormData')
  284. const useFormData = useFormDataIsSet ? req.body.useFormData : true
  285. return {
  286. // Client provided info (must be validated and not blindly trusted):
  287. headers: req.body.headers,
  288. httpMethod: req.body.httpMethod,
  289. protocol: req.body.protocol,
  290. endpoint: req.body.endpoint,
  291. uploadUrl: req.body.uploadUrl,
  292. metadata: req.body.metadata,
  293. fieldname: req.body.fieldname,
  294. useFormData,
  295. // Info coming from companion server configuration:
  296. size,
  297. companionOptions: req.companion.options,
  298. pathPrefix: `${req.companion.options.filePath}`,
  299. storage: redis.client(),
  300. s3: req.companion.s3Client ? {
  301. client: req.companion.s3Client,
  302. options: req.companion.options.s3,
  303. } : null,
  304. chunkSize: req.companion.options.chunkSize,
  305. }
  306. }
  307. /**
  308. * returns a substring of the token. Used as traceId for logging
  309. * we avoid using the entire token because this is meant to be a short term
  310. * access token between uppy client and companion websocket
  311. */
  312. get shortToken () {
  313. return Uploader.shortenToken(this.token)
  314. }
  315. async awaitReady (timeout) {
  316. logger.debug('waiting for socket connection', 'uploader.socket.wait', this.shortToken)
  317. // TODO: replace the Promise constructor call when dropping support for Node.js <16 with
  318. // await once(emitter, eventName, timeout && { signal: AbortSignal.timeout(timeout) })
  319. await new Promise((resolve, reject) => {
  320. const eventName = `connection:${this.token}`
  321. let timer
  322. let onEvent
  323. function cleanup () {
  324. emitter().removeListener(eventName, onEvent)
  325. clearTimeout(timer)
  326. }
  327. if (timeout) {
  328. // Need to timeout after a while, or we could leak emitters
  329. timer = setTimeout(() => {
  330. cleanup()
  331. reject(new Error('Timed out waiting for socket connection'))
  332. }, timeout)
  333. }
  334. onEvent = () => {
  335. cleanup()
  336. resolve()
  337. }
  338. emitter().once(eventName, onEvent)
  339. })
  340. logger.debug('socket connection received', 'uploader.socket.wait', this.shortToken)
  341. }
  342. /**
  343. * @typedef {{action: string, payload: object}} State
  344. * @param {State} state
  345. */
  346. saveState (state) {
  347. if (!this.storage) return
  348. // make sure the keys get cleaned up.
  349. // https://github.com/transloadit/uppy/issues/3748
  350. const keyExpirySec = 60 * 60 * 24
  351. const redisKey = `${Uploader.STORAGE_PREFIX}:${this.token}`
  352. this.storage.set(redisKey, jsonStringify(state), 'EX', keyExpirySec)
  353. }
  354. throttledEmitProgress = throttle((dataToEmit) => {
  355. const { bytesUploaded, bytesTotal, progress } = dataToEmit.payload
  356. logger.debug(
  357. `${bytesUploaded} ${bytesTotal} ${progress}%`,
  358. 'uploader.total.progress',
  359. this.shortToken,
  360. )
  361. this.saveState(dataToEmit)
  362. emitter().emit(this.token, dataToEmit)
  363. }, 1000, { trailing: false })
  364. /**
  365. *
  366. * @param {number} [bytesUploaded]
  367. * @param {number | null} [bytesTotalIn]
  368. */
  369. onProgress (bytesUploaded = 0, bytesTotalIn = 0) {
  370. const bytesTotal = bytesTotalIn || this.size || 0
  371. // If fully downloading before uploading, combine downloaded and uploaded bytes
  372. // This will make sure that the user sees half of the progress before upload starts (while downloading)
  373. let combinedBytes = bytesUploaded
  374. if (this._needDownloadFirst()) {
  375. combinedBytes = Math.floor((combinedBytes + (this.downloadedBytes || 0)) / 2)
  376. }
  377. // Prevent divide by zero
  378. let percentage = 0
  379. if (bytesTotal > 0) percentage = Math.min(Math.max(0, ((combinedBytes / bytesTotal) * 100)), 100)
  380. const formattedPercentage = percentage.toFixed(2)
  381. if (this._paused || this.uploadStopped) {
  382. return
  383. }
  384. const payload = { progress: formattedPercentage, bytesUploaded: combinedBytes, bytesTotal }
  385. const dataToEmit = {
  386. action: 'progress',
  387. payload,
  388. }
  389. // avoid flooding the client (and log) with progress events.
  390. // flooding will cause reduced performance and possibly network issues
  391. this.throttledEmitProgress(dataToEmit)
  392. }
  393. /**
  394. *
  395. * @param {string} url
  396. * @param {object} extraData
  397. */
  398. #emitSuccess (url, extraData) {
  399. const emitData = {
  400. action: 'success',
  401. payload: { ...extraData, complete: true, url },
  402. }
  403. this.saveState(emitData)
  404. emitter().emit(this.token, emitData)
  405. }
  406. /**
  407. *
  408. * @param {Error} err
  409. */
  410. #emitError (err) {
  411. // delete stack to avoid sending server info to client
  412. // todo remove also extraData from serializedErr in next major,
  413. // see PR discussion https://github.com/transloadit/uppy/pull/3832
  414. const { stack, ...serializedErr } = serializeError(err)
  415. const dataToEmit = {
  416. action: 'error',
  417. // @ts-ignore
  418. payload: { ...err.extraData, error: serializedErr },
  419. }
  420. this.saveState(dataToEmit)
  421. emitter().emit(this.token, dataToEmit)
  422. }
  423. /**
  424. * start the tus upload
  425. *
  426. * @param {any} stream
  427. */
  428. async _uploadTus (stream) {
  429. const uploader = this
  430. const isFileStream = stream instanceof ReadStream
  431. // chunkSize needs to be a finite value if the stream is not a file stream (fs.createReadStream)
  432. // https://github.com/tus/tus-js-client/blob/4479b78032937ac14da9b0542e489ac6fe7e0bc7/lib/node/fileReader.js#L50
  433. const chunkSize = this.options.chunkSize || (isFileStream ? Infinity : 50e6)
  434. return new Promise((resolve, reject) => {
  435. this.tus = new tus.Upload(stream, {
  436. endpoint: this.options.endpoint,
  437. uploadUrl: this.options.uploadUrl,
  438. uploadLengthDeferred: false,
  439. retryDelays: [0, 1000, 3000, 5000],
  440. uploadSize: this.size,
  441. chunkSize,
  442. headers: headerSanitize(this.options.headers),
  443. addRequestId: true,
  444. metadata: {
  445. // file name and type as required by the tusd tus server
  446. // https://github.com/tus/tusd/blob/5b376141903c1fd64480c06dde3dfe61d191e53d/unrouted_handler.go#L614-L646
  447. filename: this.uploadFileName,
  448. filetype: this.options.metadata.type,
  449. ...this.options.metadata,
  450. },
  451. /**
  452. *
  453. * @param {Error} error
  454. */
  455. onError (error) {
  456. logger.error(error, 'uploader.tus.error')
  457. // deleting tus originalRequest field because it uses the same http-agent
  458. // as companion, and this agent may contain sensitive request details (e.g headers)
  459. // previously made to providers. Deleting the field would prevent it from getting leaked
  460. // to the frontend etc.
  461. // @ts-ignore
  462. // eslint-disable-next-line no-param-reassign
  463. delete error.originalRequest
  464. // @ts-ignore
  465. // eslint-disable-next-line no-param-reassign
  466. delete error.originalResponse
  467. reject(error)
  468. },
  469. /**
  470. *
  471. * @param {number} [bytesUploaded]
  472. * @param {number} [bytesTotal]
  473. */
  474. onProgress (bytesUploaded, bytesTotal) {
  475. uploader.onProgress(bytesUploaded, bytesTotal)
  476. },
  477. onSuccess () {
  478. resolve({ url: uploader.tus.url })
  479. },
  480. })
  481. if (!this._paused) {
  482. this.tus.start()
  483. }
  484. })
  485. }
  486. async _uploadMultipart (stream) {
  487. if (!this.options.endpoint) {
  488. throw new Error('No multipart endpoint set')
  489. }
  490. function getRespObj (response) {
  491. // remove browser forbidden headers
  492. const { 'set-cookie': deleted, 'set-cookie2': deleted2, ...responseHeaders } = response.headers
  493. return {
  494. responseText: response.body,
  495. status: response.statusCode,
  496. statusText: response.statusMessage,
  497. headers: responseHeaders,
  498. }
  499. }
  500. // upload progress
  501. let bytesUploaded = 0
  502. stream.on('data', (data) => {
  503. bytesUploaded += data.length
  504. this.onProgress(bytesUploaded, undefined)
  505. })
  506. const url = this.options.endpoint
  507. const reqOptions = {
  508. headers: headerSanitize(this.options.headers),
  509. }
  510. if (this.options.useFormData) {
  511. // todo refactor once upgraded to got 12
  512. const formData = new FormData()
  513. Object.entries(this.options.metadata).forEach(([key, value]) => formData.append(key, value))
  514. formData.append(this.options.fieldname, stream, {
  515. filename: this.uploadFileName,
  516. contentType: this.options.metadata.type,
  517. knownLength: this.size,
  518. })
  519. reqOptions.body = formData
  520. } else {
  521. reqOptions.headers['content-length'] = this.size
  522. reqOptions.body = stream
  523. }
  524. try {
  525. const httpMethod = (this.options.httpMethod || '').toLowerCase() === 'put' ? 'put' : 'post'
  526. const runRequest = got[httpMethod]
  527. const response = await runRequest(url, reqOptions)
  528. if (bytesUploaded !== this.size) {
  529. const errMsg = `uploaded only ${bytesUploaded} of ${this.size} with status: ${response.statusCode}`
  530. logger.error(errMsg, 'upload.multipart.mismatch.error')
  531. throw new Error(errMsg)
  532. }
  533. return {
  534. url: null,
  535. extraData: { response: getRespObj(response), bytesUploaded },
  536. }
  537. } catch (err) {
  538. logger.error(err, 'upload.multipart.error')
  539. const statusCode = err.response?.statusCode
  540. if (statusCode != null) {
  541. throw Object.assign(new Error(err.statusMessage), {
  542. extraData: getRespObj(err.response),
  543. })
  544. }
  545. throw new Error('Unknown multipart upload error')
  546. }
  547. }
  548. /**
  549. * Upload the file to S3 using a Multipart upload.
  550. */
  551. async _uploadS3Multipart (stream) {
  552. if (!this.options.s3) {
  553. throw new Error('The S3 client is not configured on this companion instance.')
  554. }
  555. const filename = this.uploadFileName
  556. /**
  557. * @type {{client: import('@aws-sdk/client-s3').S3Client, options: Record<string, any>}}
  558. */
  559. const s3Options = this.options.s3
  560. const { client, options } = s3Options
  561. const params = {
  562. Bucket: options.bucket,
  563. Key: options.getKey(null, filename, this.options.metadata),
  564. ContentType: this.options.metadata.type,
  565. Metadata: this.options.metadata,
  566. Body: stream,
  567. }
  568. if (options.acl != null) params.ACL = options.acl
  569. const upload = new Upload({
  570. client,
  571. params,
  572. // using chunkSize as partSize too, see https://github.com/transloadit/uppy/pull/3511
  573. partSize: this.options.chunkSize,
  574. leavePartsOnError: true, // https://github.com/aws/aws-sdk-js-v3/issues/2311
  575. })
  576. upload.on('httpUploadProgress', ({ loaded, total }) => {
  577. this.onProgress(loaded, total)
  578. })
  579. const data = await upload.done()
  580. return {
  581. // @ts-expect-error For some reason `|| null` is not enough for TS
  582. url: data?.Location || null,
  583. extraData: {
  584. response: {
  585. responseText: JSON.stringify(data),
  586. headers: {
  587. 'content-type': 'application/json',
  588. },
  589. },
  590. },
  591. }
  592. }
  593. }
  594. Uploader.FILE_NAME_PREFIX = 'uppy-file'
  595. Uploader.STORAGE_PREFIX = 'companion'
  596. module.exports = Uploader
  597. module.exports.ValidationError = ValidationError