collection added

This commit is contained in:
Jonathan Jara 2025-06-06 00:37:21 -07:00
parent d7b5a0a415
commit 6546d84999

View File

@ -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
// (dont trust file.originalname if its 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);