diff options
author | ache <ache@ache.one> | 2019-11-16 23:37:24 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2019-11-16 23:38:29 +0100 |
commit | b965cadb4e94bab93b73471621846b2713250aee (patch) | |
tree | 6cbcb6aa58d675894f9b21a6712a7c8338765dba /app.js | |
parent | Fix install (diff) |
Add save button
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 65 |
1 files changed, 49 insertions, 16 deletions
@@ -1,13 +1,14 @@ 'use-strict'; const fs = require('fs'); +const path = require('path'); const dirTree = require('directory-tree'); const express = require('express'); const report = require('vfile-reporter'); const hmd = require('./tohtml'); -const path = 'md'; +const pathMD = 'md'; const app = express(); process.on('uncaughtException', err => { @@ -16,15 +17,20 @@ process.on('uncaughtException', err => { const useLandScript = ''; const rawButton = '<button class="raw_button" ><div><div>Raw</div></div></button></form>'; +const editButton = '<button class="raw_button" style="right: 45px;"><div><div>Edit</div></div></button></form>'; app.use(express.static('public')); +app.use('/edit', express.json()); -app.get(`/${path}/*`, (req, res) => { +app.get(`/${pathMD}/*`, (req, res) => { const url = decodeURI(req._parsedUrl.pathname); const {query} = req; console.log(`[${new Date()}] > ${200} - ${url}`); - if (query && query.raw === 'true') { + + if (query && query.edit === 'true') { + res.redirect(`/edit${url.slice(3)}`); + } else if (query && query.raw === 'true') { res.sendFile(url, { root: '.', dotfiles: 'deny', @@ -49,7 +55,7 @@ app.get(`/${path}/*`, (req, res) => { hmd(data, (err, file) => { res.send(`${String(file) + useLandScript - }<a href="${url}?raw=true" class="no-style">${rawButton}</a>`); + }<a href="${url}?raw=true" class="no-style">${rawButton}</a><a href="${url}?edit=true" class="no-style">${editButton}</a>`); console.error(report(err || file)); }); }); @@ -57,22 +63,23 @@ app.get(`/${path}/*`, (req, res) => { app.get('/data', (req, res) => { console.log(`[${new Date()}] > ${200} - ${req.url}`); - res.send(dirTree(path, {extensions: /\.md/})); + res.send(dirTree(pathMD, {extensions: /\.md/})); }); app.get('/img/*', (req, res) => { console.log(`[${new Date()}] > ${200} - ${req.url}`); - if (path === '/img/ic_info_black_48px.svg' || - path === '/imr/ic_error_black_48px.svg' || - path === '/imr/ic_good_black_48px.svg' || - path === '/imr/ic_bad_black_48px.svg' || - path === '/imr/ic_comment_black_48px.svg' || - path === '/imr/ic_help_black_48px.svg') { - const img = fs.readFileSync(path); + const pathR = req.url; + if (pathR === '/img/ic_info_black_48px.svg' || + pathR === '/imr/ic_error_black_48px.svg' || + pathR === '/imr/ic_good_black_48px.svg' || + pathR === '/imr/ic_bad_black_48px.svg' || + pathR === '/imr/ic_comment_black_48px.svg' || + pathR === '/imr/ic_help_black_48px.svg') { + const img = fs.readFileSync(pathR); res.writeHead(200, {'Content-Type': 'image/svg'}); res.end(img, 'binary'); } else { - const img = fs.readFileSync(req.url.replace('/img', path)); + const img = fs.readFileSync(req.url.replace('/img', pathR)); res.writeHead(200, {'Content-Type': 'image/gif'}); res.end(img, 'binary'); } @@ -88,16 +95,42 @@ app.get('/', (req, res) => { res.send(data); }); }); -app.get('/edit', (req, res) => { +app.get('/edit/?*', (req, res) => { console.log(`[${new Date()}] > ${200} - ${req.url}`); - fs.readFile('public/edit.html', 'utf8', (err, data) => { + + const pathReq = path.normalize(`md/${req.url.slice(5)}`); + + console.log(`[${new Date()}] > ${200} - ${pathReq}`); + + fs.readFile('public/edit.html', 'utf8', (err, d) => { if (err) { return console.error(err); } - res.send(data); + res.send(d); }); }); +app.put('/edit/?*', (req, res) => { + const pathR = path.normalize(`md${req.url.slice(5)}`); + console.log('PUT !'); + console.log(pathR); + if (pathR.startsWith('md/public') || + (pathR.startsWith('md/') && req.body.password === 'renard crétin')) { + fs.writeFile(pathR, req.body.text, err => { + if (err) { + res.status(404).send('Not found'); + return console.log(err); + } + }); + + res.send('Saved !'); + return; + } + + console.log("Send error !"); + res.status(500).send('Error'); +}); + app.get('*', (req, res) => { console.error(`[${new Date()}] > ${404} - ${req.url}`); fs.readFile('public/404.md', 'utf8', (err, data) => { |