Input validation, WebSocket handler, and file upload middleware
This commit is contained in:
35
src/middleware/upload.js
Normal file
35
src/middleware/upload.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const multer = require("multer");
|
||||
const path = require("path");
|
||||
const { nanoid } = require("nanoid");
|
||||
const config = require("../config");
|
||||
|
||||
const ALLOWED_MIMES = new Set([
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/webp",
|
||||
"image/avif",
|
||||
"image/tiff",
|
||||
"image/gif",
|
||||
"image/svg+xml",
|
||||
]);
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: config.storage.uploadDir,
|
||||
filename: (_req, file, cb) => {
|
||||
const ext = path.extname(file.originalname).toLowerCase();
|
||||
cb(null, `${nanoid(12)}${ext}`);
|
||||
},
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
limits: { fileSize: config.storage.maxFileSize },
|
||||
fileFilter: (_req, file, cb) => {
|
||||
if (!ALLOWED_MIMES.has(file.mimetype)) {
|
||||
return cb(new Error(`Unsupported format: ${file.mimetype}`));
|
||||
}
|
||||
cb(null, true);
|
||||
},
|
||||
});
|
||||
|
||||
module.exports = upload;
|
||||
Reference in New Issue
Block a user