Last active
February 8, 2022 09:18
-
-
Save ddhira123/ab466772739ed05fb2378d91b7c5ba7d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// More Explanations on | |
// | |
const express = require('express'); | |
const multer = require('multer'); | |
const controller = require('../controllers/PredictController'); | |
const filename = `test-image-${Date.now().toString()}.jpg`; | |
// configure multer | |
const storage = multer.diskStorage({ | |
destination: (req, file, callback) => { | |
callback(null, './public/images'); | |
}, | |
filename: (req, file, callback) => { | |
callback(null, filename); | |
}, | |
}); | |
function imageFileFilter(req, file, cb){ | |
// Allowed ext | |
const filetypes = /jpeg|jpg|png|gif/; | |
// Check ext | |
const extname = file.originalname.match(filetypes); | |
// Check mime | |
const mimetype = filetypes.test(file.mimetype); | |
if(mimetype && extname){ | |
return cb(null,true); | |
} else { | |
cb('Error: Images Only!'); | |
} | |
} | |
const upload = multer({ storage, fileFilter: imageFileFilter }); | |
let uploadSingle = upload.single('file'); | |
const router = express.Router(); | |
router.post('/', (req, res, next) => { | |
uploadSingle(req, res, (err) => { // call as a normal function | |
if (err) | |
return res.status(400).send({success: false, message: "Only Images are allowed!"});; | |
const file = req.file; | |
if (!file) { | |
return res.status(400).send({success: false, message: "Please Upload A File!"});; | |
} | |
req['filename'] = filename; | |
controller.makePredictions(req, res, next); | |
}) | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment