36 lines
809 B
JavaScript
36 lines
809 B
JavaScript
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;
|