Skip to content

Commit

Permalink
added check to not upload same image file name again for the same rol…
Browse files Browse the repository at this point in the history
…e and campaign (#504)
  • Loading branch information
quantum-grit committed Jun 19, 2023
1 parent b5f88ca commit 6ffbcf1
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions apps/api/src/campaign-file/campaign-file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6ffbcf1

Please sign in to comment.