mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
51 lines
2.2 KiB
JavaScript
51 lines
2.2 KiB
JavaScript
/***********************************************************************************************************************************************************************
|
|
* File Name: asset_upload.middleware.js
|
|
* Type of Program: Middleware
|
|
* Description: Multer config for admin asset uploads (image/video/audio/document).
|
|
* Uses memory storage — buffer is passed directly to S3.
|
|
*
|
|
* Previously this route had no `limits.fileSize` at all, so an
|
|
* oversized upload wasn't rejected cleanly — it either exhausted
|
|
* server memory buffering the whole file or blew up with a raw
|
|
* multer/Node error that never reached R.error's JSON envelope.
|
|
* The frontend's fallback then had nothing to read a `message`
|
|
* out of, so every failure surfaced as the same generic
|
|
* "Something went wrong" regardless of cause.
|
|
*
|
|
* Limits:
|
|
* fileSize: 500 MB (matches task_upload.middleware.js)
|
|
*
|
|
* Author: Kenneth Obsequio (@lash0000)
|
|
* Date Created: Jul. 13, 2026
|
|
***********************************************************************************************************************************************************************/
|
|
const multer = require('multer');
|
|
const R = require('../utils/response.util');
|
|
|
|
const MAX_FILE_SIZE = 500 * 1024 * 1024; // 500 MB
|
|
|
|
const upload = multer({
|
|
storage: multer.memoryStorage(),
|
|
limits: { fileSize: MAX_FILE_SIZE },
|
|
});
|
|
|
|
// Wraps any multer middleware (fields/array/single) so oversized or malformed
|
|
// uploads always come back as a clean R.error JSON response the frontend can
|
|
// read a `message` out of, instead of an unhandled exception or Express's
|
|
// default HTML error page.
|
|
function handleUpload(multerMiddleware) {
|
|
return (req, res, next) => {
|
|
multerMiddleware(req, res, (err) => {
|
|
if (!err) return next();
|
|
|
|
if (err.code === 'LIMIT_FILE_SIZE') {
|
|
return R.error(res, `File exceeds the ${MAX_FILE_SIZE / (1024 * 1024)} MB size limit.`, 400);
|
|
}
|
|
|
|
console.error('[MULTER ERROR][ASSETS]', err);
|
|
return R.error(res, err.message ?? 'File upload failed.', 400);
|
|
});
|
|
};
|
|
}
|
|
|
|
module.exports = { upload, handleUpload, MAX_FILE_SIZE };
|