From 0bffca41c150229f0132dd377a1fb79848686f07 Mon Sep 17 00:00:00 2001 From: Hubert MONCENIS Date: Thu, 13 Feb 2025 17:26:30 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20Afficher=20un=20message=20d'?= =?UTF-8?q?alerte=20sur=20la=20page=20du=20formulaire=20si=20une=20fichier?= =?UTF-8?q?=20d=C3=A9passe=20la=20limite=20autoris=C3=A9e=20(100%=20legacy?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../d\303\251lai/postCorrigerDelaiAccorde.ts" | 8 +++- .../d\303\251lai/postDemanderD\303\251lai.ts" | 9 +++- .../d\303\251lai/postRepondreDemandeDelai.ts" | 8 +++- .../fournisseur/postChangerFournisseur.ts | 8 +++- .../postReplyToModificationRequest.ts | 8 +++- .../postRequestModification.ts | 8 +++- .../producteur/postChangerProducteur.ts | 9 +++- .../postDemanderChangementPuissance.ts | 9 +++- .../project/postCorrectProjectData.ts | 9 +++- .../project/postSignalerDemandeDelai.ts | 9 +++- .../legacy/src/controllers/upload.ts | 43 ++++++++++++++----- .../components/UI/molecules/InputFile.tsx | 2 +- 12 files changed, 108 insertions(+), 22 deletions(-) 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..c041c1a49e 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" @@ -41,7 +41,13 @@ const schema = yup.object({ v1Router.post( routes.POST_CORRIGER_DELAI_ACCORDE, - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.GET_CORRIGER_DELAI_ACCORDE_PAGE(request.body.demandeDelaiId), { + error, + }), + ), + ), ensureRole(['admin', 'dgec-validateur', 'dreal']), safeAsyncHandler( { 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..b0b653cfcb 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" @@ -34,7 +34,14 @@ const schema = yup.object({ v1Router.post( routes.DEMANDE_DELAI_ACTION, - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.DEMANDER_DELAI(request.body.projectId), { + ...omit(request.body, 'projectId'), + error, + }), + ), + ), ensureRole('porteur-projet'), safeAsyncHandler( { 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..a39d79e01c 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" @@ -37,7 +37,13 @@ const requestBodySchema = yup.object({ v1Router.post( routes.ADMIN_REPONDRE_DEMANDE_DELAI, ensureRole(['admin', 'dgec-validateur', 'dreal']), - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.GET_DETAILS_DEMANDE_DELAI_PAGE(request.body.modificationRequestId), { + error, + }), + ), + ), asyncHandler(async (request, response) => { validateRequestBodyForErrorArray(request.body, requestBodySchema) .asyncAndThen((body) => { diff --git a/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts b/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts index f1679a05e8..0701e3d162 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts @@ -50,7 +50,13 @@ const schema = yup.object({ v1Router.post( routes.CHANGEMENT_FOURNISSEUR_ACTION, ensureRole('porteur-projet'), - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.CHANGER_FOURNISSEUR(request.body.projectId), { + error, + }), + ), + ), safeAsyncHandler( { schema, diff --git a/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts b/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts index ebdfb0c728..84fed0655c 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts @@ -42,7 +42,13 @@ const FORMAT_DATE = 'DD/MM/YYYY'; v1Router.post( routes.ADMIN_REPLY_TO_MODIFICATION_REQUEST, - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.DEMANDE_PAGE_DETAILS(request.body.modificationRequestId), { + error, + }), + ), + ), ensureRole(['admin', 'dgec-validateur', 'dreal']), asyncHandler(async (request, response) => { const { diff --git a/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts b/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts index ce309a06b3..cbf4e138d2 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts @@ -13,7 +13,13 @@ import { v1Router } from '../v1Router'; v1Router.post( routes.DEMANDE_ACTION, ensureRole('porteur-projet'), - upload.single('file'), + upload.single('file', (_, response, error) => + response.redirect( + addQueryParams(routes.LISTE_PROJETS, { + error, + }), + ), + ), asyncHandler(async (request, response) => { const { projectId } = request.body; diff --git a/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts b/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts index 8083ee09ef..685090690d 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts @@ -36,7 +36,14 @@ const schema = yup.object({ v1Router.post( routes.POST_CHANGER_PRODUCTEUR, - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.GET_CHANGER_PRODUCTEUR(request.body.projetId), { + ...omit(request.body, 'projectId'), + error, + }), + ), + ), ensureRole('porteur-projet'), safeAsyncHandler( { diff --git a/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts b/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts index 6a61f4a165..f8d600344a 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts @@ -34,7 +34,14 @@ const schema = yup.object({ v1Router.post( routes.CHANGEMENT_PUISSANCE_ACTION, ensureRole('porteur-projet'), - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.DEMANDER_CHANGEMENT_PUISSANCE(request.body.projectId), { + ...omit(request.body, 'projectId'), + error, + }), + ), + ), safeAsyncHandler( { schema, diff --git a/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts b/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts index f5eb0d1aa0..5bd89aeb0c 100644 --- a/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts +++ b/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts @@ -12,12 +12,19 @@ import { errorResponse } from '../helpers'; import { upload } from '../upload'; import { v1Router } from '../v1Router'; import { ProjetDéjàClasséError } from '../../modules/modificationRequest'; +import { request } from 'http'; const FORMAT_DATE = 'DD/MM/YYYY'; v1Router.post( routes.ADMIN_CORRECT_PROJECT_DATA_ACTION, - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.PROJECT_DETAILS(request.body.projectId), { + error, + }), + ), + ), ensureRole(['admin', 'dgec-validateur']), asyncHandler(async (request, response) => { if (request.body.numeroCRE || request.body.familleId || request.body.appelOffreAndPeriode) { diff --git a/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts b/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts index 763de1067d..539ad0f8ff 100644 --- a/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts +++ b/packages/applications/legacy/src/controllers/project/postSignalerDemandeDelai.ts @@ -54,7 +54,14 @@ const requestBodySchema = yup.object({ v1Router.post( routes.ADMIN_SIGNALER_DEMANDE_DELAI_POST, - upload.single('file'), + upload.single('file', (request, response, error) => + response.redirect( + addQueryParams(routes.ADMIN_SIGNALER_DEMANDE_DELAI_PAGE(request.body.projectId), { + ...request.body, + error, + }), + ), + ), ensureRole(['admin', 'dgec-validateur', 'dreal']), asyncHandler(async (request, response) => { validateRequestBody(request.body, requestBodySchema) diff --git a/packages/applications/legacy/src/controllers/upload.ts b/packages/applications/legacy/src/controllers/upload.ts index b7f266c136..e57bbb37ce 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,37 @@ 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, + errorCallback: (request: core.Request, response: core.Response, error: string) => void, + ) => + (req, response, next) => { + const uploadHandler = uploadWithMulter.single(filename); + + uploadHandler(req, response, (err) => { + if (err) { + logger.error(err); + return errorCallback( + req, + response, + `Le fichier ne pas dépasser ${FILE_SIZE_LIMIT_IN_MB} Mo`, + ); } - } - }); - 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/views/components/UI/molecules/InputFile.tsx b/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx index f87534e479..4821db2a7d 100644 --- a/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx +++ b/packages/applications/legacy/src/views/components/UI/molecules/InputFile.tsx @@ -27,7 +27,7 @@ export const InputFile = ({ disabled={disabled} className={className} /> - Taille maximale du fichier : 50 Mo + Taille maximale du fichier : 25 Mo ); }; From 944ffd9eadc5f8ab31b536758d0f2e5b315fb851 Mon Sep 17 00:00:00 2001 From: Hubert Moncenis Date: Thu, 13 Feb 2025 17:35:44 +0100 Subject: [PATCH 2/3] Update packages/applications/legacy/src/controllers/upload.ts Co-authored-by: Violette <27735540+VioMrqs@users.noreply.github.com> --- packages/applications/legacy/src/controllers/upload.ts | 2 +- .../legacy/src/views/components/UI/molecules/InputFile.tsx | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/applications/legacy/src/controllers/upload.ts b/packages/applications/legacy/src/controllers/upload.ts index e57bbb37ce..86bfc5b042 100644 --- a/packages/applications/legacy/src/controllers/upload.ts +++ b/packages/applications/legacy/src/controllers/upload.ts @@ -27,7 +27,7 @@ export const upload = { return errorCallback( req, response, - `Le fichier ne pas dépasser ${FILE_SIZE_LIMIT_IN_MB} Mo`, + `Le fichier ne doit pas dépasser ${FILE_SIZE_LIMIT_IN_MB} Mo`, ); } 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 4821db2a7d..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 : 25 Mo + + Taille maximale du fichier : {FILE_SIZE_LIMIT_IN_MB} Mo + ); }; From 398e7339f1fe775b13857c3b7d2126be7c0a8b51 Mon Sep 17 00:00:00 2001 From: Hubert MONCENIS Date: Fri, 14 Feb 2025 10:17:38 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20Message=20d'erreur=20si=20fi?= =?UTF-8?q?chier=20plus=20gros=20que=2025=20Mo=20sur=20legacy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/helpers/fileSizeLimitError.ts | 5 ++++ .../legacy/src/controllers/helpers/index.ts | 1 + .../d\303\251lai/postCorrigerDelaiAccorde.ts" | 16 ++++++------ .../d\303\251lai/postDemanderD\303\251lai.ts" | 17 +++++++------ .../d\303\251lai/postRepondreDemandeDelai.ts" | 25 +++++++++++++------ .../fournisseur/postChangerFournisseur.ts | 16 ++++++------ .../postReplyToModificationRequest.ts | 18 ++++++------- .../postRequestModification.ts | 16 ++++++------ .../producteur/postChangerProducteur.ts | 17 +++++++------ .../postDemanderChangementPuissance.ts | 17 +++++++------ .../project/postCorrectProjectData.ts | 19 +++++++------- .../project/postSignalerDemandeDelai.ts | 25 ++++++++++++------- .../legacy/src/controllers/upload.ts | 13 +++------- .../keycloak/attachUserToRequestMiddleware.ts | 1 + 14 files changed, 116 insertions(+), 90 deletions(-) create mode 100644 packages/applications/legacy/src/controllers/helpers/fileSizeLimitError.ts 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 c041c1a49e..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" @@ -41,13 +41,7 @@ const schema = yup.object({ v1Router.post( routes.POST_CORRIGER_DELAI_ACCORDE, - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.GET_CORRIGER_DELAI_ACCORDE_PAGE(request.body.demandeDelaiId), { - error, - }), - ), - ), + upload.single('file'), ensureRole(['admin', 'dgec-validateur', 'dreal']), safeAsyncHandler( { @@ -84,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 b0b653cfcb..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" @@ -34,14 +34,7 @@ const schema = yup.object({ v1Router.post( routes.DEMANDE_DELAI_ACTION, - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.DEMANDER_DELAI(request.body.projectId), { - ...omit(request.body, 'projectId'), - error, - }), - ), - ), + upload.single('file'), ensureRole('porteur-projet'), safeAsyncHandler( { @@ -56,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 a39d79e01c..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, @@ -37,13 +38,7 @@ const requestBodySchema = yup.object({ v1Router.post( routes.ADMIN_REPONDRE_DEMANDE_DELAI, ensureRole(['admin', 'dgec-validateur', 'dreal']), - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.GET_DETAILS_DEMANDE_DELAI_PAGE(request.body.modificationRequestId), { - error, - }), - ), - ), + upload.single('file'), asyncHandler(async (request, response) => { validateRequestBodyForErrorArray(request.body, requestBodySchema) .asyncAndThen((body) => { @@ -63,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}`, @@ -106,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 0701e3d162..e54c984141 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/fournisseur/postChangerFournisseur.ts @@ -50,13 +50,7 @@ const schema = yup.object({ v1Router.post( routes.CHANGEMENT_FOURNISSEUR_ACTION, ensureRole('porteur-projet'), - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.CHANGER_FOURNISSEUR(request.body.projectId), { - error, - }), - ), - ), + upload.single('file'), safeAsyncHandler( { schema, @@ -68,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 84fed0655c..70233dce5e 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/postReplyToModificationRequest.ts @@ -37,20 +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', (request, response, error) => - response.redirect( - addQueryParams(routes.DEMANDE_PAGE_DETAILS(request.body.modificationRequestId), { - error, - }), - ), - ), + 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 cbf4e138d2..2ab21f068d 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/postRequestModification.ts @@ -13,14 +13,16 @@ import { v1Router } from '../v1Router'; v1Router.post( routes.DEMANDE_ACTION, ensureRole('porteur-projet'), - upload.single('file', (_, response, error) => - response.redirect( - addQueryParams(routes.LISTE_PROJETS, { - error, - }), - ), - ), + 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 685090690d..372043c6b7 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/producteur/postChangerProducteur.ts @@ -36,14 +36,7 @@ const schema = yup.object({ v1Router.post( routes.POST_CHANGER_PRODUCTEUR, - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.GET_CHANGER_PRODUCTEUR(request.body.projetId), { - ...omit(request.body, 'projectId'), - error, - }), - ), - ), + upload.single('file'), ensureRole('porteur-projet'), safeAsyncHandler( { @@ -58,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 f8d600344a..5af15cfaed 100644 --- a/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts +++ b/packages/applications/legacy/src/controllers/modificationRequest/puissance/postDemanderChangementPuissance.ts @@ -34,14 +34,7 @@ const schema = yup.object({ v1Router.post( routes.CHANGEMENT_PUISSANCE_ACTION, ensureRole('porteur-projet'), - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.DEMANDER_CHANGEMENT_PUISSANCE(request.body.projectId), { - ...omit(request.body, 'projectId'), - error, - }), - ), - ), + upload.single('file'), safeAsyncHandler( { schema, @@ -54,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 5bd89aeb0c..feb9933c9a 100644 --- a/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts +++ b/packages/applications/legacy/src/controllers/project/postCorrectProjectData.ts @@ -12,21 +12,20 @@ import { errorResponse } from '../helpers'; import { upload } from '../upload'; import { v1Router } from '../v1Router'; import { ProjetDéjàClasséError } from '../../modules/modificationRequest'; -import { request } from 'http'; - -const FORMAT_DATE = 'DD/MM/YYYY'; v1Router.post( routes.ADMIN_CORRECT_PROJECT_DATA_ACTION, - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.PROJECT_DETAILS(request.body.projectId), { - error, - }), - ), - ), + 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 539ad0f8ff..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'; @@ -54,18 +55,15 @@ const requestBodySchema = yup.object({ v1Router.post( routes.ADMIN_SIGNALER_DEMANDE_DELAI_POST, - upload.single('file', (request, response, error) => - response.redirect( - addQueryParams(routes.ADMIN_SIGNALER_DEMANDE_DELAI_PAGE(request.body.projectId), { - ...request.body, - error, - }), - ), - ), + upload.single('file'), ensureRole(['admin', 'dgec-validateur', 'dreal']), 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; @@ -100,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 86bfc5b042..4fb06511f0 100644 --- a/packages/applications/legacy/src/controllers/upload.ts +++ b/packages/applications/legacy/src/controllers/upload.ts @@ -14,21 +14,14 @@ const uploadWithMulter = multer({ export const upload = { single: - ( - filename: string, - errorCallback: (request: core.Request, response: core.Response, error: string) => void, - ) => - (req, response, next) => { + (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); - return errorCallback( - req, - response, - `Le fichier ne doit pas dépasser ${FILE_SIZE_LIMIT_IN_MB} Mo`, - ); + req.errorFileSizeLimit = `Le fichier ne doit pas dépasser ${FILE_SIZE_LIMIT_IN_MB} Mo`; + return next(); } response.on('finish', async () => { 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; } }