first commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Niolla
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is proprietary and confidential.
|
||||
* Unauthorized copying, modification, distribution, or use
|
||||
* of this file, via any medium, is strictly prohibited.
|
||||
*/
|
||||
|
||||
// app/middlewares/upload.middleware.js
|
||||
|
||||
const multer = require("multer");
|
||||
|
||||
// Use memory storage (required for S3 upload)
|
||||
const storage = multer.memoryStorage();
|
||||
|
||||
// File filter (only images + PDFs)
|
||||
const fileFilter = (req, file, cb) => {
|
||||
const allowedTypes = [
|
||||
"image/",
|
||||
"application/pdf",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xlsx
|
||||
"application/vnd.ms-excel" // .xls
|
||||
];
|
||||
|
||||
const isValid =
|
||||
file.mimetype.startsWith("image/") ||
|
||||
file.mimetype === "application/pdf" ||
|
||||
file.mimetype === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
|
||||
file.mimetype === "application/vnd.ms-excel";
|
||||
|
||||
if (isValid) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error("Only images, PDFs, and Excel files are allowed"), false);
|
||||
}
|
||||
};
|
||||
|
||||
// Multer instance
|
||||
const upload = multer({
|
||||
storage,
|
||||
fileFilter,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024, // max 5MB (global limit)
|
||||
},
|
||||
});
|
||||
|
||||
// Middleware wrappers
|
||||
|
||||
// Single file upload
|
||||
const uploadSingle = (fieldName) => upload.single(fieldName);
|
||||
|
||||
// Multiple files (same field)
|
||||
const uploadMultiple = (fieldName, maxCount = 5) =>
|
||||
upload.array(fieldName, maxCount);
|
||||
|
||||
// Multiple fields (advanced)
|
||||
const uploadFields = (fields) => upload.fields(fields);
|
||||
|
||||
module.exports = {
|
||||
uploadSingle,
|
||||
uploadMultiple,
|
||||
uploadFields,
|
||||
};
|
||||
Reference in New Issue
Block a user