diff --git a/packages/applications/legacy/src/controllers/helpers/fileSizeLimitError.ts b/packages/applications/legacy/src/controllers/helpers/fileSizeLimitError.ts new file mode 100644 index 0000000000..3e08c92249 --- /dev/null +++ b/packages/applications/legacy/src/controllers/helpers/fileSizeLimitError.ts @@ -0,0 +1,5 @@ +export class FileSizeLimitError extends Error { + constructor(message: string) { + super(message); + } +} diff --git a/packages/applications/legacy/src/controllers/helpers/index.ts b/packages/applications/legacy/src/controllers/helpers/index.ts index cbef267411..5755c333b7 100644 --- a/packages/applications/legacy/src/controllers/helpers/index.ts +++ b/packages/applications/legacy/src/controllers/helpers/index.ts @@ -13,3 +13,4 @@ export * from './yupTransformations'; export * from './getCurrentUrl'; export * from './getPagination'; export * from './isSoumisAuxGF'; +export * from './fileSizeLimitError'; diff --git "a/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postCorrigerDelaiAccorde.ts" "b/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postCorrigerDelaiAccorde.ts" index a3f2affa8f..4c126989ce 100644 --- "a/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postCorrigerDelaiAccorde.ts" +++ "b/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postCorrigerDelaiAccorde.ts" @@ -78,6 +78,14 @@ v1Router.post( ); } + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.GET_CORRIGER_DELAI_ACCORDE_PAGE(demandeDélaiId), { + error: request.errorFileSizeLimit, + }), + ); + } + const fichierRéponse = request.file && { contents: fs.createReadStream(request.file.path), filename: `${Date.now()}-${request.file.originalname}`, diff --git "a/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postDemanderD\303\251lai.ts" "b/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postDemanderD\303\251lai.ts" index 3e9609e826..f0dbd28b38 100644 --- "a/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postDemanderD\303\251lai.ts" +++ "b/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postDemanderD\303\251lai.ts" @@ -49,6 +49,14 @@ v1Router.post( }, }, async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.DEMANDER_DELAI(request.body.projectId), { + error: request.errorFileSizeLimit, + }), + ); + } + const { projectId, justification, diff --git "a/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postRepondreDemandeDelai.ts" "b/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postRepondreDemandeDelai.ts" index ef6040b2b1..59248cef94 100644 --- "a/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postRepondreDemandeDelai.ts" +++ "b/packages/applications/legacy/src/controllers/modificationRequest/d\303\251lai/postRepondreDemandeDelai.ts" @@ -9,6 +9,7 @@ import { AccorderDateAchèvementAntérieureDateThéoriqueError } from '../../../ import asyncHandler from '../../helpers/asyncHandler'; import { errorResponse, + FileSizeLimitError, iso8601DateToDateYupTransformation, RequestValidationErrorArray, unauthorizedResponse, @@ -57,6 +58,10 @@ v1Router.post( ); } + if (request.errorFileSizeLimit) { + return errAsync(new FileSizeLimitError(request.errorFileSizeLimit)); + } + const file = request.file && { contents: fs.createReadStream(request.file.path), filename: `${Date.now()}-${request.file.originalname}`, @@ -100,6 +105,18 @@ v1Router.post( return unauthorizedResponse({ request, response }); } + if (error instanceof FileSizeLimitError) { + return response.redirect( + addQueryParams( + routes.GET_DETAILS_DEMANDE_DELAI_PAGE(request.body.modificationRequestId), + { + ...request.body, + error, + }, + ), + ); + } + if (error instanceof RequestValidationErrorArray) { return response.redirect( addQueryParams( diff --git a/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts b/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts index f1679a05e8..e54c984141 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts @@ -62,6 +62,14 @@ v1Router.post( ), }, async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.CHANGER_FOURNISSEUR(request.body.projectId), { + error: request.errorFileSizeLimit, + }), + ); + } + const { user } = request; const { projectId, evaluationCarbone, justification } = request.body; const file = request.file && { diff --git a/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts b/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts index ebdfb0c728..70233dce5e 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts @@ -37,14 +37,20 @@ import { fr } from 'date-fns/locale'; import { mediator } from 'mediateur'; import { ModificationRequest, Project } from '../../infra/sequelize'; -const FORMAT_DATE = 'DD/MM/YYYY'; - v1Router.post( routes.ADMIN_REPLY_TO_MODIFICATION_REQUEST, upload.single('file'), ensureRole(['admin', 'dgec-validateur', 'dreal']), asyncHandler(async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.DEMANDE_PAGE_DETAILS(request.body.modificationRequestId), { + error: request.errorFileSizeLimit, + }), + ); + } + const { user: { role }, body: { diff --git a/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts b/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts index ce309a06b3..2ab21f068d 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts @@ -15,6 +15,14 @@ v1Router.post( ensureRole('porteur-projet'), upload.single('file'), asyncHandler(async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.LISTE_PROJETS, { + error: request.errorFileSizeLimit, + }), + ); + } + const { projectId } = request.body; if (!validateUniqueId(projectId)) { diff --git a/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts b/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts index 8083ee09ef..372043c6b7 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts @@ -51,6 +51,14 @@ v1Router.post( }, }, async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.GET_CHANGER_PRODUCTEUR(request.body.projetId), { + error: request.errorFileSizeLimit, + }), + ); + } + const { user } = request; const { projetId, justification, producteur } = request.body; diff --git a/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts b/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts index 6a61f4a165..5af15cfaed 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts @@ -47,6 +47,14 @@ v1Router.post( ), }, async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.DEMANDER_CHANGEMENT_PUISSANCE(request.body.projectId), { + error: request.errorFileSizeLimit, + }), + ); + } + const { body: { projectId, puissance, justification }, user, diff --git a/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts b/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts index f5eb0d1aa0..feb9933c9a 100644 --- a/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts +++ b/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts @@ -13,13 +13,19 @@ import { upload } from '../upload'; import { v1Router } from '../v1Router'; import { ProjetDéjàClasséError } from '../../modules/modificationRequest'; -const FORMAT_DATE = 'DD/MM/YYYY'; - v1Router.post( routes.ADMIN_CORRECT_PROJECT_DATA_ACTION, upload.single('file'), ensureRole(['admin', 'dgec-validateur']), asyncHandler(async (request, response) => { + if (request.errorFileSizeLimit) { + return response.redirect( + addQueryParams(routes.PROJECT_DETAILS(request.body.projectId), { + error: request.errorFileSizeLimit, + }), + ); + } + if (request.body.numeroCRE || request.body.familleId || request.body.appelOffreAndPeriode) { return response.redirect( addQueryParams(routes.PROJECT_DETAILS(request.body.projectId), { diff --git a/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts b/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts index 763de1067d..52bb9c0e9d 100644 --- a/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts +++ b/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts @@ -1,6 +1,6 @@ import fs from 'fs'; import { ensureRole, signalerDemandeDelai } from '../../config'; -import { logger } from '../../core/utils'; +import { errAsync, logger } from '../../core/utils'; import asyncHandler from '../helpers/asyncHandler'; import { UnauthorizedError } from '../../modules/shared'; import routes from '../../routes'; @@ -8,6 +8,7 @@ import { errorResponse, iso8601DateToDateYupTransformation, RequestValidationError, + FileSizeLimitError, unauthorizedResponse, validateRequestBody, } from '../helpers'; @@ -59,6 +60,10 @@ v1Router.post( asyncHandler(async (request, response) => { validateRequestBody(request.body, requestBodySchema) .asyncAndThen((body) => { + if (request.errorFileSizeLimit) { + return errAsync(new FileSizeLimitError(request.errorFileSizeLimit)); + } + const { projectId, decidedOn, status, notes, délaiCdc2022 } = body; const { user: signaledBy } = request; @@ -93,6 +98,15 @@ v1Router.post( ); }, (error) => { + if (error instanceof FileSizeLimitError) { + return response.redirect( + addQueryParams(routes.ADMIN_SIGNALER_DEMANDE_DELAI_PAGE(request.body.projectId), { + ...request.body, + error, + }), + ); + } + if (error instanceof RequestValidationError) { return response.redirect( addQueryParams(routes.ADMIN_SIGNALER_DEMANDE_DELAI_PAGE(request.body.projectId), { diff --git a/packages/applications/legacy/src/controllers/upload.ts b/packages/applications/legacy/src/controllers/upload.ts index b7f266c136..4fb06511f0 100644 --- a/packages/applications/legacy/src/controllers/upload.ts +++ b/packages/applications/legacy/src/controllers/upload.ts @@ -1,4 +1,7 @@ import multer from 'multer'; + +import type core from 'express-serve-static-core'; + import { promises as fs } from 'fs'; import { logger } from '../core/utils'; @@ -10,19 +13,30 @@ const uploadWithMulter = multer({ }); export const upload = { - single: (filename: string) => (req, res, next) => { - res.on('finish', async () => { - if (req.file) { - try { - await fs.unlink(req.file.path); - } catch (error) { - logger.error(error); + single: + (filename: string) => (req: core.Request, response: core.Response, next: core.NextFunction) => { + const uploadHandler = uploadWithMulter.single(filename); + + uploadHandler(req, response, (err) => { + if (err) { + logger.error(err); + req.errorFileSizeLimit = `Le fichier ne doit pas dépasser ${FILE_SIZE_LIMIT_IN_MB} Mo`; + return next(); } - } - }); - return uploadWithMulter.single(filename)(req, res, next); - }, + response.on('finish', async () => { + if (req.file) { + try { + await fs.unlink(req.file.path); + } catch (error) { + logger.error(error); + } + } + }); + + next(); + }); + }, multiple: (filename?: string) => (req, res, next) => { return filename ? uploadWithMulter.array(filename)(req, res, next) diff --git a/packages/applications/legacy/src/infra/keycloak/attachUserToRequestMiddleware.ts b/packages/applications/legacy/src/infra/keycloak/attachUserToRequestMiddleware.ts index 055964a05f..f833ec9080 100644 --- a/packages/applications/legacy/src/infra/keycloak/attachUserToRequestMiddleware.ts +++ b/packages/applications/legacy/src/infra/keycloak/attachUserToRequestMiddleware.ts @@ -21,6 +21,7 @@ declare module 'express-serve-static-core' { accountUrl: string; permissions: Permission[]; }; + errorFileSizeLimit?: string; } } diff --git a/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx b/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx index f87534e479..21b30ca020 100644 --- a/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx +++ b/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx @@ -1,6 +1,8 @@ import React from 'react'; import { Input, LabelDescription } from '../atoms'; +const FILE_SIZE_LIMIT_IN_MB = 25; + type InputFileProps = { id?: string; name?: string; @@ -27,7 +29,9 @@ export const InputFile = ({ disabled={disabled} className={className} /> - Taille maximale du fichier : 50 Mo + + Taille maximale du fichier : {FILE_SIZE_LIMIT_IN_MB} Mo + ); };