From adddb844178a3bf0a2ee72411219bcd5961047db Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 27 Dec 2023 23:39:32 +0100 Subject: [PATCH] Add cdn replacements --- src/components/player/display/base.ts | 7 ++++--- src/components/player/display/chromecast.ts | 3 ++- src/setup/config.ts | 7 +++++++ src/utils/cdn.ts | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/utils/cdn.ts diff --git a/src/components/player/display/base.ts b/src/components/player/display/base.ts index ad333358..7a9ce041 100644 --- a/src/components/player/display/base.ts +++ b/src/components/player/display/base.ts @@ -12,6 +12,7 @@ import { SourceQuality, getPreferredQuality, } from "@/stores/player/utils/qualities"; +import { processCdnLink } from "@/utils/cdn"; import { canChangeVolume, canFullscreen, @@ -101,7 +102,7 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { function setupSource(vid: HTMLVideoElement, src: LoadableSource) { if (src.type === "hls") { if (canPlayHlsNatively(vid)) { - vid.src = src.url; + vid.src = processCdnLink(src.url); vid.currentTime = startAt; return; } @@ -151,12 +152,12 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { } hls.attachMedia(vid); - hls.loadSource(src.url); + hls.loadSource(processCdnLink(src.url)); vid.currentTime = startAt; return; } - vid.src = src.url; + vid.src = processCdnLink(src.url); vid.currentTime = startAt; } diff --git a/src/components/player/display/chromecast.ts b/src/components/player/display/chromecast.ts index ecf59a3d..dbaab0df 100644 --- a/src/components/player/display/chromecast.ts +++ b/src/components/player/display/chromecast.ts @@ -8,6 +8,7 @@ import { DisplayMeta, } from "@/components/player/display/displayInterface"; import { LoadableSource } from "@/stores/player/utils/qualities"; +import { processCdnLink } from "@/utils/cdn"; import { canChangeVolume, canFullscreen, @@ -112,7 +113,7 @@ export function makeChromecastDisplayInterface( metaData.title = meta.title; const mediaInfo = new chrome.cast.media.MediaInfo("video", type); - (mediaInfo as any).contentUrl = source.url; + (mediaInfo as any).contentUrl = processCdnLink(source.url); mediaInfo.streamType = chrome.cast.media.StreamType.BUFFERED; mediaInfo.metadata = metaData; mediaInfo.customData = { diff --git a/src/setup/config.ts b/src/setup/config.ts index 2f16714b..998383c8 100644 --- a/src/setup/config.ts +++ b/src/setup/config.ts @@ -18,6 +18,7 @@ interface Config { BACKEND_URL: string; DISALLOWED_IDS: string; TURNSTILE_KEY: string; + CDN_REPLACEMENTS: string; } export interface RuntimeConfig { @@ -32,6 +33,7 @@ export interface RuntimeConfig { BACKEND_URL: string; DISALLOWED_IDS: string[]; TURNSTILE_KEY: string | null; + CDN_REPLACEMENTS: Array; } const env: Record = { @@ -46,6 +48,7 @@ const env: Record = { BACKEND_URL: import.meta.env.VITE_BACKEND_URL, DISALLOWED_IDS: import.meta.env.VITE_DISALLOWED_IDS, TURNSTILE_KEY: import.meta.env.VITE_TURNSTILE_KEY, + CDN_REPLACEMENTS: import.meta.env.VITE_CDN_REPLACEMENTS, }; // loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js) @@ -84,5 +87,9 @@ export function conf(): RuntimeConfig { .split(",") .map((v) => v.trim()) .filter((v) => v.length > 0), // Should be comma-seperated and contain the media type and ID, formatted like so: movie-753342,movie-753342,movie-753342 + CDN_REPLACEMENTS: getKey("CDN_REPLACEMENTS", "") + .split(",") + .map((v) => v.split(":").map((s) => s.trim())) + .filter((v) => v.length > 0), // The format is :,: }; } diff --git a/src/utils/cdn.ts b/src/utils/cdn.ts new file mode 100644 index 00000000..bf87fb27 --- /dev/null +++ b/src/utils/cdn.ts @@ -0,0 +1,14 @@ +import { conf } from "@/setup/config"; + +export function processCdnLink(url: string): string { + const parsedUrl = new URL(url); + const replacements = conf().CDN_REPLACEMENTS; + for (const [before, after] of replacements) { + if (parsedUrl.hostname.endsWith(before)) { + parsedUrl.host = after; + return parsedUrl.toString(); + } + } + + return url; +}