Skip to content

Instantly share code, notes, and snippets.

@tamalchowdhury
Created August 18, 2018 13:00
Show Gist options
  • Save tamalchowdhury/a01a259e94de3fda360f1c14afe2815e to your computer and use it in GitHub Desktop.
Save tamalchowdhury/a01a259e94de3fda360f1c14afe2815e to your computer and use it in GitHub Desktop.
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