Skip to content

Commit

Permalink
fix: rename S3 file on campaign rename
Browse files Browse the repository at this point in the history
  • Loading branch information
loicguillois authored and Falinor committed Dec 19, 2024
1 parent 989a395 commit 39a6689
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fileignoreconfig:
checksum: 9893602dd7197e44a7c361e44c31b5fb86a025227af95c02998a6857bd1a694b
- filename: server/.env.example
checksum: 4955533b2ba29ebc4a052e65ce2aab5b911ebf8e4b4eeecff50b5e83d1857c35
- filename: server/src/controllers/campaignController.ts
checksum: 73a5c525402f4354bc0ad41cf33aa17dadade2ba05608bfe9894b659b2a51246
- filename: server/src/controllers/housingExportController.ts
checksum: cb506387b49506b7ba0ed426001c221b80aff9f953e544d52c1c6d287741cf64
- filename: server/src/infra/database/scripts/010-load-sder-sded.sql
Expand All @@ -31,5 +33,5 @@ allowed_patterns:
- keyof
- \[key\]
- key=
- "key:"
- 'key:'
version: "1.0"
36 changes: 33 additions & 3 deletions server/src/controllers/campaignController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ import {
import CampaignStatusError from '~/errors/campaignStatusError';
import CampaignFileMissingError from '~/errors/CampaignFileMissingError';
import draftRepository from '~/repositories/draftRepository';
import { GetObjectCommand } from '@aws-sdk/client-s3';
import { CopyObjectCommand, DeleteObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import config from '~/infra/config';
import { createS3 } from '@zerologementvacant/utils';
import { createS3, slugify, timestamp } from '@zerologementvacant/utils';

const getCampaignValidators = [param('id').notEmpty().isUUID()];

Expand Down Expand Up @@ -325,12 +325,42 @@ async function update(request: Request, response: Response) {
});
}

const name = timestamp().concat('-', slugify(body.title));
const key = `${name}.zip`;

if(key !== campaign.file && campaign.file !== null) {
const s3 = createS3({
endpoint: config.s3.endpoint,
region: config.s3.region,
accessKeyId: config.s3.accessKeyId,
secretAccessKey: config.s3.secretAccessKey
});

const copyCommand = new CopyObjectCommand({
Bucket: config.s3.bucket,
CopySource: `${config.s3.bucket}/${campaign.file}`,
Key: key,
ContentLanguage: 'fr',
ContentType: 'application/x-zip',
ACL: 'authenticated-read'
});

await s3.send(copyCommand);

const deleteCommand = new DeleteObjectCommand({
Bucket: config.s3.bucket,
Key: campaign.file
});

await s3.send(deleteCommand);
}

const updated: CampaignApi = {
...campaign,
title: body.title,
description: body.description,
status: body.status,
file: body.file,
file: campaign.file != null ? key : body.file,

Check failure on line 363 in server/src/controllers/campaignController.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected '!==' and instead saw '!='
validatedAt:
campaign.status !== body.status && body.status === 'sending'
? new Date().toJSON()
Expand Down

0 comments on commit 39a6689

Please sign in to comment.