diff --git a/.env.example b/.env.example index f4dcb3d..30a20b3 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ APP_PORT=8080 APP_DEBUG=false -MAXLEN=5120 \ No newline at end of file +MAXLEN=5120 +MAXFILES=128 \ No newline at end of file diff --git a/helpers/content/index.js b/helpers/content/index.js index 1a60846..3bf7c9d 100644 --- a/helpers/content/index.js +++ b/helpers/content/index.js @@ -1,26 +1,56 @@ const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); +const glob = require('glob'); const root = path.join(process.cwd(), '/usercontent'); +const { MAXFILES } = process.env; + console.log(root); + +let initalized = false; + +let submitted = 0; + +async function init() { + if (initalized) return; + glob(root + '/**', undefined, (err, files) => { + if (err) { + console.error(err); + return; + } + files.filter(file => { + return !file.startsWith('.'); + }).forEach(file => { + submitted++; + }); + initalized = true; + }); +} + +function submitted() {return submitted;} + const make_id = () => { return crypto.randomBytes(8).toString('hex'); }; async function get(id) { + init(); return fs.readFileSync(path.join(root, id)).toString(); } async function write(fname, data) { + init(); + submitted++; return fs.writeFile(path.join(root, fname), data, () => {}); } async function create(data) { + if (submitted >= MAXFILES) return "NA"; const id = make_id(); await write(id, data); return id; } -module.exports = { get, write, create, make_id }; \ No newline at end of file +module.exports = { get, write, create, make_id, submitted }; \ No newline at end of file diff --git a/package.json b/package.json index 1910886..5588a67 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dotenv": "^16.0.3", "express": "^4.18.2", "express-async-handler": "^1.2.0", + "glob": "^9.2.1", "pug": "^3.0.2" } } diff --git a/routes/main.js b/routes/main.js index 886055d..490fa8a 100644 --- a/routes/main.js +++ b/routes/main.js @@ -1,9 +1,13 @@ const express = require('express'); const router = express.Router(); const handler = require('express-async-handler'); +const content = require('../helpers/content'); async function index(req, res) { - res.render('main', {maxlen: process.env.MAXLEN}); + res.render('main', { + maxlen: process.env.MAXLEN, + exceeded: content.submitted() >= process.env.MAXFILES + }); } router.get('/', handler(index)); diff --git a/routes/upload.js b/routes/upload.js index 64bf567..da86057 100644 --- a/routes/upload.js +++ b/routes/upload.js @@ -3,7 +3,15 @@ const router = express.Router(); const handler = require('express-async-handler'); const content = require('../helpers/content'); +const { MAXFILES } = process.env; + async function upload(req, res) { + + if (content.submitted() >= MAXFILES) { + res.status(405).send('Not allowed'); + return; + } + const data = req.body.text; const id = await content.create(data); res.redirect( diff --git a/views/main.pug b/views/main.pug index b080fa3..21f91e8 100644 --- a/views/main.pug +++ b/views/main.pug @@ -5,4 +5,10 @@ block content p(align='center') textarea(name='text' class='data' placeholder='Put your text in here!' + (maxlen ? ` (Max length is ${maxlen} bytes)` : '')) br - input(type='submit' value='Upload!') \ No newline at end of file + if (!exceeded) + input(type='submit' value='Upload!') + if (exceeded) + p(style='color:darkred;font-weight:bold;font-size:9pt') + | Max uploads limit exceeded. No more uploads would be accepted. + br + | Contact site administrator so they would increase the limit or delete some uploads. \ No newline at end of file