collection added
This commit is contained in:
parent
d7b5a0a415
commit
6546d84999
@ -11,21 +11,27 @@ const storage = multer.diskStorage({
|
||||
cb(null, path.join(__dirname, "../../../uploads/cars"));
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
// Prepend a timestamp to avoid name collisions
|
||||
const uniqueName = `${Date.now()}-${file.originalname}`;
|
||||
// Always give it a timestamp-based name + the correct extension
|
||||
// (don’t trust file.originalname if it’s a data:URI blob)
|
||||
let ext = ".jpg";
|
||||
if (file.mimetype === "image/png") {
|
||||
ext = ".png";
|
||||
} else if (file.mimetype === "image/jpeg") {
|
||||
ext = ".jpg";
|
||||
}
|
||||
|
||||
const uniqueName = `${Date.now()}${ext}`;
|
||||
cb(null, uniqueName);
|
||||
},
|
||||
});
|
||||
|
||||
const allowedMimeTypes = new Set([
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/jpg",
|
||||
]);
|
||||
const allowedMimeTypes = new Set(["image/jpeg", "image/png", "image/jpg"]);
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
limits: { fileSize: 5 * 1024 * 1024 },
|
||||
storage: storage,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024, // max 5 MB
|
||||
},
|
||||
fileFilter: (req, file, cb) => {
|
||||
if (!allowedMimeTypes.has(file.mimetype)) {
|
||||
return cb(new Error("Only JPG/JPEG/PNG images are allowed"), false);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user