add upload functionality

This commit is contained in:
b1ek 2023-03-04 11:31:07 +10:00
parent 44d6348400
commit 8965c64a83
Signed by: blek
GPG Key ID: 14546221E3595D0C
6 changed files with 50 additions and 0 deletions

26
helpers/content/index.js Normal file
View File

@ -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 };

View File

@ -1,4 +1,8 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const bodyparse = require('body-parser');
router.use(bodyparse.json());
router.use(bodyparse.urlencoded({extended: true}));
module.exports = router; module.exports = router;

View File

@ -10,6 +10,7 @@
"author": "b1ek", "author": "b1ek",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"body-parser": "^1.20.2",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"express-async-handler": "^1.2.0", "express-async-handler": "^1.2.0",

View File

@ -2,5 +2,6 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
router.use(require('./main')); router.use(require('./main'));
router.use(require('./upload'));
module.exports = router; module.exports = router;

16
routes/upload.js Normal file
View File

@ -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;

2
usercontent/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
./*
!./.gitignore