From 8965c64a83e06d748a4e472f706820f725014c8a Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 4 Mar 2023 11:31:07 +1000 Subject: [PATCH] add upload functionality --- helpers/content/index.js | 26 ++++++++++++++++++++++++++ middleware/index.js | 4 ++++ package.json | 1 + routes/index.js | 1 + routes/upload.js | 16 ++++++++++++++++ usercontent/.gitignore | 2 ++ 6 files changed, 50 insertions(+) create mode 100644 helpers/content/index.js create mode 100644 routes/upload.js create mode 100644 usercontent/.gitignore diff --git a/helpers/content/index.js b/helpers/content/index.js new file mode 100644 index 0000000..02f1fff --- /dev/null +++ b/helpers/content/index.js @@ -0,0 +1,26 @@ +const fs = require('fs'); +const path = require('path'); +const crypto = require('crypto'); + +const root = path.join(process.cwd(), '/usercontent'); +console.log(root); + +const make_id = () => { + return crypto.randomBytes(8).toString('hex'); +}; + +async function get(id) { + return fs.readFile(path.join(root, id)); +} + +async function write(fname, data) { + return fs.writeFile(path.join(root, fname), data); +} + +async function create(data) { + const id = make_id(); + await fs.writeFile(path.join(root, id), data); + return id; +} + +module.exports = { get, write, create, make_id }; \ No newline at end of file diff --git a/middleware/index.js b/middleware/index.js index 935c3d3..c0b37d1 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -1,4 +1,8 @@ const express = require('express'); const router = express.Router(); +const bodyparse = require('body-parser'); + +router.use(bodyparse.json()); +router.use(bodyparse.urlencoded({extended: true})); module.exports = router; \ No newline at end of file diff --git a/package.json b/package.json index e83b067..3314fc8 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "b1ek", "license": "MIT", "dependencies": { + "body-parser": "^1.20.2", "dotenv": "^16.0.3", "express": "^4.18.2", "express-async-handler": "^1.2.0", diff --git a/routes/index.js b/routes/index.js index d39da75..7720a2d 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,5 +2,6 @@ const express = require('express'); const router = express.Router(); router.use(require('./main')); +router.use(require('./upload')); module.exports = router; \ No newline at end of file diff --git a/routes/upload.js b/routes/upload.js new file mode 100644 index 0000000..64bf567 --- /dev/null +++ b/routes/upload.js @@ -0,0 +1,16 @@ +const express = require('express'); +const router = express.Router(); +const handler = require('express-async-handler'); +const content = require('../helpers/content'); + +async function upload(req, res) { + const data = req.body.text; + const id = await content.create(data); + res.redirect( + '/view?id=' + encodeURIComponent(id) + ); +} + +router.post('/upload', handler(upload)); + +module.exports = router; \ No newline at end of file diff --git a/usercontent/.gitignore b/usercontent/.gitignore new file mode 100644 index 0000000..f4e7d4b --- /dev/null +++ b/usercontent/.gitignore @@ -0,0 +1,2 @@ +./* +!./.gitignore \ No newline at end of file