feat(thumbnails): integrate thumbnail service in LocalLibrary
This commit is contained in:
1 parent
fc03bee9b1
commit
16817b72b8
2 files changed
+137
-1
No files matched your search
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { invoke, convertFileSrc } from '@tauri-apps/api/core';
|
||||
import { getThumbnailUrl, getCachedThumbnailUrl } from '$lib/services/thumbnailService';
|
||||
import { open, ask } from '@tauri-apps/plugin-dialog';
|
||||
import { onMount, onDestroy } from 'svelte';
|
||||
import {
|
||||
@@ -1405,10 +1406,34 @@
|
||||
|
||||
// Memoization cache for artwork URLs to avoid repeated convertFileSrc calls
|
||||
const artworkUrlCache = new Map<string, string>();
|
||||
// Thumbnail URL cache (separate from full-res cache)
|
||||
let thumbnailUrlCache = $state<Map<string, string>>(new Map());
|
||||
// Track pending thumbnail requests to avoid duplicates
|
||||
const pendingThumbnails = new Set<string>();
|
||||
|
||||
function getArtworkUrl(path?: string): string {
|
||||
if (!path) return '';
|
||||
|
||||
// For grid/list views, prefer thumbnails
|
||||
const cachedThumb = thumbnailUrlCache.get(path);
|
||||
if (cachedThumb) return cachedThumb;
|
||||
|
||||
// Start thumbnail generation in background if not already pending
|
||||
if (!pendingThumbnails.has(path)) {
|
||||
pendingThumbnails.add(path);
|
||||
getThumbnailUrl(path).then(thumbUrl => {
|
||||
pendingThumbnails.delete(path);
|
||||
// Update the reactive cache to trigger re-render
|
||||
thumbnailUrlCache = new Map(thumbnailUrlCache).set(path, thumbUrl);
|
||||
}).catch(() => {
|
||||
pendingThumbnails.delete(path);
|
||||
// On error, cache the original URL
|
||||
const fallbackUrl = convertFileSrc(path);
|
||||
thumbnailUrlCache = new Map(thumbnailUrlCache).set(path, fallbackUrl);
|
||||
});
|
||||
}
|
||||
|
||||
// Return full image while thumbnail loads
|
||||
const cached = artworkUrlCache.get(path);
|
||||
if (cached) return cached;
|
||||
|
||||
@@ -1417,6 +1442,16 @@
|
||||
return url;
|
||||
}
|
||||
|
||||
// For album detail view, always use full resolution
|
||||
function getFullArtworkUrl(path?: string): string {
|
||||
if (!path) return '';
|
||||
const cached = artworkUrlCache.get(path);
|
||||
if (cached) return cached;
|
||||
const url = convertFileSrc(path);
|
||||
artworkUrlCache.set(path, url);
|
||||
return url;
|
||||
}
|
||||
|
||||
function matchesAlbumSearch(album: LocalAlbum, query: string): boolean {
|
||||
const needle = query.trim().toLowerCase();
|
||||
if (!needle) return true;
|
||||
@@ -1938,7 +1973,7 @@
|
||||
<div class="album-header">
|
||||
<div class="album-artwork">
|
||||
{#if selectedAlbum.artwork_path}
|
||||
<img src={getArtworkUrl(selectedAlbum.artwork_path)} alt={selectedAlbum.title} />
|
||||
<img src={getFullArtworkUrl(selectedAlbum.artwork_path)} alt={selectedAlbum.title} />
|
||||
{:else}
|
||||
<div class="artwork-placeholder">
|
||||
<Disc3 size={64} />
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { invoke, convertFileSrc } from '@tauri-apps/api/core';
|
||||
|
||||
// Cache for thumbnail URLs (artwork_path -> thumbnail_url)
|
||||
const thumbnailCache = new Map<string, string>();
|
||||
|
||||
// Pending requests to avoid duplicate calls
|
||||
const pendingRequests = new Map<string, Promise<string>>();
|
||||
|
||||
/**
|
||||
* Get a thumbnail URL for an artwork file.
|
||||
* Generates the thumbnail on first request and caches subsequent calls.
|
||||
*/
|
||||
export async function getThumbnailUrl(artworkPath: string): Promise<string> {
|
||||
// Return cached URL if available
|
||||
if (thumbnailCache.has(artworkPath)) {
|
||||
return thumbnailCache.get(artworkPath)!;
|
||||
}
|
||||
|
||||
// If there's already a pending request for this artwork, wait for it
|
||||
if (pendingRequests.has(artworkPath)) {
|
||||
return pendingRequests.get(artworkPath)!;
|
||||
}
|
||||
|
||||
// Create a new request
|
||||
const requestPromise = (async () => {
|
||||
try {
|
||||
// Call backend to get/generate thumbnail
|
||||
const thumbnailPath = await invoke<string>('library_get_thumbnail', {
|
||||
artworkPath,
|
||||
});
|
||||
|
||||
// Convert to asset URL
|
||||
const url = convertFileSrc(thumbnailPath);
|
||||
thumbnailCache.set(artworkPath, url);
|
||||
return url;
|
||||
} catch (error) {
|
||||
console.error('Failed to get thumbnail:', error);
|
||||
// Fall back to original artwork URL
|
||||
const fallbackUrl = convertFileSrc(artworkPath);
|
||||
thumbnailCache.set(artworkPath, fallbackUrl);
|
||||
return fallbackUrl;
|
||||
} finally {
|
||||
pendingRequests.delete(artworkPath);
|
||||
}
|
||||
})();
|
||||
|
||||
pendingRequests.set(artworkPath, requestPromise);
|
||||
return requestPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get thumbnail URL synchronously if cached, otherwise return null.
|
||||
* Useful for initial render while async loading completes.
|
||||
*/
|
||||
export function getCachedThumbnailUrl(artworkPath: string): string | null {
|
||||
return thumbnailCache.get(artworkPath) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preload thumbnails for a batch of artwork paths.
|
||||
* Useful for preloading visible items.
|
||||
*/
|
||||
export async function preloadThumbnails(artworkPaths: string[]): Promise<void> {
|
||||
await Promise.all(
|
||||
artworkPaths
|
||||
.filter(path => path && !thumbnailCache.has(path))
|
||||
.map(path => getThumbnailUrl(path))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the thumbnail cache.
|
||||
*/
|
||||
export function clearThumbnailCache(): void {
|
||||
thumbnailCache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear thumbnails on the backend and frontend.
|
||||
*/
|
||||
export async function clearAllThumbnails(): Promise<void> {
|
||||
await invoke('library_clear_thumbnails');
|
||||
thumbnailCache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of the thumbnails cache in bytes.
|
||||
*/
|
||||
export async function getThumbnailsCacheSize(): Promise<number> {
|
||||
return invoke<number>('library_get_thumbnails_cache_size');
|
||||
}
|
||||
|
||||
/**
|
||||
* Format cache size for display.
|
||||
*/
|
||||
export function formatCacheSize(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`;
|
||||
}
|
||||
Reference in new issue
Block a user