diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 4227a9ef6..f37ce7f52 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -58,8 +58,8 @@ souvlaki = "0.7" # Desktop notifications notify-rust = "4" -# Image decoding (for tray icon) -image = { version = "0.25", default-features = false, features = ["png"] } +# Image processing (tray icon + thumbnails) +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp"] } # URL opening open = "5" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 9027e9fac..c494dd135 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -530,6 +530,11 @@ pub fn run() { library::commands::discogs_search_artist, library::commands::discogs_search_artwork, library::commands::discogs_download_artwork, + // Thumbnail commands + library::commands::library_get_thumbnail, + library::commands::library_clear_thumbnails, + library::commands::library_get_thumbnails_cache_size, + // Artwork commands library::commands::library_fetch_missing_artwork, library::commands::library_fetch_album_artwork, library::commands::library_set_album_artwork, diff --git a/src-tauri/src/library/commands.rs b/src-tauri/src/library/commands.rs index 51952ba6f..256a99ccf 100644 --- a/src-tauri/src/library/commands.rs +++ b/src-tauri/src/library/commands.rs @@ -11,6 +11,7 @@ use crate::discogs::DiscogsClient; use crate::library::{ cue_to_tracks, get_artwork_cache_dir, CueParser, LibraryDatabase, LibraryFolder, LibraryScanner, LibraryStats, LocalAlbum, LocalArtist, LocalTrack, MetadataExtractor, ScanError, ScanProgress, ScanStatus, + thumbnails, }; use crate::network::{is_network_path, MountKind, NetworkFs}; @@ -1875,3 +1876,37 @@ pub async fn library_get_tracks_by_ids( Ok(tracks) } + +/// Get or generate a thumbnail for an artwork file +/// Returns the path to the thumbnail file +#[tauri::command] +pub async fn library_get_thumbnail( + artwork_path: String, +) -> Result { + log::debug!("Command: library_get_thumbnail for {}", artwork_path); + + let source_path = PathBuf::from(&artwork_path); + + if !source_path.exists() { + return Err(format!("Artwork file not found: {}", artwork_path)); + } + + let thumbnail_path = thumbnails::get_or_generate_thumbnail(&source_path) + .map_err(|e| e.to_string())?; + + Ok(thumbnail_path.to_string_lossy().to_string()) +} + +/// Clear the thumbnails cache +#[tauri::command] +pub async fn library_clear_thumbnails() -> Result<(), String> { + log::info!("Command: library_clear_thumbnails"); + thumbnails::clear_thumbnails().map_err(|e| e.to_string()) +} + +/// Get the thumbnails cache size in bytes +#[tauri::command] +pub async fn library_get_thumbnails_cache_size() -> Result { + log::debug!("Command: library_get_thumbnails_cache_size"); + thumbnails::get_cache_size().map_err(|e| e.to_string()) +} diff --git a/src-tauri/src/library/errors.rs b/src-tauri/src/library/errors.rs index 3f1fb6ed4..5de63ccb9 100644 --- a/src-tauri/src/library/errors.rs +++ b/src-tauri/src/library/errors.rs @@ -18,4 +18,7 @@ pub enum LibraryError { #[error("Invalid path: {0}")] InvalidPath(String), + + #[error("{0}")] + Other(String), } diff --git a/src-tauri/src/library/mod.rs b/src-tauri/src/library/mod.rs index bcfff207d..ea046829e 100644 --- a/src-tauri/src/library/mod.rs +++ b/src-tauri/src/library/mod.rs @@ -10,6 +10,7 @@ pub mod errors; pub mod metadata; pub mod models; pub mod scanner; +pub mod thumbnails; pub use commands::LibraryState; pub use cue_parser::{cue_to_tracks, CueParser, CueSheet, CueTrack}; diff --git a/src-tauri/src/library/thumbnails.rs b/src-tauri/src/library/thumbnails.rs new file mode 100644 index 000000000..4413b8960 --- /dev/null +++ b/src-tauri/src/library/thumbnails.rs @@ -0,0 +1,163 @@ +use image::imageops::FilterType; +use image::ImageReader; +use std::fs; +use std::io::Cursor; +use std::path::{Path, PathBuf}; + +use super::errors::LibraryError; + +/// Default thumbnail size (width and height) +const THUMBNAIL_SIZE: u32 = 200; + +/// Get the thumbnails directory path +pub fn get_thumbnails_dir() -> Result { + let data_dir = dirs::data_local_dir() + .ok_or_else(|| LibraryError::Other("Could not find data directory".into()))?; + let thumbnails_dir = data_dir.join("qbz").join("thumbnails"); + + // Create directory if it doesn't exist + if !thumbnails_dir.exists() { + fs::create_dir_all(&thumbnails_dir) + .map_err(|e| LibraryError::Other(format!("Failed to create thumbnails directory: {}", e)))?; + } + + Ok(thumbnails_dir) +} + +/// Generate a unique filename for a thumbnail based on the source path +fn get_thumbnail_filename(source_path: &Path) -> String { + use std::collections::hash_map::DefaultHasher; + use std::hash::{Hash, Hasher}; + + let mut hasher = DefaultHasher::new(); + source_path.to_string_lossy().hash(&mut hasher); + format!("{:016x}.jpg", hasher.finish()) +} + +/// Get the thumbnail path for a source image +pub fn get_thumbnail_path(source_path: &Path) -> Result { + let thumbnails_dir = get_thumbnails_dir()?; + let filename = get_thumbnail_filename(source_path); + Ok(thumbnails_dir.join(filename)) +} + +/// Check if a thumbnail exists for the given source path +pub fn thumbnail_exists(source_path: &Path) -> Result { + let thumbnail_path = get_thumbnail_path(source_path)?; + Ok(thumbnail_path.exists()) +} + +/// Generate a thumbnail for the given source image +pub fn generate_thumbnail(source_path: &Path) -> Result { + let thumbnail_path = get_thumbnail_path(source_path)?; + + // If thumbnail already exists, return it + if thumbnail_path.exists() { + return Ok(thumbnail_path); + } + + // Read source image + let img = ImageReader::open(source_path) + .map_err(|e| LibraryError::Other(format!("Failed to open image: {}", e)))? + .decode() + .map_err(|e| LibraryError::Other(format!("Failed to decode image: {}", e)))?; + + // Resize to thumbnail size (maintaining aspect ratio, fit within square) + let thumbnail = img.resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE, FilterType::Lanczos3); + + // Save as JPEG with quality 85 + thumbnail + .save(&thumbnail_path) + .map_err(|e| LibraryError::Other(format!("Failed to save thumbnail: {}", e)))?; + + Ok(thumbnail_path) +} + +/// Generate a thumbnail from image bytes (for embedded artwork) +pub fn generate_thumbnail_from_bytes( + bytes: &[u8], + cache_key: &str, +) -> Result { + let thumbnails_dir = get_thumbnails_dir()?; + + // Generate filename from cache key + use std::collections::hash_map::DefaultHasher; + use std::hash::{Hash, Hasher}; + + let mut hasher = DefaultHasher::new(); + cache_key.hash(&mut hasher); + let filename = format!("{:016x}.jpg", hasher.finish()); + let thumbnail_path = thumbnails_dir.join(&filename); + + // If thumbnail already exists, return it + if thumbnail_path.exists() { + return Ok(thumbnail_path); + } + + // Decode image from bytes + let cursor = Cursor::new(bytes); + let img = ImageReader::new(cursor) + .with_guessed_format() + .map_err(|e| LibraryError::Other(format!("Failed to guess image format: {}", e)))? + .decode() + .map_err(|e| LibraryError::Other(format!("Failed to decode image: {}", e)))?; + + // Resize to thumbnail size + let thumbnail = img.resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE, FilterType::Lanczos3); + + // Save as JPEG + thumbnail + .save(&thumbnail_path) + .map_err(|e| LibraryError::Other(format!("Failed to save thumbnail: {}", e)))?; + + Ok(thumbnail_path) +} + +/// Get or generate a thumbnail for an artwork path +/// Returns the path to the thumbnail file +pub fn get_or_generate_thumbnail(artwork_path: &Path) -> Result { + let thumbnail_path = get_thumbnail_path(artwork_path)?; + + if thumbnail_path.exists() { + return Ok(thumbnail_path); + } + + generate_thumbnail(artwork_path) +} + +/// Clear all thumbnails (useful for cache cleanup) +pub fn clear_thumbnails() -> Result<(), LibraryError> { + let thumbnails_dir = get_thumbnails_dir()?; + + if thumbnails_dir.exists() { + fs::remove_dir_all(&thumbnails_dir) + .map_err(|e| LibraryError::Other(format!("Failed to clear thumbnails: {}", e)))?; + fs::create_dir_all(&thumbnails_dir) + .map_err(|e| LibraryError::Other(format!("Failed to recreate thumbnails directory: {}", e)))?; + } + + Ok(()) +} + +/// Get the total size of the thumbnails cache in bytes +pub fn get_cache_size() -> Result { + let thumbnails_dir = get_thumbnails_dir()?; + + if !thumbnails_dir.exists() { + return Ok(0); + } + + let mut total_size = 0u64; + + for entry in fs::read_dir(&thumbnails_dir) + .map_err(|e| LibraryError::Other(format!("Failed to read thumbnails directory: {}", e)))? + { + if let Ok(entry) = entry { + if let Ok(metadata) = entry.metadata() { + total_size += metadata.len(); + } + } + } + + Ok(total_size) +}