|
@ -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; |