collection added
This commit is contained in:
parent
6546d84999
commit
8bf0b8d831
@ -6,32 +6,24 @@ const path = require("path");
|
|||||||
const authMiddleware = require('../../middleware/authMiddleware');
|
const authMiddleware = require('../../middleware/authMiddleware');
|
||||||
|
|
||||||
const storage = multer.diskStorage({
|
const storage = multer.diskStorage({
|
||||||
destination: function (req, file, cb) {
|
destination: (req, file, cb) => {
|
||||||
// Make sure this folder exists on disk: ./uploads/cars
|
|
||||||
cb(null, path.join(__dirname, "../../../uploads/cars"));
|
cb(null, path.join(__dirname, "../../../uploads/cars"));
|
||||||
},
|
},
|
||||||
filename: function (req, file, cb) {
|
filename: (req, file, cb) => {
|
||||||
// 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";
|
let ext = ".jpg";
|
||||||
if (file.mimetype === "image/png") {
|
if (file.mimetype === "image/png") ext = ".png";
|
||||||
ext = ".png";
|
else if (file.mimetype === "image/jpeg") ext = ".jpg";
|
||||||
} else if (file.mimetype === "image/jpeg") {
|
|
||||||
ext = ".jpg";
|
|
||||||
}
|
|
||||||
|
|
||||||
const uniqueName = `${Date.now()}${ext}`;
|
const uniqueName = Date.now() + ext;
|
||||||
cb(null, uniqueName);
|
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({
|
export const upload = multer({
|
||||||
storage: storage,
|
storage,
|
||||||
limits: {
|
limits: { fileSize: 5 * 1024 * 1024 },
|
||||||
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