diff --git a/app.js b/app.js index 07026b1..e7d0f45 100644 --- a/app.js +++ b/app.js @@ -10,6 +10,7 @@ let indexRoute = require("./routes/index"); let uploadRoute = require("./routes/upload"); let loginRoute = require("./routes/login"); let imagesRoute = require("./routes/images"); +let removeRoute = require("./routes/remove"); let notFoundRoute = require("./routes/notFound"); let errorRoute = require("./routes/error"); @@ -42,6 +43,7 @@ app.use(morgan("dev")) .use("/upload", uploadRoute) .use("/login", loginRoute) .use("/images", imagesRoute) + .use("/remove", removeRoute) .use(notFoundRoute) .use(errorRoute) .listen(process.env.PORT || 8080); diff --git a/routes/remove.js b/routes/remove.js new file mode 100644 index 0000000..ad883a1 --- /dev/null +++ b/routes/remove.js @@ -0,0 +1,17 @@ +let router = require("express").Router(); +let isAuth = require("../user").isAuth; +let fs = require("fs"); + + +router.get("/:name", isAuth, (req, res, next) => { + if ("name" in req.params && fs.existsSync("./images/"+req.params.name)) { + fs.unlinkSync("./images/"+req.params.name); + res.redirect("/"); + } else { + next(); + } +}); + + +module.exports = router; +