Skip to content

Commit

Permalink
Merge pull request #871 from MTES-MCT/on-the-fly-download-link-genera…
Browse files Browse the repository at this point in the history
…tion-publipostage

feat: generate download link on the fly for publipostage
  • Loading branch information
loicguillois authored Sep 11, 2024
2 parents ad88126 + 351a91e commit 6af48cd
Show file tree
Hide file tree
Showing 10 changed files with 1,292 additions and 75 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/Draft/DraftDownloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Props {
}

function DraftDownloader(props: Readonly<Props>) {
const description = props.campaign.file?.split('/').pop()?.split('?')[0];
const description = props.campaign.file;
const link = `${config.apiEndpoint}/api/campaigns/${
props.campaign.id
}/download?x-access-token=${authService.authHeader()?.['x-access-token']}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/healthcheck/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/client-s3": "^3.649.0",
"@zerologementvacant/utils": "workspace:*",
"async": "^3.2.5",
"pg": "^8.11.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "jest"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/client-s3": "^3.649.0",
"@faker-js/faker": "^8.4.1",
"lodash": "^4.17.21",
"pino": "^9.1.0",
Expand Down
2 changes: 1 addition & 1 deletion queue/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PORT=8080
PORT=8081
AUTH_SECRET=secret
SERVICE_ACCOUNT=[email protected]
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/zlv
Expand Down
5 changes: 2 additions & 3 deletions queue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
"start": "node ./dist/bin/index.js"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/lib-storage": "^3.578.0",
"@aws-sdk/s3-request-presigner": "^3.577.0",
"@aws-sdk/client-s3": "^3.649.0",
"@aws-sdk/lib-storage": "^3.649.0",
"@bull-board/api": "5.20.2",
"@bull-board/express": "5.20.2",
"@bull-board/ui": "5.20.2",
Expand Down
16 changes: 2 additions & 14 deletions queue/src/workers/generate-mails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GetObjectCommand } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';

import archiver from 'archiver';
import { Worker, WorkerOptions } from 'bullmq';
import { parseRedisUrl } from 'parse-redis-url-simple';
Expand Down Expand Up @@ -189,20 +188,9 @@ export default function createWorker() {

logger.info('Uploaded file to S3');

const command = new GetObjectCommand({
Bucket: config.s3.bucket,
Key: Key
});

const signedUrl = await getSignedUrl(s3, command, {
expiresIn: 60 * 60 * 24 * 7 // TTL: 7 days
});

logger.info(`Generated signed URL: ${signedUrl}`);

await api.campaign.update(campaign.id, {
...campaign,
file: signedUrl
file: Key
});

return { id: campaign.id };
Expand Down
8 changes: 8 additions & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ CEREMA_TOKEN=token
CEREMA_INVITE_LIMIT=10
CEREMA_DRY_MODE=true
CEREMA_FORCE_INVITE=false

S3_ENDPOINT=
S3_REGION=any
S3_BUCKET=
S3_ACCESS_KEY_ID=whatever
S3_SECRET_ACCESS_KEY=whatever

REDIS_URL=redis://localhost:6379
6 changes: 4 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"seed": "yarn db seed:run"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.577.0",
"@aws-sdk/client-s3": "^3.649.0",
"@aws-sdk/s3-request-presigner": "^3.649.0",
"@commander-js/extra-typings": "^12.1.0",
"@elastic/elasticsearch": "^8.13.1",
"@faker-js/faker": "^8.4.1",
Expand Down Expand Up @@ -87,6 +88,7 @@
"@jest/globals": "^29.7.0",
"@tsconfig/node20": "^20.1.4",
"@types/async": "^3.2.24",
"@types/aws-sdk": "^2.7.0",
"@types/bcryptjs": "^2.4.6",
"@types/cli-progress": "^3.11.5",
"@types/convict": "^6.1.6",
Expand All @@ -103,7 +105,7 @@
"@types/lodash": "^4.17.4",
"@types/memoizee": "^0.4.11",
"@types/multer": "^1.4.11",
"@types/multer-s3": "^3.0.3",
"@types/multer-s3": "^3",
"@types/node": "^20.12.12",
"@types/nodemailer": "^6.4.15",
"@types/pg": "^8.11.6",
Expand Down
24 changes: 23 additions & 1 deletion server/src/controllers/campaignController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import CampaignStatusError from '~/errors/campaignStatusError';
import CampaignFileMissingError from '~/errors/CampaignFileMissingError';
import draftRepository from '~/repositories/draftRepository';
import DraftMissingError from '~/errors/draftMissingError';
import { GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import config from '~/infra/config';
import { createS3 } from '@zerologementvacant/utils';

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

Expand Down Expand Up @@ -82,7 +86,25 @@ async function downloadCampaign(request: Request, response: Response) {
campaign.exportedAt = new Date().toISOString();
await campaignRepository.update(campaign);

response.redirect(campaign.file);
const command = new GetObjectCommand({
Bucket: config.s3.bucket,
Key: campaign.file
});

const s3 = createS3({
endpoint: config.s3.endpoint,
region: config.s3.region,
accessKeyId: config.s3.accessKeyId,
secretAccessKey: config.s3.secretAccessKey
});

const signedUrl = await getSignedUrl(s3, command, {
expiresIn: 60 * 60 // TTL: 1 hour
});

logger.debug(`Generated signed URL: ${signedUrl}`);

response.redirect(signedUrl);
}

const listValidators: ValidationChain[] = [
Expand Down
Loading

0 comments on commit 6af48cd

Please sign in to comment.