From 83289e13d9727d047f17c1d4582db3c71791a087 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 6 Jan 2020 22:38:44 +0200 Subject: [PATCH] encoding, url shortener works, kinda --- applications/highlight-termbin/index.js | 17 ++++++++- applications/tempfiles/index.js | 49 +++++++++++++------------ package.json | 6 +-- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/applications/highlight-termbin/index.js b/applications/highlight-termbin/index.js index 34fa19a..2414ddb 100644 --- a/applications/highlight-termbin/index.js +++ b/applications/highlight-termbin/index.js @@ -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,'&').replace(//g,'>') + let etext = encodeSpecial(text) let payload = '' + '' + diff --git a/applications/tempfiles/index.js b/applications/tempfiles/index.js index 6d39ff7..a6716ea 100644 --- a/applications/tempfiles/index.js +++ b/applications/tempfiles/index.js @@ -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('

Basic URL Shortener

\ +
') + }) + 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('

Basic URL Shortener

') - }) - 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 } diff --git a/package.json b/package.json index 62dce8b..edd4e5a 100644 --- a/package.json +++ b/package.json @@ -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" } }