node-server-engine 1.10.8 → 1.10.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -31,11 +31,13 @@ function fileUploader(filesOptions, validator) {
|
|
|
31
31
|
|
|
32
32
|
// Setup multer with memory storage
|
|
33
33
|
const storage = _multer.default.memoryStorage();
|
|
34
|
+
// Resolve global per-file upload size from env (e.g. "32MB") or fallback to 32MB
|
|
35
|
+
const globalMaxSize = process.env.UPLOAD_MAX_SIZE && (0, _bytes.default)(process.env.UPLOAD_MAX_SIZE) || 32 * 1024 * 1024;
|
|
34
36
|
const upload = (0, _multer.default)({
|
|
35
37
|
storage,
|
|
36
38
|
limits: {
|
|
37
|
-
fileSize:
|
|
38
|
-
} //
|
|
39
|
+
fileSize: globalMaxSize
|
|
40
|
+
} // Global per-file limit (bytes)
|
|
39
41
|
}).any();
|
|
40
42
|
upload(request, response, async err => {
|
|
41
43
|
if (err) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileUploader.js","names":["_WebError","require","_report","_multer","_interopRequireDefault","_validate","_path","_bytes","namespace","fileUploader","filesOptions","validator","request","response","next","reportDebug","message","data","storage","multer","memoryStorage","upload","limits","fileSize","any","err","error","uploadedFiles","Array","isArray","files","Object","values","flat","map","file","originalname","validate","uploadedFileKeys","fieldname","missingFiles","filter","option","required","includes","key","length","WebError","statusCode","errorCode","fileOption","find","mimeTypes","mimetype","expected","received","maxSize","maxSizeInBytes","
|
|
1
|
+
{"version":3,"file":"fileUploader.js","names":["_WebError","require","_report","_multer","_interopRequireDefault","_validate","_path","_bytes","namespace","fileUploader","filesOptions","validator","request","response","next","reportDebug","message","data","storage","multer","memoryStorage","globalMaxSize","process","env","UPLOAD_MAX_SIZE","bytes","upload","limits","fileSize","any","err","error","uploadedFiles","Array","isArray","files","Object","values","flat","map","file","originalname","validate","uploadedFileKeys","fieldname","missingFiles","filter","option","required","includes","key","length","WebError","statusCode","errorCode","fileOption","find","mimeTypes","mimetype","expected","received","maxSize","maxSizeInBytes","size","receivedSize","noExtension","path","basename","extname"],"sources":["../../../src/middleware/fileUploader/fileUploader.ts"],"sourcesContent":["import { Request, Response, NextFunction } from 'express';\nimport { Schema } from 'express-validator';\nimport { WebError } from 'entities/WebError';\nimport { reportDebug } from 'utils/report';\nimport multer from 'multer';\nimport { FileUploaderConfig } from './fileUploader.types';\nimport { validate } from './validate';\nimport path from 'path';\nimport bytes from 'bytes';\n\nconst namespace = 'engine:middleware:fileUploader';\n\n/**\n * Middleware for handling file uploads.\n * @param filesOptions Configuration options for expected files.\n * @param validator Validation schema for request fields.\n */\nexport function fileUploader(\n filesOptions: Array<FileUploaderConfig>,\n validator: Schema\n) {\n return async (\n request: Request,\n response: Response,\n next: NextFunction\n ): Promise<void> => {\n reportDebug({\n namespace,\n message: 'Received request to upload file',\n data: { filesOptions, validator }\n });\n\n // Setup multer with memory storage\n const storage = multer.memoryStorage();\n // Resolve global per-file upload size from env (e.g. \"32MB\") or fallback to 32MB\n const globalMaxSize =\n (process.env.UPLOAD_MAX_SIZE && bytes(process.env.UPLOAD_MAX_SIZE)) ||\n 32 * 1024 * 1024;\n\n const upload = multer({\n storage,\n limits: { fileSize: globalMaxSize } // Global per-file limit (bytes)\n }).any();\n\n upload(request, response, async (err) => {\n if (err) {\n reportDebug({\n namespace,\n message: 'Multer upload error',\n data: { error: err.message }\n });\n return next(err);\n }\n\n // Ensure `request.files` is properly typed\n let uploadedFiles: Express.Multer.File[] = [];\n if (Array.isArray(request.files)) {\n uploadedFiles = request.files;\n } else if (typeof request.files === 'object') {\n uploadedFiles = Object.values(request.files).flat();\n }\n\n // Log uploaded files\n reportDebug({\n namespace,\n message: 'Files Received',\n data: { uploadedFiles: uploadedFiles.map((file) => file.originalname) }\n });\n\n // Validate request body fields\n try {\n await validate(request, validator);\n } catch (error) {\n reportDebug({\n namespace,\n message: 'Validation failed',\n data: { error }\n });\n return next(error);\n }\n\n // Extract uploaded field names\n const uploadedFileKeys = uploadedFiles.map((file) => file.fieldname);\n\n // Check for missing required files\n const missingFiles = filesOptions.filter(\n (option) => option.required && !uploadedFileKeys.includes(option.key)\n );\n\n if (missingFiles.length > 0) {\n reportDebug({\n namespace,\n message: 'Missing required files',\n data: { missingFiles, uploadedFileKeys }\n });\n\n return next(\n new WebError({\n statusCode: 400,\n errorCode: 'missing-field',\n message: 'Some required files have not been uploaded',\n data: { missingFiles, uploadedFileKeys }\n })\n );\n }\n\n // Validate each uploaded file\n for (const file of uploadedFiles) {\n const fileOption = filesOptions.find(\n (option) => option.key === file.fieldname\n );\n\n if (!fileOption) continue;\n\n // MIME Type Validation\n if (\n fileOption.mimeTypes &&\n !fileOption.mimeTypes.includes(file.mimetype)\n ) {\n return next(\n new WebError({\n statusCode: 400,\n errorCode: 'invalid-mime-type',\n message: `Invalid file type for ${file.fieldname}`,\n data: { expected: fileOption.mimeTypes, received: file.mimetype }\n })\n );\n }\n\n // Max File Size Validation\n if (fileOption.maxSize) {\n const maxSizeInBytes = bytes(fileOption.maxSize);\n if (maxSizeInBytes && file.size > maxSizeInBytes) {\n return next(\n new WebError({\n statusCode: 400,\n errorCode: 'file-too-large',\n message: `File ${file.originalname} exceeds the allowed size`,\n data: { maxSize: fileOption.maxSize, receivedSize: file.size }\n })\n );\n }\n }\n\n // No Extension Handling\n if (fileOption.noExtension) {\n file.originalname = path.basename(\n file.originalname,\n path.extname(file.originalname)\n );\n }\n }\n\n request.files = uploadedFiles;\n\n next();\n });\n };\n}\n"],"mappings":";;;;;;;AAEA,IAAAA,SAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAC,sBAAA,CAAAH,OAAA;AAEA,IAAAI,SAAA,GAAAJ,OAAA;AACA,IAAAK,KAAA,GAAAF,sBAAA,CAAAH,OAAA;AACA,IAAAM,MAAA,GAAAH,sBAAA,CAAAH,OAAA;AAEA,MAAMO,SAAS,GAAG,gCAAgC;;AAElD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AC,CAAC;IACR,CAAC,CAAC;EACJ,CAAC;AACH","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-server-engine",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.9",
|
|
4
4
|
"description": "Framework used to develop backend services. This package ships with a lot of features to standardize the creation of services, letting you focus on the business logic.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|