encoding, url shortener works, kinda
This commit is contained in:
parent
8ec4dc09ba
commit
83289e13d9
@ -10,6 +10,21 @@ const cfgLoader = require(path.join('..', '..', 'config-loader'))(path.join(__di
|
|||||||
index: 'index.html'
|
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 () {
|
async function init () {
|
||||||
let config = await cfgLoader
|
let config = await cfgLoader
|
||||||
let root = path.resolve(config.root)
|
let root = path.resolve(config.root)
|
||||||
@ -47,7 +62,7 @@ async function init () {
|
|||||||
return res.set('Content-Type', 'text/plain').send(text)
|
return res.set('Content-Type', 'text/plain').send(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
let etext = text.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
|
let etext = encodeSpecial(text)
|
||||||
let payload =
|
let payload =
|
||||||
'<?xml version="1.0" encoding="UTF-8"?>' +
|
'<?xml version="1.0" encoding="UTF-8"?>' +
|
||||||
'<html xmlns="http://www.w3.org/1999/xhtml" class="hljs">' +
|
'<html xmlns="http://www.w3.org/1999/xhtml" class="hljs">' +
|
||||||
|
@ -70,25 +70,6 @@ async function init () {
|
|||||||
|
|
||||||
await clearDatabase(config, dbPromise)
|
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
|
// Upload a file or publish a hash
|
||||||
router.post('/publish', async (req, res, next) => {
|
router.post('/publish', async (req, res, next) => {
|
||||||
let ip = req.ip
|
let ip = req.ip
|
||||||
@ -160,13 +141,18 @@ async function init () {
|
|||||||
res.send(uploadedFiles.join('\n'))
|
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) => {
|
router.post('/shorten', async (req, res) => {
|
||||||
let ip = req.ip
|
let ip = req.ip
|
||||||
let url = req.body.url
|
let url = req.body.url
|
||||||
|
|
||||||
// Simple URL validator
|
// Simple URL validator
|
||||||
try {
|
try {
|
||||||
let a = new URL(url)
|
let a = new URL.URL(url)
|
||||||
if (a.protocol.indexOf('http') !== 0 && a.protocol.indexOf('ftp') !== -1) {
|
if (a.protocol.indexOf('http') !== 0 && a.protocol.indexOf('ftp') !== -1) {
|
||||||
throw new Error('Unsupported protocol')
|
throw new Error('Unsupported protocol')
|
||||||
}
|
}
|
||||||
@ -200,10 +186,6 @@ async function init () {
|
|||||||
res.send(resp)
|
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) => {
|
router.get('/shorten/:hash', async (req, res) => {
|
||||||
let hash = req.params.hash
|
let hash = req.params.hash
|
||||||
let db = await dbPromise
|
let db = await dbPromise
|
||||||
@ -212,6 +194,25 @@ async function init () {
|
|||||||
res.redirect(get.url)
|
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
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,10 +10,10 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"license": "Unlicense",
|
"license": "Unlicense",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.18.3",
|
"body-parser": "^1.19.0",
|
||||||
"express": "^4.16.4",
|
"express": "^4.17.1",
|
||||||
"express-async-errors": "^3.1.1",
|
"express-async-errors": "^3.1.1",
|
||||||
"multiparty": "^4.2.1",
|
"multiparty": "^4.2.1",
|
||||||
"sqlite": "^3.0.1"
|
"sqlite": "^3.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user