2019-11-07 20:39:22 +01:00
|
|
|
const { getHtml } = require('./html');
|
2020-04-08 19:10:33 +02:00
|
|
|
const { Lbryio } = require('lbryinc/dist/bundle.es.js');
|
2020-03-25 22:49:14 +01:00
|
|
|
const { generateStreamUrl, CONTINENT_COOKIE } = require('../../ui/util/lbrytv');
|
2020-04-08 19:10:33 +02:00
|
|
|
const fetch = require('node-fetch');
|
2019-11-07 20:39:22 +01:00
|
|
|
const Router = require('@koa/router');
|
2019-11-11 19:19:31 +01:00
|
|
|
|
2020-04-08 19:10:33 +02:00
|
|
|
// So any code from 'lbry-redux'/'lbryinc' that uses `fetch` can be run on the server
|
|
|
|
global.fetch = fetch;
|
|
|
|
|
2019-11-07 20:39:22 +01:00
|
|
|
const router = new Router();
|
|
|
|
|
2020-03-27 19:57:03 +01:00
|
|
|
function getStreamUrl(ctx) {
|
2020-01-13 18:36:26 +01:00
|
|
|
const { claimName, claimId } = ctx.params;
|
2020-03-25 22:49:14 +01:00
|
|
|
|
|
|
|
// hack to get around how we managing the continent cookie
|
|
|
|
// defaulting to "NA" becasue saved-passwords.js assumes it's in the browser and won't work properly
|
|
|
|
// changes need to be made to that to better work with the server
|
|
|
|
const streamingContinentCookie = ctx.cookies.get(CONTINENT_COOKIE) || 'NA';
|
|
|
|
const streamUrl = generateStreamUrl(claimName, claimId, undefined, streamingContinentCookie);
|
2020-03-27 19:57:03 +01:00
|
|
|
return streamUrl;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:10:33 +02:00
|
|
|
function getSupportedCDN(continent) {
|
|
|
|
switch (continent) {
|
|
|
|
case 'NA':
|
2020-04-08 19:50:42 +02:00
|
|
|
case 'AS':
|
2020-04-08 19:10:33 +02:00
|
|
|
case 'EU':
|
|
|
|
return continent;
|
|
|
|
default:
|
|
|
|
return 'NA';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:47:06 +02:00
|
|
|
async function fetchContinentCookie(ip) {
|
|
|
|
return Lbryio.call('locale', 'get', { ip }, 'post').then(result => {
|
2020-04-08 19:10:33 +02:00
|
|
|
const userContinent = getSupportedCDN(result.continent);
|
|
|
|
return userContinent;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:57:03 +01:00
|
|
|
router.get(`/$/download/:claimName/:claimId`, async ctx => {
|
|
|
|
const streamUrl = getStreamUrl(ctx);
|
2020-03-25 22:49:14 +01:00
|
|
|
const downloadUrl = `${streamUrl}?download=1`;
|
2020-01-13 18:36:26 +01:00
|
|
|
ctx.redirect(downloadUrl);
|
|
|
|
});
|
|
|
|
|
2020-03-27 19:57:03 +01:00
|
|
|
router.get(`/$/stream/:claimName/:claimId`, async ctx => {
|
|
|
|
const streamUrl = getStreamUrl(ctx);
|
|
|
|
ctx.redirect(streamUrl);
|
|
|
|
});
|
|
|
|
|
2019-11-07 20:39:22 +01:00
|
|
|
router.get('*', async ctx => {
|
2020-04-08 19:10:33 +02:00
|
|
|
const hasContinentCookie = ctx.cookies.get(CONTINENT_COOKIE);
|
2020-04-08 22:47:06 +02:00
|
|
|
const ip = ctx.ip;
|
|
|
|
|
2020-04-08 19:10:33 +02:00
|
|
|
if (!hasContinentCookie) {
|
2020-04-08 22:47:06 +02:00
|
|
|
try {
|
|
|
|
const continentValue = await fetchContinentCookie(ip);
|
|
|
|
ctx.cookies.set(CONTINENT_COOKIE, continentValue, { httpOnly: false });
|
|
|
|
} catch (e) {}
|
2020-04-08 19:10:33 +02:00
|
|
|
}
|
2019-11-07 20:39:22 +01:00
|
|
|
const html = await getHtml(ctx);
|
|
|
|
ctx.body = html;
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|