Spaces:
Running
Running
import multer from "multer"; | |
import path from "path"; | |
import { Request } from "express"; | |
const storage = multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, 'uploads/'); | |
}, | |
filename: (req, file, cb) => { | |
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9); | |
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname)); | |
} | |
}); | |
const fileFilter = (req: Request, file: Express.Multer.File, cb: multer.FileFilterCallback) => { | |
// Allow only image files | |
if (file.mimetype.startsWith('image/')) { | |
cb(null, true); | |
} else { | |
cb(new Error('Only image files are allowed')); | |
} | |
}; | |
export const upload = multer({ | |
storage, | |
fileFilter, | |
limits: { | |
fileSize: 5 * 1024 * 1024, // 5MB limit | |
} | |
}); | |