2019-02-05 16:27:36 +00:00
|
|
|
const express = require('express')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs')
|
|
|
|
const fsp = fs.promises
|
|
|
|
|
|
|
|
const router = express.Router()
|
|
|
|
const cfgLoader = require(path.join('..', '..', 'config-loader'))(path.join(__dirname, 'config.json'), {
|
|
|
|
root: path.join(process.cwd(), '..', 'fiche', 'code'),
|
2019-02-26 09:52:23 +00:00
|
|
|
style: 'tomorrow-night',
|
|
|
|
index: 'index.html'
|
2019-02-05 16:27:36 +00:00
|
|
|
})
|
|
|
|
|
2020-09-13 17:24:36 +00:00
|
|
|
function encodeSpecial (string) {
|
2020-01-06 20:38:44 +00:00
|
|
|
let i = string.length
|
2020-09-13 17:24:36 +00:00
|
|
|
const a = []
|
2020-01-06 20:38:44 +00:00
|
|
|
|
2020-09-13 17:24:36 +00:00
|
|
|
while (i--) {
|
2020-01-06 20:38:44 +00:00
|
|
|
var iC = string[i].charCodeAt()
|
2020-09-13 17:24:36 +00:00
|
|
|
if (iC < 65 || iC > 127 || (iC > 90 && iC < 97)) {
|
2020-01-06 20:38:44 +00:00
|
|
|
a[i] = '&#' + iC + ';'
|
2020-09-13 17:24:36 +00:00
|
|
|
} else {
|
2020-01-06 20:38:44 +00:00
|
|
|
a[i] = string[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.join('')
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:27:36 +00:00
|
|
|
async function init () {
|
2020-09-13 17:24:36 +00:00
|
|
|
const config = await cfgLoader
|
|
|
|
const root = path.resolve(config.root)
|
2019-02-05 16:27:36 +00:00
|
|
|
|
|
|
|
await fsp.access(root, fs.constants.F_OK)
|
|
|
|
|
|
|
|
router.get('/:name', async (req, res, next) => {
|
2020-09-13 17:24:36 +00:00
|
|
|
const name = req.params.name
|
2019-02-05 16:47:08 +00:00
|
|
|
if (name.length > 5) {
|
2019-02-05 16:27:36 +00:00
|
|
|
try {
|
2020-09-13 17:24:36 +00:00
|
|
|
const text = await fsp.readFile(path.join(root, 'index.html'))
|
2019-02-05 16:27:36 +00:00
|
|
|
res.send(text)
|
|
|
|
} catch (e) {
|
|
|
|
res.status(403)
|
|
|
|
}
|
|
|
|
return res.end()
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:24:36 +00:00
|
|
|
const fichePath = path.join(root, name, 'index.txt')
|
|
|
|
const ua = req.get('User-Agent')
|
2019-02-05 16:27:36 +00:00
|
|
|
let reqRaw = req.query.raw != null
|
2019-08-06 12:35:01 +00:00
|
|
|
|
|
|
|
if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true
|
2019-02-05 16:27:36 +00:00
|
|
|
|
2020-09-13 17:24:36 +00:00
|
|
|
let text
|
2019-02-05 16:27:36 +00:00
|
|
|
try {
|
2020-09-13 17:24:36 +00:00
|
|
|
text = await fsp.readFile(fichePath, { encoding: 'utf8' })
|
2019-02-05 16:27:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(404).end()
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:15:00 +00:00
|
|
|
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
|
|
|
|
|
2019-02-05 16:27:36 +00:00
|
|
|
if (reqRaw) {
|
|
|
|
return res.set('Content-Type', 'text/plain').send(text)
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:24:36 +00:00
|
|
|
const etext = encodeSpecial(text)
|
|
|
|
const payload = `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html class="hljs">
|
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" href="/assets/css/t.css" />
|
|
|
|
<link rel="stylesheet" href="/assets/css/t-styles/${config.style}.css" />
|
|
|
|
<script src="/assets/js/highlight.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<pre><code>${etext}</code></pre>
|
|
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
res.set('Content-Type', 'text/html; charset=UTF-8').send(payload)
|
2019-02-05 16:27:36 +00:00
|
|
|
})
|
|
|
|
|
2019-02-26 09:52:23 +00:00
|
|
|
router.get('/', (req, res, next) => {
|
|
|
|
if (!config.index) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:47 +00:00
|
|
|
let inp = config.index
|
|
|
|
if (inp.indexOf('/') !== 0) {
|
|
|
|
inp = path.join(__dirname, inp)
|
2019-02-26 09:52:23 +00:00
|
|
|
} else {
|
2019-02-26 10:06:47 +00:00
|
|
|
inp = path.resolve(inp)
|
2019-02-26 09:52:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 10:06:47 +00:00
|
|
|
res.sendFile(inp)
|
2019-02-26 09:52:23 +00:00
|
|
|
})
|
|
|
|
|
2019-02-05 16:27:36 +00:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = init
|