From cc6a20f8678a4b71c54819464c03e1787d27d7d8 Mon Sep 17 00:00:00 2001 From: Shashank Kaul Date: Sat, 30 Nov 2019 23:06:26 +0530 Subject: [PATCH 1/5] [create] Added form to index template. --- public/index.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/public/index.html b/public/index.html index b273ab0..d7c2dc7 100644 --- a/public/index.html +++ b/public/index.html @@ -7,6 +7,12 @@

⚡ :magnet:

+
+

URI:

+

Title (optional):

+ +
+

POST to /api with this request body:

       {

From 7a694069ffc289a5dd320b42128991b7439e0dce Mon Sep 17 00:00:00 2001
From: Shashank Kaul 
Date: Sat, 30 Nov 2019 23:06:41 +0530
Subject: [PATCH 2/5] [refactor]

---
 routes/index.js  | 29 ++++++++++++++++++------
 routes/magnet.js | 68 +++++++++++++++++++++++++++++++++-----------------------
 2 files changed, 62 insertions(+), 35 deletions(-)

diff --git a/routes/index.js b/routes/index.js
index 9c18c01..02a20a0 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -4,7 +4,7 @@ const router = express.Router();
 const db = require('../modules/db');
 const magnet = require('./magnet');
 
-template = (magnet, meta) => (`
+const foundTemplate = (magnet, meta) => (`
 
 
 
@@ -22,24 +22,39 @@ template = (magnet, meta) => (`
 
 `)
 
-router.get('/:shortlink', (req, res, next) => {
+const notFoundTemplate = () => (`
+
+
+
+
+	
+	Not Found
+	
+
+
+	

⚡ Oops! Nothing found here.

+ + +`) + +router.get('/:shortlink', (req, res) => { db.get(req.params.shortlink) .then(record => { const r = String(record); - const [ magnet, title ] = r.split('@@title@@'); + const [magnet, title] = r.split('@@title@@'); const meta = { title: title ? '⚡ ' + title : '⚡ :magnet:' } - res.send(template(magnet, meta)) + res.send(foundTemplate(magnet, meta)) }) .catch(e => { console.log(`[ERR!] Occured while retrieving shortlink`, e.stack); - res.end('Invalid shortlink') + res.send(notFoundTemplate()) }); -}) +}); /* GET home page. */ -router.get('/', function(req, res, next) { +router.get('/', function (req, res) { res.render('index', { title: ':magnet: ⚡️' }); }); diff --git a/routes/magnet.js b/routes/magnet.js index 9b01af6..b514719 100644 --- a/routes/magnet.js +++ b/routes/magnet.js @@ -3,47 +3,59 @@ const router = express.Router(); const db = require('../modules/db'); -const createMagnet = (uri, title, baseUrl, ctx) => { - const key = Math.random().toString(36).slice(4); - if (!uri.match(/magnet:\?xt=urn:.*/i)) { - console.log(`[ERR!] Invalid request ${uri}`); - ctx.res.status(400); - ctx.res.json({ - err: 'Invalid magnet URI' - }); - return; - } - - const value = title ? uri + '@@title@@' + title : uri; - - db.put(key, value) - .then(() => ctx.res.json({ - status: 'OK', - uri: baseUrl + '/' + key - })) - .catch(e => { +const handleSuccess = ({ res, baseUrl, key }) => res.json({ status: 'OK', uri: baseUrl + '/' + key }) + +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}`); + return res.status(400).json({ + err: 'Invalid magnet URI' + }); + } + default: { console.log(`[ERR!] Occured while creating new shortlink`, e.stack); - ctx.res.status(520); - ctx.res.json({ + return res.status(520).json({ err: 'Unknown err! Please try again' }); - }) + } + } } -router.get('/:body', (req, res, next) => { +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) => { 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'); - return createMagnet(uri, title, baseUrl, { req, res }); -}) + return createMagnet(uri, title, baseUrl, res); +}); /* POST magnet link. */ -router.post('/', (req, res, next) => { +router.post('/', (req, res) => { const { uri, title } = req.body; const baseUrl = req.protocol + '://' + req.get('host'); - return createMagnet(uri, title, baseUrl, { req, res }); + return createMagnet(uri, title, baseUrl, res); }); module.exports = router; \ No newline at end of file From 45d0a8fbdcbae5abbb69bdaa643675eb0b803d96 Mon Sep 17 00:00:00 2001 From: Shashank Kaul Date: Sat, 30 Nov 2019 23:13:01 +0530 Subject: [PATCH 3/5] package-lock update --- package-lock.json | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index f14150d..daca0fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "magnet", - "version": "0.0.0", + "name": "@codefeathers/magnet", + "version": "0.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -138,15 +138,6 @@ "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" }, - "cookie-parser": { - "version": "1.4.3", - "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.3.tgz", - "integrity": "sha1-D+MfoZ0AC5X0qt8fU/3CuKIDuqU=", - "requires": { - "cookie": "0.3.1", - "cookie-signature": "1.0.6" - } - }, "cookie-signature": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", From b16985e15cd376b9e30d36facbc7699d28b565ed Mon Sep 17 00:00:00 2001 From: Shashank Kaul Date: Sun, 1 Dec 2019 02:08:54 +0530 Subject: [PATCH 4/5] [magnet] Added title to shortlink html --- routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/index.js b/routes/index.js index 02a20a0..a6464a3 100644 --- a/routes/index.js +++ b/routes/index.js @@ -15,7 +15,7 @@ const foundTemplate = (magnet, meta) => (` -

⚡ :magnet:

+

${meta.title}

${magnet}

From d911d54238d28d4e97348ffb558dd119bd6b14f6 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 11 Feb 2020 13:34:33 +0530 Subject: [PATCH 5/5] (fix) title --- routes/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/index.js b/routes/index.js index a6464a3..fdd53f6 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,7 +10,7 @@ const foundTemplate = (magnet, meta) => (` - ${meta.title} + ⚡ ${meta.title}