Created
August 18, 2018 13:00
-
-
Save tamalchowdhury/a01a259e94de3fda360f1c14afe2815e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const multerOptions = { | |
storege: multer.memoryStorage(), | |
fileFilter(req, file, next) { | |
const isPhoto = file.mimetype.startsWith('image/'); | |
if(isPhoto) { | |
next(null, true) | |
} else { | |
next({message: 'You must supply an image'}, false) | |
} | |
} | |
} | |
exports.upload = multer(multerOptions).single('photo') | |
exports.resize = async (req, res, next) => { | |
// Check if there is a file | |
if(!req.file) { | |
next(); | |
return; | |
} else { | |
const extension = req.file.mimetype.split('/')[1]; | |
req.body.photo = `${uuid.v4()}.${extension}`; | |
const photo = await jimp.read(req.file.buffer); | |
await photo.resize(800, jimp.AUTO); | |
await photo.write(`./public/uploads/${req.body.photo}`); | |
next(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment