92 lines
3.5 KiB
JavaScript
92 lines
3.5 KiB
JavaScript
// Server images (group/channel icons, channel banners, host banner, avatars) as blob: URLs.
|
|
// Bytes come from the core's file transfer (or main's allowlisted http(s) fetch); MIME is sniffed
|
|
// upstream and re-checked here. Failures are cached as null so a missing file is not refetched.
|
|
import { tp, call, log } from './core.js';
|
|
|
|
const ALLOWED = new Set(['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'image/x-icon', 'image/svg+xml']);
|
|
const cache = new Map();
|
|
|
|
function toUrl(result) {
|
|
if (!result || !ALLOWED.has(result.mime) || typeof result.data !== 'string') return null;
|
|
const bin = atob(result.data);
|
|
const bytes = new Uint8Array(bin.length);
|
|
for (let i = 0; i < bin.length; i += 1) bytes[i] = bin.charCodeAt(i);
|
|
return URL.createObjectURL(new Blob([bytes], { type: result.mime }));
|
|
}
|
|
|
|
function cached(key, load) {
|
|
if (!cache.has(key)) {
|
|
cache.set(key, load().then(toUrl).catch((err) => {
|
|
if (err.code !== 'not_found') log('info', `media ${key.split(':')[0]} unavailable: ${err.code ?? err.message}`);
|
|
return null;
|
|
}));
|
|
}
|
|
return cache.get(key);
|
|
}
|
|
|
|
async function viaCore(cmd, args) {
|
|
try {
|
|
return await call(cmd, args);
|
|
} catch (err) {
|
|
if (err.code === 'external_media' && err.detail && typeof err.detail.url === 'string') {
|
|
const r = await tp.fetchExternalMedia(err.detail.url);
|
|
if (r && r.ok) return r.result;
|
|
const e = new Error(r?.error?.message ?? 'external fetch failed');
|
|
e.code = r?.error?.code ?? 'io';
|
|
throw e;
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/** TeamSpeak's built-in group icon ids (bridge §4.16): the server has no file for them. */
|
|
const BUILTIN_ICONS = new Set([100, 200, 300, 500, 600]);
|
|
|
|
export function isBuiltinIcon(id) {
|
|
return BUILTIN_ICONS.has(id);
|
|
}
|
|
|
|
export function iconUrl(iconId) {
|
|
if (!iconId || isBuiltinIcon(iconId)) return Promise.resolve(null);
|
|
return cached(`icon:${iconId}`, () => call('fetch_icon', { icon_id: iconId }));
|
|
}
|
|
|
|
export function channelBannerUrl(channel) {
|
|
if (!channel.banner_url) return Promise.resolve(null);
|
|
return cached(`banner:${channel.id}:${channel.banner_url}`, () => viaCore('fetch_banner', { channel_id: channel.id }));
|
|
}
|
|
|
|
export function serverBannerUrl(server) {
|
|
if (!server?.hostbanner_gfx_url) return Promise.resolve(null);
|
|
return cached(`hostbanner:${server.hostbanner_gfx_url}`, () => viaCore('fetch_server_banner'));
|
|
}
|
|
|
|
export function avatarUrl(client) {
|
|
if (client.avatar_hash) return cached(`avatar:${client.uid}:${client.avatar_hash}`, () => call('fetch_avatar', { client_id: client.id }));
|
|
const myts = mytsAvatar(client);
|
|
if (myts) {
|
|
return cached(`myts:${myts}`, async () => {
|
|
const r = await tp.fetchExternalMedia(myts);
|
|
if (r && r.ok) return r.result;
|
|
const e = new Error(r?.error?.message ?? 'external fetch failed');
|
|
e.code = r?.error?.code ?? 'io';
|
|
throw e;
|
|
});
|
|
}
|
|
return Promise.resolve(null);
|
|
}
|
|
|
|
/** myTeamSpeak avatar URL (bridge: myts_avatars[{variant,url}]); the highest variant observed is used. */
|
|
function mytsAvatar(client) {
|
|
if (!Array.isArray(client.myts_avatars)) return null;
|
|
const list = client.myts_avatars.filter((v) => v && typeof v.url === 'string' && v.url.startsWith('https://'));
|
|
list.sort((a, b) => (b.variant ?? 0) - (a.variant ?? 0));
|
|
return list[0]?.url ?? null;
|
|
}
|
|
|
|
/** Forget everything (new server or disconnect) and release blob URLs. */
|
|
export function clearMedia() {
|
|
for (const p of cache.values()) p.then((url) => url && URL.revokeObjectURL(url));
|
|
cache.clear();
|
|
}
|