1- import { Express } from "express" ;
1+ import { Express , RequestHandler } from "express" ;
22import session from "express-session" ;
33import bodyParser from "body-parser" ;
44import cookieParser from "cookie-parser" ;
@@ -16,7 +16,13 @@ import { schemas } from "./openapi/schemas";
1616import { COSMICDS_OPENAPI_VERSION , COSMICDS_OPENAPI_APIKEY_SCHEME , COSMICDS_OPENAPI_TAGS } from "./openapi/options" ;
1717import { registerSwaggerDocs } from "./openapi/utils" ;
1818
19- export const uploader = multer ( { storage : multer . memoryStorage ( ) } ) ;
19+ const MAX_SIZE_MB = 5 ;
20+ export const uploader = multer ( {
21+ storage : multer . memoryStorage ( ) ,
22+ limits : {
23+ fileSize : MAX_SIZE_MB * 1024 * 1024 ,
24+ }
25+ } ) ;
2026
2127export function setupApp ( app : Express , db : Sequelize ) {
2228
@@ -72,7 +78,22 @@ export function setupApp(app: Express, db: Sequelize) {
7278 app . use ( apiKeyMiddleware ) ;
7379
7480 // parse requests of content-type - application/json
75- app . use ( bodyParser . json ( ) ) ;
81+ // Skip paths where we aren't going to be uploading JSON
82+ // e.g. temporary file paths
83+ const jsonParser = bodyParser . json ( ) ;
84+ function skipForEndpoints ( endpoints : [ string , string [ ] ] [ ] ) : RequestHandler {
85+ return function ( req , res , next ) {
86+ for ( const [ path , methods ] of endpoints ) {
87+ if ( req . path . startsWith ( path ) && methods . some ( method => req . method . toLowerCase ( ) == method ) ) {
88+ return next ( ) ;
89+ }
90+ }
91+ return jsonParser ( req , res , next ) ;
92+ } ;
93+ }
94+ app . use ( skipForEndpoints ( [
95+ [ "/temp" , [ "post" , "patch" ] ] ,
96+ ] ) ) ;
7697
7798 // parse requests of content-type - application/x-www-form-urlencoded
7899 app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
0 commit comments