Skip to content

Instantly share code, notes, and snippets.

@linux08
Created November 4, 2018 23:31
Show Gist options
  • Save linux08/cc1898987b8aee7dc1782626f279ca4f to your computer and use it in GitHub Desktop.
Save linux08/cc1898987b8aee7dc1782626f279ca4f to your computer and use it in GitHub Desktop.
exports.uploadFile = async (req, res, next) => {
if (!req.file) {
return res.status(422).json({
error: 'File needs to be provided.',
});
}
const mime = req.file.mimetype;
if (mime.split('/')[0] !== 'image') {
fs.unlink(req.file.path);
return res.status(422).json({
error: 'File needs to be an image.',
});
}
const fileSize = req.file.size;
if (fileSize > MAX_SIZE) {
fs.unlink(req.file.path);
return res.status(422).json({
error: `Image needs to be smaller than ${MAX_SIZE} bytes.`,
});
}
const data = fs.readFileSync(req.file.path);
return ipfs.add(data)
.then((file) => {
if (file) {
req.data = file;
next();
} else {
res.status(400).send('Error processing file');
}
})
.catch((err) => {
res.status(500).send(err.message);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment