lbry-desktop/lbrytv/middleware/redirect.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-11-07 20:39:22 +01:00
const config = require('../../config');
const PAGES = require('../../ui/constants/pages');
2019-12-03 18:55:29 +01:00
const { formatInAppUrl } = require('../../ui/util/url');
const { parseURI } = require('lbry-redux');
2019-11-07 20:39:22 +01:00
async function redirectMiddleware(ctx, next) {
const requestHost = ctx.host;
const path = ctx.path;
const url = ctx.url;
2020-01-10 04:18:23 +01:00
// Getting err: too many redirects on some urls because of this
// Need a better solution
// const decodedUrl = decodeURIComponent(url);
// if (decodedUrl !== url) {
// ctx.redirect(decodedUrl);
// return;
// }
2019-11-07 20:39:22 +01:00
2019-11-11 19:19:31 +01:00
if (path.endsWith('/') && path.length > 1) {
2019-11-07 20:39:22 +01:00
ctx.redirect(url.replace(/\/$/, ''));
return;
}
if (!path.startsWith('/$/') && path.match(/^([^@/:]+)\/([^:/]+)$/)) {
ctx.redirect(url.replace(/^([^@/:]+)\/([^:/]+)(:(\/.*))/, '$1:$2')); // test against path, but use ctx.url to retain parameters
return;
}
if (requestHost === 'open.lbry.com' || requestHost === 'open.lbry.io') {
const openQuery = '?src=open';
2019-12-03 18:55:29 +01:00
let redirectUrl = config.URL + formatInAppUrl(url, openQuery);
if (redirectUrl.includes('?')) {
redirectUrl = redirectUrl.replace('?', `${openQuery}&`);
} else {
redirectUrl += openQuery;
}
2019-11-07 20:39:22 +01:00
ctx.redirect(redirectUrl);
return;
}
2020-01-09 01:26:36 +01:00
if (path === '/') {
ctx.redirect(`/$/${PAGES.CHANNELS_FOLLOWING}`);
return;
}
2019-11-07 20:39:22 +01:00
// No redirects needed
await next();
}
module.exports = redirectMiddleware;