Uploader.js 23 KB

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