update framework

This commit is contained in:
2026-07-22 10:25:02 +07:00
parent 3da8608cf1
commit a836645728
6 changed files with 160 additions and 124 deletions

View File

@@ -106,9 +106,19 @@ const agentStorage = multer.diskStorage({
}
});
const multipartLimits = Object.freeze({
fieldNameSize: 64,
fieldNestingDepth: 0,
fieldSize: 64 * 1024,
fields: 16,
files: 1,
parts: 17
});
const upload = multer({
storage,
limits: {
...multipartLimits,
fileSize: Number(process.env.MAX_UPLOAD_BYTES || 1024 * 1024 * 1024)
}
});
@@ -116,6 +126,7 @@ const upload = multer({
const agentUpload = multer({
storage: agentStorage,
limits: {
...multipartLimits,
fileSize: Number(process.env.AGENT_MAX_UPLOAD_BYTES || process.env.MAX_UPLOAD_BYTES || 1024 * 1024 * 1024)
}
});
@@ -123,7 +134,7 @@ const agentUpload = multer({
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true, limit: '32kb', parameterLimit: 100 }));
app.use(express.json({ limit: '32kb' }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/vendor/notiflix', express.static(path.join(__dirname, 'node_modules/notiflix/dist')));
@@ -2450,6 +2461,29 @@ app.get('/builder', asyncRoute(async (req, res) => {
}));
app.use(async (error, req, res, next) => {
if (error instanceof multer.MulterError) {
const uploadErrorMessages = {
LIMIT_FIELD_COUNT: 'Biểu mẫu upload có quá nhiều trường.',
LIMIT_FIELD_KEY: 'Tên trường upload quá dài.',
LIMIT_FIELD_NESTING: 'Tên trường upload không đúng định dạng.',
LIMIT_FIELD_VALUE: 'Nội dung một trường upload quá dài.',
LIMIT_FILE_COUNT: 'Chỉ được upload một file mỗi lần.',
LIMIT_FILE_SIZE: 'File upload vượt quá dung lượng cho phép.',
LIMIT_PART_COUNT: 'Biểu mẫu upload có quá nhiều thành phần.',
LIMIT_UNEXPECTED_FILE: 'Trường file upload không hợp lệ.'
};
const returnPath = req.path.startsWith('/agent/') ? '/agent' : '/packages';
console.warn(`Rejected multipart upload (${error.code || 'UNKNOWN'}):`, error.message);
redirectWithNotice(
res,
returnPath,
'warning',
uploadErrorMessages[error.code] || 'Biểu mẫu upload không hợp lệ.'
);
return;
}
console.error(error);
if (req.path.startsWith('/api/notifications')) {