From 6ffbcf145d578aa5baa4222f7894588035b87003 Mon Sep 17 00:00:00 2001 From: quantum-grit <91589884+quantum-grit@users.noreply.github.com> Date: Mon, 19 Jun 2023 13:05:48 +0300 Subject: [PATCH] added check to not upload same image file name again for the same role and campaign (#504) --- .../campaign-file/campaign-file.service.ts | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/apps/api/src/campaign-file/campaign-file.service.ts b/apps/api/src/campaign-file/campaign-file.service.ts index 6cd1d5069..836fd94a9 100644 --- a/apps/api/src/campaign-file/campaign-file.service.ts +++ b/apps/api/src/campaign-file/campaign-file.service.ts @@ -26,19 +26,25 @@ export class CampaignFileService { campaignId, personId: person.id, } - const dbFile = await this.prisma.campaignFile.create({ data: file }) + // Check if the file already exists in the DB + let dbFile = await this.prisma.campaignFile.findFirst({ where: { role, campaignId, filename } }) - // Use the DB primary key as the S3 key. This will make sure it is always unique. - await this.s3.uploadObject( - this.bucketName, - dbFile.id, - encodeURIComponent(filename), - mimetype, - buffer, - 'Campaign', - campaignId, - person.id, - ) + if (!dbFile) { + // If not, create a new record + dbFile = await this.prisma.campaignFile.create({ data: file }) + + // Use the DB primary key as the S3 key. This will make sure it is always unique. + await this.s3.uploadObject( + this.bucketName, + dbFile.id, + encodeURIComponent(filename), + mimetype, + buffer, + 'Campaign', + campaignId, + person.id, + ) + } return dbFile.id }