Skip to content

Commit

Permalink
fix: Catch errors when uploading files (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio committed Apr 1, 2024
1 parent 97de8e3 commit b704157
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/S3/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,32 @@ class Storage implements multer.StorageEngine {
)
}

// The upload key will be a unique UUID first so it can be later renamed (copy + delete in S3) or deleted to the file key we obtained.
// We do it this way because we want to avoid having files in memory for each upload, but we need to read the file stream to hash it.
// That means that we cant hash first and upload later, we need to do it on the same stream read
const id = uuid()
try {
// The upload key will be a unique UUID first so it can be later renamed (copy + delete in S3) or deleted to the file key we obtained.
// We do it this way because we want to avoid having files in memory for each upload, but we need to read the file stream to hash it.
// That means that we cant hash first and upload later, we need to do it on the same stream read
const id = uuid()

// The original stream comes from file.stream. The reading of the stream is kicked of when we pipe it into the first PassThrough
// This allows for both operations, the upload and the file key generation to be done "simultaneously" without keeping the entire stream data in memory
// The original stream comes from file.stream. The reading of the stream is kicked of when we pipe it into the first PassThrough
// This allows for both operations, the upload and the file key generation to be done "simultaneously" without keeping the entire stream data in memory

const uploadStream = file.stream.pipe(new PassThrough())
const uploadStream = file.stream.pipe(new PassThrough())

const [key] = await Promise.all([
this.getKey(file, req),
uploadFile(id, uploadStream, ACL.publicRead, {
ContentType: file.mimetype,
}),
])
const [key] = await Promise.all([
this.getKey(file, req),
uploadFile(id, uploadStream, ACL.publicRead, {
ContentType: file.mimetype,
}),
])

// move file to key
await moveFile(id, key)

callback(null, {
fieldname: key,
})
// move file to key
await moveFile(id, key)
callback(null, {
fieldname: key,
})
} catch (error) {
callback(error)
}
}

async _removeFile(
Expand Down

0 comments on commit b704157

Please sign in to comment.