34 lines
846 B
TypeScript
34 lines
846 B
TypeScript
import multer, { FileFilterCallback } from "multer";
|
|
import { Request } from "express";
|
|
|
|
// Allowed image formats
|
|
const allowedImageTypes: string[] = ["image/jpeg", "image/png", "image/webp"];
|
|
|
|
const storage = multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, "uploads/profileImages");
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, `${Date.now()}-${file.originalname}`);
|
|
},
|
|
});
|
|
|
|
export const uploadProfileImage = multer({
|
|
storage,
|
|
limits: {
|
|
fileSize: 2 * 1024 * 1024, // 2 MB
|
|
},
|
|
fileFilter: (
|
|
req: Request,
|
|
file: Express.Multer.File,
|
|
cb: FileFilterCallback
|
|
) => {
|
|
if (!allowedImageTypes.includes(file.mimetype)) {
|
|
return cb(
|
|
new Error("فقط فرمتهای JPG, PNG, WEBP برای تصویر پروفایل معتبر است")
|
|
);
|
|
}
|
|
cb(null, true);
|
|
},
|
|
});
|