diff --git a/ui/util/lbrytv.js b/ui/util/lbrytv.js index f2e0387a5..6163a17ed 100644 --- a/ui/util/lbrytv.js +++ b/ui/util/lbrytv.js @@ -1,10 +1,35 @@ +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) { - const prefix = LBRY_TV_STREAMING_API || 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}`; } diff --git a/ui/util/saved-passwords.js b/ui/util/saved-passwords.js index bc7946daf..3393098e9 100644 --- a/ui/util/saved-passwords.js +++ b/ui/util/saved-passwords.js @@ -10,7 +10,7 @@ const isProduction = process.env.NODE_ENV === 'production'; const maxExpiration = 2147483647; let sessionPassword; -function setCookie(name: string, value: string, expirationDaysOnWeb: number) { +export function setCookie(name: string, value: string, expirationDaysOnWeb: number) { let expires = ''; if (expirationDaysOnWeb) { let date = new Date(); @@ -27,7 +27,7 @@ function setCookie(name: string, value: string, expirationDaysOnWeb: number) { document.cookie = cookie; } -function getCookie(name: string) { +export function getCookie(name: string) { const nameEQ = name + '='; const cookies = document.cookie.split(';');