@@ -10,6 +10,7 @@ import { AppRequest } from './app/controller/AppRequest';
1010import { GetController } from './app/controller/GetController' ;
1111import { PostController } from './app/controller/PostController' ;
1212import { DocumentManagerController } from './app/document/DocumentManagementController' ;
13+ import { MAX_UPLOAD_FILE_COUNT , MAX_UPLOAD_FILE_SIZE_BYTES } from './app/document/DocumentUploadLimits' ;
1314import { getUserSequence , stepsWithContent } from './steps' ;
1415import { AccessibilityStatementGetController } from './steps/accessibility-statement/get' ;
1516import * as applicant1AccessCodeContent from './steps/applicant1/enter-your-access-code/content' ;
@@ -22,7 +23,7 @@ import { ApplicationWithdrawnGetController } from './steps/application-withdrawn
2223import { ContactUsGetController } from './steps/contact-us/get' ;
2324import { CookiesGetController } from './steps/cookies/get' ;
2425import { DraftApplicationSaveSignOutGetController } from './steps/draft-application-save-sign-out/get' ;
25- import { ErrorController } from './steps/error/error.controller' ;
26+ import { ErrorController , HTTPError } from './steps/error/error.controller' ;
2627import * as existingApplicationContent from './steps/existing-application/content' ;
2728import { ExistingApplicationGetController } from './steps/existing-application/get' ;
2829import { ExistingApplicationPostController } from './steps/existing-application/post' ;
@@ -70,11 +71,35 @@ import { WebChatGetController } from './steps/webchat/get';
7071
7172const handleUploads = multer ( {
7273 limits : {
73- fileSize : 25 * 1024 * 1024 ,
74- files : 5 ,
74+ fileSize : MAX_UPLOAD_FILE_SIZE_BYTES ,
75+ files : MAX_UPLOAD_FILE_COUNT ,
7576 } ,
7677} ) ;
7778
79+ const uploadFilesMiddleware : RequestHandler = ( req , res , next ) => {
80+ handleUploads . array ( 'files[]' , MAX_UPLOAD_FILE_COUNT ) ( req , res , err => {
81+ if ( ! err ) {
82+ return next ( ) ;
83+ }
84+
85+ if ( err instanceof multer . MulterError ) {
86+ ( req as AppRequest ) . locals ?. logger ?. warn (
87+ `Multer rejected upload(code=${ err . code } , contentLength=${ req . headers [ 'content-length' ] || 'n/a' } )`
88+ ) ;
89+
90+ if ( err . code === 'LIMIT_FILE_SIZE' ) {
91+ return next ( new HTTPError ( 'Uploaded file exceeds the 25MB limit' , 413 ) ) ;
92+ }
93+
94+ if ( err . code === 'LIMIT_FILE_COUNT' ) {
95+ return next ( new HTTPError ( 'Uploaded file count exceeds the allowed limit' , 400 ) ) ;
96+ }
97+ }
98+
99+ return next ( err ) ;
100+ } ) ;
101+ } ;
102+
78103const ext = extname ( __filename ) ;
79104
80105export class Routes {
@@ -111,7 +136,7 @@ export class Routes {
111136 app . post ( POSTCODE_LOOKUP , errorHandler ( new PostcodeLookupPostController ( ) . post ) ) ;
112137
113138 const documentManagerController = new DocumentManagerController ( ) ;
114- app . post ( DOCUMENT_MANAGER , handleUploads . array ( 'files[]' , 5 ) , errorHandler ( documentManagerController . post ) ) ;
139+ app . post ( DOCUMENT_MANAGER , uploadFilesMiddleware , errorHandler ( documentManagerController . post ) ) ;
115140 app . get ( `${ DOCUMENT_MANAGER } /delete/:index` , errorHandler ( documentManagerController . delete ) ) ;
116141
117142 for ( const step of stepsWithContent ) {
0 commit comments