Add file type filter on upload
This commit is contained in:
parent
2f3df8dd28
commit
34c328214e
3 changed files with 22 additions and 3 deletions
|
@ -3,7 +3,11 @@ let isAuth = require("../user").isAuth;
|
||||||
|
|
||||||
|
|
||||||
router.get("/", isAuth, (req, res) => {
|
router.get("/", isAuth, (req, res) => {
|
||||||
res.render("index");
|
let invalidType = false;
|
||||||
|
if ("invalidType" in req.query) {
|
||||||
|
invalidType = true;
|
||||||
|
}
|
||||||
|
res.render("index", {invalidType: invalidType});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,24 @@ let storage = multer.diskStorage({
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let upload = multer({storage: storage});
|
let upload = multer({
|
||||||
|
storage: storage,
|
||||||
|
fileFilter: (req, file, cb) => {
|
||||||
|
if (file.mimetype.substring(0, 6) !== "image/") {
|
||||||
|
req.fileErrorValidation = true;
|
||||||
|
return cb(null, false, new Error("Wrong file type"));
|
||||||
|
}
|
||||||
|
cb(null, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
router.post("/", isAuth, upload.single("image"), (req, res) => {
|
router.post("/", isAuth, upload.single("image"), (req, res) => {
|
||||||
res.redirect("/images/" + req.file.filename);
|
if (req.fileErrorValidation) {
|
||||||
|
res.redirect("/?invalidType");
|
||||||
|
} else {
|
||||||
|
res.redirect("/images/" + req.file.filename);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
extend layout
|
extend layout
|
||||||
block content
|
block content
|
||||||
h1 Image Node
|
h1 Image Node
|
||||||
|
if invalidType
|
||||||
|
h2.fail Invalid image type !
|
||||||
form(method="POST", action="/upload", enctype="multipart/form-data")
|
form(method="POST", action="/upload", enctype="multipart/form-data")
|
||||||
input.file_button(type="file", name="image", accept="image/*")
|
input.file_button(type="file", name="image", accept="image/*")
|
||||||
button(type="submit") Upload
|
button(type="submit") Upload
|
||||||
|
|
Loading…
Reference in a new issue