UNPKG

mean-guide-backend

Version:
27 lines (24 loc) 661 B
const multer = require('multer') const MIME_TYPE_MAP = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/jpg': 'jpg', } const storage = multer.diskStorage({ destination: (req, file, cb) => { const isValid = MIME_TYPE_MAP[file.mimetype] let error = new Error(`Invalid mime type ${file.mimetype}`) if (isValid) { error = null } cb(error, 'images') }, filename: (req, file, cb) => { const name = file.originalname.toLowerCase().split(' ').join('-') const ext = MIME_TYPE_MAP[file.mimetype] cb(null, `${name}-${Date.now()}.${ext}`) }, }) module.exports = { imageProcess: multer({ storage }).single('image') }