// Image cache service — cache statistics, limits and eviction. // // This module used to also export `getCachedImageUrl`, which built // `${serverUrl}/Items/${itemId}/Images/${imageType}` in the frontend. That was a // Jellyfin route in the presentation layer — domain logic by this project's own // litmus test (would it change if Jellyfin changed its API?) — and it was // **dead**: nothing outside this file and its test ever called it. The live path // is CachedImage.svelte -> commands.imageGetUrl -> Rust, which was already // correct. It was deleted rather than migrated (DR-285). // // Note for whoever touches the CSP next: that function was the last // `convertFileSrc` caller, so the asset-protocol grant narrowed to // `$APPDATA/thumbnails/**` under DR-198 now has no caller at all and is a // candidate for removal. Left in place here deliberately — dropping a capability // grant is a security change that deserves its own commit and its own testing on // Android, not a side effect of deleting dead code. // // TRACES: UR-007, UR-085 | DR-016, DR-285 import { commands } from "$lib/api/bindings"; /** * Statistics about the thumbnail cache */ export interface ImageCacheStats { totalSizeBytes: number; itemCount: number; limitBytes: number; } /** * Get thumbnail cache statistics */ export async function getCacheStats(): Promise { return commands.thumbnailGetStats(); } /** * Set cache storage limit in bytes * * @param limitBytes - The maximum cache size in bytes (0 = unlimited) */ export async function setCacheLimit(limitBytes: number): Promise { await commands.thumbnailSetLimit(limitBytes); } /** * Clear all cached thumbnails */ export async function clearCache(): Promise { await commands.thumbnailClearCache(); } /** * Delete cached thumbnails for a specific item * * @param itemId - The Jellyfin item ID */ export async function deleteItemCache(itemId: string): Promise { await commands.thumbnailDeleteItem(itemId); } /** * Format bytes to human-readable string * * @param bytes - Number of bytes * @returns Human-readable string (e.g., "1.5 GB") */ export function formatBytes(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`; } /** * Convert gigabytes to bytes */ export function gbToBytes(gb: number): number { return gb * 1024 * 1024 * 1024; } /** * Convert bytes to gigabytes */ export function bytesToGb(bytes: number): number { return bytes / (1024 * 1024 * 1024); }