const express = require("express"), fs = require("fs"), path = require("path"), multer = require("multer"), bodyParser = require("body-parser") var app = express() var uploads = multer({ storage: multer.memoryStorage() }) var PORT = process.env.PORT || 8080 var directory = process.env.FILES_DIR var passwd = process.env.PASSWORD var baseHTML = fs.readFileSync(path.join(__dirname, 'resources/base.html')).toString() var baseStart = baseHTML.substring(0, baseHTML.indexOf('{CONTENT}')) var baseEnd = baseHTML.substring(baseHTML.indexOf('{CONTENT}') + 9, baseHTML.length) if (!directory) { console.error("No directory specified! Please specify one using the environment variable FILES_DIR.") return } app.use('/b', express.static(directory)) app.use(express.static(path.join(__dirname, 'static'))) app.use(bodyParser.urlencoded({ extended: false })) app.listen(PORT, () => { console.log("Now listening on PORT: " + PORT) if (!passwd) { console.log("Password variable not found. Uploading from site is not available.") } else { console.log("Password variable found! Uploads are now available on the site.") } }) app.get("/b*", (req, res) => { var file = req.params[0] var absPath = path.join(directory, file) res.setHeader('Content-Type', 'text/html') try { var dirContents = fs.readdirSync(absPath) } catch (error) { res.send(baseStart + "

404: not found!

" + baseEnd) return } res.write(baseStart) res.write('Parent Directory
') for (let index = 0; index < dirContents.length; index++) { const file = dirContents[index]; res.write(`${file}
`) } if (passwd) { res.write(`
Dashboard page for this folder
`) } res.write(baseEnd) res.end() }) app.get("/", (req, res) => { res.redirect("/b") }) app.post("/upload", uploads.single('file'), (req, res) => { var file = req.file fs.writeFileSync(path.join(__dirname, req.body.directory, file.originalname), file.buffer) res.redirect(req.body.directory) }) if (passwd) { app.get("/dashboard.html", (req, res) => { res.send(fs.readFileSync(path.join(__dirname, 'resources/dashboard.html')).toString().replace("{DIRECTORY}", req.query.folder)) }) }