add upload functionality
This commit is contained in:
parent
44d6348400
commit
8965c64a83
|
@ -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 };
|
|
@ -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;
|
|
@ -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",
|
||||
|
|
|
@ -2,5 +2,6 @@ const express = require('express');
|
|||
const router = express.Router();
|
||||
|
||||
router.use(require('./main'));
|
||||
router.use(require('./upload'));
|
||||
|
||||
module.exports = router;
|
|
@ -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;
|
|
@ -0,0 +1,2 @@
|
|||
./*
|
||||
!./.gitignore
|
Loading…
Reference in New Issue