Files
gerbeur/src/utils/urls.ts
khannurien 76de798faf
Some checks failed
Build and Publish Docker Image / build-and-push (push) Failing after 20s
v3: fixed authorization bug with stale tokens, fixed private dump access bug, various small fixes across the app
2026-06-27 10:40:07 +00:00

40 lines
1.3 KiB
TypeScript

import { API_URL } from "../config/api.ts";
/**
* Direct URL to a dump's uploaded file. The viewer's token is passed as a query
* param — media elements (`<audio>`, `<img>`, …) can't send an `Authorization`
* header — so the server can authorize access to private dumps. Same approach as
* the WebSocket connection (`?token=`).
*/
export function dumpFileUrl(
dump: { id: string; fileSize?: number },
token?: string | null,
): string {
const auth = token ? `&token=${encodeURIComponent(token)}` : "";
return `${API_URL}/api/files/${dump.id}?v=${dump.fileSize ?? 0}${auth}`;
}
/** Thumbnail URL for a dump, with the viewer's token (see {@link dumpFileUrl}). */
export function dumpThumbnailUrl(
dump: { id: string },
token?: string | null,
): string {
const auth = token ? `?token=${encodeURIComponent(token)}` : "";
return `${API_URL}/api/thumbnails/${dump.id}${auth}`;
}
export function normalizeUrl(input: string): string {
const s = input.trim();
if (!s || /^https?:\/\//i.test(s)) return s;
if (s.startsWith("//")) return `https:${s}`;
return `https://${s}`;
}
export function dumpUrl(dump: { id: string; slug?: string }): string {
return `/dumps/${dump.slug ?? dump.id}`;
}
export function playlistUrl(playlist: { id: string; slug?: string }): string {
return `/playlists/${playlist.slug ?? playlist.id}`;
}