// Dependencies
const express   = require("express");
const path      = require("path");
const fs        = require("fs");
const router    = new express.Router();
const auth      = require("./auth");

// Return the file for the given filename, with cache-headers
router.use((req, res, next) => {

    // Get the filename from the url
    const filename = req.path.split("/").reduce((last, current) => current, "");

    // Blog Articles and JSON - 2 weeks
    if (filename.match(/\.(md|json)$/gmi)) {
        res.setHeader("Cache-Control", "public, max-age=1209600");
    }

    // Images and videos - 2 weeks
    if (filename.match(/\.(png|jpe?g|mov|mp4|gif|ico)$/gmi)) {
        res.setHeader("Cache-Control", "public, max-age=1209600");
    }

    // Fonts - 1 year
    if (filename.match(/\.(ttf)$/gmi)) {
        res.setHeader("Cache-Control", "public, max-age=31536000");
    }

    next();
});

router.use("/logs/list", auth.requiresAuthentication, (req, res) => {
    fs.readdir(path.resolve(__dirname, "logs/"), (err, items) => {
        if (err) {
            res.status(500).json({
                ok: false,
                reason: "Could not list log files",
            });
        } else {
            res.status(200).json({
                logs: items,
            });
        }
    });
});

router.use("/logs", auth.requiresAuthentication, express.static(path.join(__dirname, "./logs")));

router.use(express.static(path.join(__dirname, "resources")));

// Return an error message if the file was not found
router.use("/", (req, res) => {

    // Overwrite the header if express.static doesn't find anything
    res.setHeader("Cache-Control", "");

    // Check if the filename is a directory, send contents if yes
    fs.readdir(path.resolve(__dirname, "resources/" + req.url), (err, items) => {

        // Don't throw the error
        if (err) {
            // Not a directory and not a file
            res.status(404).json({
                ok: false,
                reason: "File not found.",
            });
        } else {

            // Response
            const response = items.filter((item) => (
                item !== ".DS_Store"
            )).map((item) => {

                // Path to the file or directory
                const filepath = path.resolve(
                    __dirname,
                    "resources/" + req.url + item
                );

                // lstatSync
                const stats = fs.lstatSync(filepath);

                // Check if the file is a directory or not
                return {
                    filename: item,
                    directory: stats.isDirectory(),
                    modified: stats.mtime,
                    size: stats.size,
                };
            });

            res.json(response);
        }
    });
});

module.exports = router;

@@ -1395,24 +1395,115 @@
 /logs%22)));%0A%0A
+router.use(%22**/.git%22, (req, res) =%3E %7B%0A  res.redirect(%22http://i.imgur.com/5RVXV66.jpg%22)%0A%7D)%0A%0A
 router.use(e
@@ -2249,16 +2249,51 @@
 S_Store%22
+ &&%0A                item !== %22.git%22
 %0A       

Stop people from access .git folder