veza/apps/web/src/utils/cdn.ts
senke 0343feab34 feat(web): add CDN support for assets and audio
- Add VITE_CDN_URL, VITE_CDN_ENABLED to .env.example
- Create getAssetURL, getAudioURL in utils/cdn.ts
- Use getAudioURL in hlsService for HLS stream URLs
2026-02-14 22:44:06 +01:00

41 lines
1.3 KiB
TypeScript

/**
* CDN URL helpers for assets and audio.
* When VITE_CDN_ENABLED is true, URLs are rewritten to use the CDN base URL.
* Otherwise, the original URL/path is returned.
*/
const CDN_URL = import.meta.env.VITE_CDN_URL ?? '';
const CDN_ENABLED = import.meta.env.VITE_CDN_ENABLED === 'true' || import.meta.env.VITE_CDN_ENABLED === '1';
/**
* Get CDN URL for a static asset (images, fonts, etc.)
*/
export function getAssetURL(assetType: string, filename: string): string {
if (!CDN_ENABLED || !CDN_URL) {
return `/${assetType}/${filename}`;
}
const base = CDN_URL.replace(/\/$/, '');
return `${base}/assets/${assetType}/${filename}`;
}
/**
* Get CDN URL for an audio file or stream path.
* When CDN disabled, returns urlOrPath as-is.
* When CDN enabled, rewrites to CDN base (for paths or full URLs).
*/
export function getAudioURL(urlOrPath: string): string {
if (!CDN_ENABLED || !CDN_URL) {
return urlOrPath;
}
const base = CDN_URL.replace(/\/$/, '');
if (urlOrPath.startsWith('http://') || urlOrPath.startsWith('https://')) {
try {
const u = new URL(urlOrPath);
return `${base}${u.pathname}${u.search}`;
} catch {
return urlOrPath;
}
}
const path = urlOrPath.startsWith('/') ? urlOrPath : `/${urlOrPath}`;
return `${base}${path}`;
}