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"));
|
cb(null, path.join(__dirname, "../../../uploads/cars"));
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: function (req, file, cb) {
|
||||||
// Prepend a timestamp to avoid name collisions
|
// Always give it a timestamp-based name + the correct extension
|
||||||
const uniqueName = `${Date.now()}-${file.originalname}`;
|
// (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);
|
cb(null, uniqueName);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const allowedMimeTypes = new Set([
|
const allowedMimeTypes = new Set(["image/jpeg", "image/png", "image/jpg"]);
|
||||||
"image/jpeg",
|
|
||||||
"image/png",
|
|
||||||
"image/jpg",
|
|
||||||
]);
|
|
||||||
|
|
||||||
const upload = multer({
|
const upload = multer({
|
||||||
storage,
|
storage: storage,
|
||||||
limits: { fileSize: 5 * 1024 * 1024 },
|
limits: {
|
||||||
|
fileSize: 5 * 1024 * 1024, // max 5 MB
|
||||||
|
},
|
||||||
fileFilter: (req, file, cb) => {
|
fileFilter: (req, file, cb) => {
|
||||||
if (!allowedMimeTypes.has(file.mimetype)) {
|
if (!allowedMimeTypes.has(file.mimetype)) {
|
||||||
return cb(new Error("Only JPG/JPEG/PNG images are allowed"), false);
|
return cb(new Error("Only JPG/JPEG/PNG images are allowed"), false);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user