diff --git a/confiture-rest-api/src/audits/audit.service.ts b/confiture-rest-api/src/audits/audit.service.ts index a95c8c10..983eb757 100644 --- a/confiture-rest-api/src/audits/audit.service.ts +++ b/confiture-rest-api/src/audits/audit.service.ts @@ -447,28 +447,11 @@ export class AuditService { criterium: number, file: Express.Multer.File ) { - const randomPrefix = nanoid(); - - const key = `audits/${editUniqueId}/${randomPrefix}/${file.originalname}`; - - const thumbnailKey = `audits/${editUniqueId}/${randomPrefix}/thumbnail_${file.originalname}`; - - const thumbnailBuffer = await sharp(file.buffer) - .jpeg({ - mozjpeg: true - }) - .flatten({ background: { r: 255, g: 255, b: 255, alpha: 0 } }) - .resize(200, 200, { fit: "inside" }) - .toBuffer(); - - await Promise.all([ - this.fileStorageService.uploadFile(file.buffer, file.mimetype, key), - this.fileStorageService.uploadFile( - thumbnailBuffer, - "image/jpeg", - thumbnailKey - ) - ]); + const { key, thumbnailKey } = await this.uploadFileToStorage( + editUniqueId, + file, + { createThumbnail: display === FileDisplay.ATTACHMENT } + ); const storedFile = await this.prisma.storedFile.create({ data: { @@ -528,21 +511,53 @@ export class AuditService { } async saveNotesFile(editUniqueId: string, file: Express.Multer.File) { + const { key, thumbnailKey } = await this.uploadFileToStorage( + editUniqueId, + file, + { createThumbnail: display === FileDisplay.ATTACHMENT } + ); + + const storedFile = await this.prisma.auditFile.create({ + data: { + audit: { + connect: { + editUniqueId + } + }, + + key, + originalFilename: file.originalname, + mimetype: file.mimetype, + size: file.size, + + thumbnailKey, + } + }); + + return storedFile; + } + + async uploadFileToStorage( + uniqueId: string, + file: Express.Multer.File, + options?: { createThumbnail: boolean } + ): Promise<{ key: string; thumbnailKey?: string }> { const randomPrefix = nanoid(); - const key = `audits/${editUniqueId}/${randomPrefix}/${file.originalname}`; + const key: string = `audits/${uniqueId}/${randomPrefix}/${file.originalname}`; - let thumbnailKey; + let thumbnailKey: string; - if (file.mimetype.startsWith("image")) { + if (file.mimetype.startsWith("image") && options.createThumbnail) { // If it's an image, create a thumbnail and upload it - thumbnailKey = `audits/${editUniqueId}/${randomPrefix}/thumbnail_${file.originalname}`; + thumbnailKey = `audits/${uniqueId}/${randomPrefix}/thumbnail_${file.originalname}`; const thumbnailBuffer = await sharp(file.buffer) - .resize(200, 200, { fit: "inside" }) .jpeg({ mozjpeg: true }) + .flatten({ background: { r: 255, g: 255, b: 255, alpha: 0 } }) + .resize(200, 200, { fit: "inside" }) .toBuffer(); await Promise.all([ @@ -556,25 +571,7 @@ export class AuditService { } else { await this.fileStorageService.uploadFile(file.buffer, file.mimetype, key); } - - const storedFile = await this.prisma.auditFile.create({ - data: { - audit: { - connect: { - editUniqueId - } - }, - - key, - originalFilename: file.originalname, - mimetype: file.mimetype, - size: file.size, - - thumbnailKey - } - }); - - return storedFile; + return { key, thumbnailKey }; } /**