36 lines
901 B
36 lines
901 B
import multer from "multer";
|
|
import path from "path";
|
|
|
|
// Konfigurasi storage
|
|
const storage = multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, "uploads/"); // Folder penyimpanan
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, file.fieldname + "-" + Date.now() + "-" + file.originalname);
|
|
},
|
|
});
|
|
|
|
// Filter jenis file
|
|
const fileFilter = (req, file, cb) => {
|
|
const allowedTypes = /jpeg|jpg|png|gif/;
|
|
const extname = allowedTypes.test(
|
|
path.extname(file.originalname).toLowerCase()
|
|
);
|
|
const mimetype = allowedTypes.test(file.mimetype);
|
|
|
|
if (mimetype && extname) {
|
|
return cb(null, true);
|
|
} else {
|
|
cb(new Error("Only images (jpeg, jpg, png, gif) are allowed"));
|
|
}
|
|
};
|
|
|
|
//for multiple images
|
|
const upload = multer({
|
|
storage: storage,
|
|
limits: { fileSize: 5 * 1024 * 1024 }, // Maksimal 5MB per file
|
|
fileFilter: fileFilter,
|
|
});
|
|
|
|
export default upload;
|