lbry-desktop/ui/util/lbrytv.js
2020-03-17 18:25:49 +01:00

43 lines
1.3 KiB
JavaScript

const { Lbryio } = require('lbryinc');
const { URL, LBRY_TV_STREAMING_API } = require('../../config');
const { getCookie, setCookie } = require('../../ui/util/saved-passwords');
const CONTINENT_COOKIE = 'continent';
function generateStreamUrl(claimName, claimId, apiUrl) {
let prefix = LBRY_TV_STREAMING_API || apiUrl;
const continent = getCookie(CONTINENT_COOKIE);
if (continent && prefix.split('//').length > 1) {
prefix = prefix.replace('//', '//' + continent + '.');
} else {
Lbryio.call('locale', 'get', {}, 'post').then(result => {
const userContinent = getSupportedCDN(result.continent);
setCookie(CONTINENT_COOKIE, userContinent, 1);
});
}
return `${prefix}/content/claims/${claimName}/${claimId}/stream`;
}
function getSupportedCDN(continent) {
switch (continent) {
case 'NA':
case 'EU':
return continent;
default:
return 'NA';
}
}
function generateEmbedUrl(claimName, claimId) {
return `${URL}/$/embed/${claimName}/${claimId}`;
}
function generateDownloadUrl(claimName, claimId, apiUrl) {
const streamUrl = generateStreamUrl(claimName, claimId, apiUrl);
return `${streamUrl}?download=1`;
}
// module.exports needed since the web server imports this function
module.exports = { generateStreamUrl, generateEmbedUrl, generateDownloadUrl };