From 8487089209c78b06d92155e94a15120f9882600b Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Mon, 25 Sep 2023 10:28:49 -0600 Subject: [PATCH] feat: probably improved single object S3 upload --- backend/routers/index.ts | 2 +- backend/s3.ts | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/routers/index.ts b/backend/routers/index.ts index 2ea265f..123591f 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -57,7 +57,7 @@ router.post('/', async (req, res) => { // * Move the file if (!s3) await fs.move(bbFile.file, destination); - else await uploadFileS3(await fs.readFile(bbFile.file), bbFile.mimetype, fileKey); + else await uploadFileS3(await fs.readFile(bbFile.file), fileKey, bbFile.mimetype, size, sha256); // Build ass metadata const assFile: AssFile = { diff --git a/backend/s3.ts b/backend/s3.ts index 6f4f59f..74b265b 100644 --- a/backend/s3.ts +++ b/backend/s3.ts @@ -53,12 +53,14 @@ const s3 = (): S3Client | null => { /** * Basic single file upload */ -const doObjectUpload = (file: Buffer, mimetype: string, fileKey: string): Promise => +const doObjectUpload = (file: Buffer, fileKey: string, mimetype: string, size: number, sha256: string): Promise => new Promise((resolve, reject) => s3()!.send(new PutObjectCommand({ Bucket: UserConfig.config.s3!.bucket, Key: fileKey, ContentType: mimetype, - Body: new Uint8Array(file) + ContentLength: size, + Body: new Uint8Array(file), + ChecksumSHA256: sha256 })).then(resolve).catch(reject)); /** @@ -129,12 +131,12 @@ const doMultipartUpload = (file: Buffer, mimetype: string, fileKey: string): Pro /** * Uploads a file to your configured S3 provider */ -export const uploadFileS3 = (file: Buffer, mimetype: string, fileKey: string): Promise => new Promise(async (resolve, reject) => { +export const uploadFileS3 = (file: Buffer, fileKey: string, mimetype: string, size: number, sha256: string): Promise => new Promise(async (resolve, reject) => { if (!s3readyCheck) return reject(NYR); try { // todo: determine when to do multipart uplloads - await doObjectUpload(file, mimetype, fileKey); + await doObjectUpload(file, fileKey, mimetype, size, sha256); resolve(void 0); } catch (err) { log.error('Failed to upload object to S3', fileKey);