Skip to content

Commit

Permalink
Factorize code in AuditService
Browse files Browse the repository at this point in the history
Use uploadFileToStorage within saveExampleImage and saveNotesFile

Fix dark background on notes thumbnails
  • Loading branch information
yaaax committed Sep 9, 2024
1 parent ecd65d7 commit 729ff78
Showing 1 changed file with 43 additions and 46 deletions.
89 changes: 43 additions & 46 deletions confiture-rest-api/src/audits/audit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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([
Expand 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 };
}

/**
Expand Down

0 comments on commit 729ff78

Please sign in to comment.