encoding, url shortener works, kinda

This commit is contained in:
Evert Prants 2020-01-06 22:38:44 +02:00
parent 8ec4dc09ba
commit 83289e13d9
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 44 additions and 28 deletions

View File

@ -10,6 +10,21 @@ const cfgLoader = require(path.join('..', '..', 'config-loader'))(path.join(__di
index: 'index.html'
})
function encodeSpecial (string) {
let i = string.length
let a = []
while (i--) {
var iC = string[i].charCodeAt()
if (iC < 65 || iC > 127 || (iC > 90 && iC < 97)) {
a[i] = '&#' + iC + ';'
} else {
a[i] = string[i]
}
}
return a.join('')
}
async function init () {
let config = await cfgLoader
let root = path.resolve(config.root)
@ -47,7 +62,7 @@ async function init () {
return res.set('Content-Type', 'text/plain').send(text)
}
let etext = text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
let etext = encodeSpecial(text)
let payload =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<html xmlns="http://www.w3.org/1999/xhtml" class="hljs">' +

View File

@ -70,25 +70,6 @@ async function init () {
await clearDatabase(config, dbPromise)
// Serve a file or a hash
// Files should be served from an external web server (such as nginx) whenever possible.
router.get('/:hash', async (req, res, next) => {
if (!req.params.hash) return res.status(400).send('Invalid request')
let db = await dbPromise
let file = await db.get('SELECT * FROM File WHERE path = ?', req.params.hash)
let translation = await db.get('SELECT * FROM Translation WHERE translation = ?', req.params.hash)
if (!file && !translation) return res.status(404).end()
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
if (translation) {
return res.redirect(config.gateway + '/ipfs/' + translation.file_hash)
}
res.sendFile(path.join(root, file.path))
})
// Upload a file or publish a hash
router.post('/publish', async (req, res, next) => {
let ip = req.ip
@ -160,13 +141,18 @@ async function init () {
res.send(uploadedFiles.join('\n'))
})
router.get('/shorten', async (req, res) => {
res.send('<h1>Basic URL Shortener</h1><form action="" method="post"> \
<input type="url" name="url" placeholder="URL to shorten"/><input type="submit" value="Go!"/></form>')
})
router.post('/shorten', async (req, res) => {
let ip = req.ip
let url = req.body.url
// Simple URL validator
try {
let a = new URL(url)
let a = new URL.URL(url)
if (a.protocol.indexOf('http') !== 0 && a.protocol.indexOf('ftp') !== -1) {
throw new Error('Unsupported protocol')
}
@ -200,10 +186,6 @@ async function init () {
res.send(resp)
})
router.get('/shorten', (req, res) => {
res.send('<h1>Basic URL Shortener</h1><form action=""><input type="url" name="url" placeholder="URL to shorten"/><input type="submit" value="Go!"/></form>')
})
router.get('/shorten/:hash', async (req, res) => {
let hash = req.params.hash
let db = await dbPromise
@ -212,6 +194,25 @@ async function init () {
res.redirect(get.url)
})
// Serve a file or a hash
// Files should be served from an external web server (such as nginx) whenever possible.
router.get('/:hash', async (req, res, next) => {
if (!req.params.hash) return res.status(400).send('Invalid request')
let db = await dbPromise
let file = await db.get('SELECT * FROM File WHERE path = ?', req.params.hash)
let translation = await db.get('SELECT * FROM Translation WHERE translation = ?', req.params.hash)
if (!file && !translation) return res.status(404).end()
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
if (translation) {
return res.redirect(config.gateway + '/ipfs/' + translation.file_hash)
}
res.sendFile(path.join(root, file.path))
})
return router
}

View File

@ -10,10 +10,10 @@
"private": true,
"license": "Unlicense",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"multiparty": "^4.2.1",
"sqlite": "^3.0.1"
"sqlite": "^3.0.3"
}
}