Created
November 4, 2018 23:31
-
-
Save linux08/cc1898987b8aee7dc1782626f279ca4f 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
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