Skip to content

Instantly share code, notes, and snippets.

@UrusuLambda
Created February 1, 2018 15:38
Show Gist options
  • Save UrusuLambda/27234753c2283363d329d171e48d7540 to your computer and use it in GitHub Desktop.
Save UrusuLambda/27234753c2283363d329d171e48d7540 to your computer and use it in GitHub Desktop.
router.post('/', upload.single('imgfile'), function (req, res) {
var fextention = path.extname(req.file.originalname).toLowerCase();
var targetPath = uuid.v4() + fextention;
var tempPath = req.file.path,
targetResolvedPath = path.resolve('./uploads/'+targetPath);
//tempPathに置かれている画像ファイルをeasyimgのrescropでwidthを1000にする.
if ( fextention === '.png' || fextention === '.jpg' || fextention === '.bmp') {
easyimg.rescrop({
src:tempPath, dst:targetResolvedPath, width:1000
}).then(function(image){
res.json({image_url:targetPath});
});
} else {
//png jpg bmpじゃなかったらファイル削除
fs.unlink(tempPath, function () {
if (err) throw err;
res.json({message:"Only png bmp jpg files are allowed"});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment