diff --git a/routes/index.js b/routes/index.js index 0760d20..41d71b0 100644 --- a/routes/index.js +++ b/routes/index.js @@ -3,7 +3,11 @@ let isAuth = require("../user").isAuth; router.get("/", isAuth, (req, res) => { - res.render("index"); + let invalidType = false; + if ("invalidType" in req.query) { + invalidType = true; + } + res.render("index", {invalidType: invalidType}); }); diff --git a/routes/upload.js b/routes/upload.js index 07044a4..7d31abb 100644 --- a/routes/upload.js +++ b/routes/upload.js @@ -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) => { - res.redirect("/images/" + req.file.filename); + if (req.fileErrorValidation) { + res.redirect("/?invalidType"); + } else { + res.redirect("/images/" + req.file.filename); + } }); diff --git a/views/index.pug b/views/index.pug index 65dde84..57d7986 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,6 +1,8 @@ extend layout block content h1 Image Node + if invalidType + h2.fail Invalid image type ! form(method="POST", action="/upload", enctype="multipart/form-data") input.file_button(type="file", name="image", accept="image/*") button(type="submit") Upload