Browse Source

[refactor]

pull/1/head
Shashank Kaul 5 years ago
parent
commit
7a694069ff
  1. 27
      routes/index.js
  2. 58
      routes/magnet.js

27
routes/index.js

@ -4,7 +4,7 @@ const router = express.Router();
const db = require('../modules/db'); const db = require('../modules/db');
const magnet = require('./magnet'); const magnet = require('./magnet');
template = (magnet, meta) => (` const foundTemplate = (magnet, meta) => (`
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html> <html>
@ -22,7 +22,22 @@ template = (magnet, meta) => (`
</html> </html>
`) `)
router.get('/:shortlink', (req, res, next) => { const notFoundTemplate = () => (`
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Not Found</title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<h1 style="text-align: center;"> Oops! Nothing found here.</h1>
</body>
</html>
`)
router.get('/:shortlink', (req, res) => {
db.get(req.params.shortlink) db.get(req.params.shortlink)
.then(record => { .then(record => {
const r = String(record); const r = String(record);
@ -30,16 +45,16 @@ router.get('/:shortlink', (req, res, next) => {
const meta = { const meta = {
title: title ? '⚡ ' + title : '⚡ :magnet:' title: title ? '⚡ ' + title : '⚡ :magnet:'
} }
res.send(template(magnet, meta)) res.send(foundTemplate(magnet, meta))
}) })
.catch(e => { .catch(e => {
console.log(`[ERR!] Occured while retrieving shortlink`, e.stack); console.log(`[ERR!] Occured while retrieving shortlink`, e.stack);
res.end('Invalid shortlink') res.send(notFoundTemplate())
});
}); });
})
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function (req, res) {
res.render('index', { title: ':magnet: ⚡️' }); res.render('index', { title: ':magnet: ⚡️' });
}); });

58
routes/magnet.js

@ -3,47 +3,59 @@ const router = express.Router();
const db = require('../modules/db'); const db = require('../modules/db');
const createMagnet = (uri, title, baseUrl, ctx) => { const handleSuccess = ({ res, baseUrl, key }) => res.json({ status: 'OK', uri: baseUrl + '/' + key })
const key = Math.random().toString(36).slice(4);
if (!uri.match(/magnet:\?xt=urn:.*/i)) { const handleError = ({ res, uri }, err) => {
switch (err.message) {
case "ERR_NO_URI": {
console.log(`[ERR!] Invalid request ${uri}`);
return res.status(400).json({
err: 'No magnet URI found'
});
}
case "ERR_INVALID_URI": {
console.log(`[ERR!] Invalid request ${uri}`); console.log(`[ERR!] Invalid request ${uri}`);
ctx.res.status(400); return res.status(400).json({
ctx.res.json({
err: 'Invalid magnet URI' err: 'Invalid magnet URI'
}); });
return;
} }
default: {
const value = title ? uri + '@@title@@' + title : uri;
db.put(key, value)
.then(() => ctx.res.json({
status: 'OK',
uri: baseUrl + '/' + key
}))
.catch(e => {
console.log(`[ERR!] Occured while creating new shortlink`, e.stack); console.log(`[ERR!] Occured while creating new shortlink`, e.stack);
ctx.res.status(520); return res.status(520).json({
ctx.res.json({
err: 'Unknown err! Please try again' err: 'Unknown err! Please try again'
}); });
}) }
}
}
const createMagnet = (uri, title, baseUrl, res) => {
if (!uri)
return Promise.reject(new Error("ERR_NO_URI"));
if (!uri.match(/magnet:\?xt=urn:.*/i))
return Promise.reject(new Error("ERR_INVALID_URI"));
const key = Math.random().toString(36).slice(4);
const value = title ? uri + '@@title@@' + title : uri;
return db.put(key, value)
.then(() => handleSuccess({ res, baseUrl, key }))
.catch(handleError.bind(undefined, { res, uri }));
} }
router.get('/:body', (req, res, next) => { router.get('/:body', (req, res) => {
const body = req.originalUrl.split('/api/')[1]; const body = req.originalUrl.split('/api/')[1];
const [uri, title] = body.split('@@title@@'); const [uri, title] = body.split('@@title@@');
const baseUrl = req.protocol + '://' + req.get('host'); const baseUrl = req.protocol + '://' + req.get('host');
return createMagnet(uri, title, baseUrl, { req, res }); return createMagnet(uri, title, baseUrl, res);
}) });
/* POST magnet link. */ /* POST magnet link. */
router.post('/', (req, res, next) => { router.post('/', (req, res) => {
const { uri, title } = req.body; const { uri, title } = req.body;
const baseUrl = req.protocol + '://' + req.get('host'); const baseUrl = req.protocol + '://' + req.get('host');
return createMagnet(uri, title, baseUrl, { req, res }); return createMagnet(uri, title, baseUrl, res);
}); });
module.exports = router; module.exports = router;
Loading…
Cancel
Save