Input validation, WebSocket handler, and file upload middleware

This commit is contained in:
Walusimbi Silver
2026-03-17 13:02:26 +03:00
parent 0382f813a8
commit cebe7bb0d2
3 changed files with 206 additions and 0 deletions

35
src/middleware/upload.js Normal file
View 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;