diff --git a/crates/qbz-library/src/database.rs b/crates/qbz-library/src/database.rs index 52d05f33a..903b93876 100644 --- a/crates/qbz-library/src/database.rs +++ b/crates/qbz-library/src/database.rs @@ -3457,6 +3457,34 @@ impl LibraryDatabase { Ok(result) } + /// Get all custom artist images (for bulk lookup) + pub fn get_all_custom_artist_images( + &self, + ) -> Result, LibraryError> { + let mut stmt = self + .conn + .prepare( + "SELECT artist_name, custom_image_path FROM artist_images WHERE custom_image_path IS NOT NULL", + ) + .map_err(|e| LibraryError::Database(format!("Failed to prepare query: {}", e)))?; + + let rows = stmt + .query_map([], |row| { + Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)) + }) + .map_err(|e| { + LibraryError::Database(format!("Failed to query custom artist images: {}", e)) + })?; + + let mut map = std::collections::HashMap::new(); + for row in rows { + if let Ok((artist_name, custom_image_path)) = row { + map.insert(artist_name, custom_image_path); + } + } + Ok(map) + } + /// Get all canonical artist names mapping (for bulk lookup) pub fn get_all_canonical_names( &self, diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index 2c5666ae4..4bb9c3e40 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -4218,12 +4218,18 @@ pub async fn v2_library_cache_artist_image( .map_err(|e| e.to_string()) } +#[derive(Debug, Clone, serde::Serialize)] +pub struct CustomArtistImageResult { + pub image_path: String, + pub thumbnail_path: String, +} + #[tauri::command] pub async fn v2_library_set_custom_artist_image( artist_name: String, custom_image_path: String, state: State<'_, LibraryState>, -) -> Result<(), String> { +) -> Result { let artwork_dir = get_artwork_cache_dir(); let source = std::path::Path::new(&custom_image_path); if !source.exists() { @@ -4232,20 +4238,43 @@ pub async fn v2_library_set_custom_artist_image( custom_image_path )); } - let extension = source.extension().and_then(|e| e.to_str()).unwrap_or("jpg"); - use md5::{Digest, Md5}; + + // Validate extension + let extension = source + .extension() + .and_then(|e| e.to_str()) + .map(|e| e.to_lowercase()) + .unwrap_or_default(); + if !["png", "jpg", "jpeg", "webp"].contains(&extension.as_str()) { + return Err(format!( + "Unsupported image format: {}. Use png, jpg, jpeg, or webp.", + extension + )); + } + + // Generate filename using MD5 hash of artist name let mut hasher = Md5::new(); hasher.update(artist_name.as_bytes()); let artist_hash = format!("{:x}", hasher.finalize()); - let filename = format!( - "artist_custom_{}_{}.{}", - artist_hash, - chrono::Utc::now().timestamp(), - extension - ); - let dest_path = artwork_dir.join(filename); - std::fs::copy(source, &dest_path).map_err(|e| format!("Failed to copy artwork: {}", e))?; + let timestamp = chrono::Utc::now().timestamp(); + let filename = format!("artist_custom_{}_{}.jpg", artist_hash, timestamp); + let dest_path = artwork_dir.join(&filename); + // Decode, resize to max 1000x1000, save as JPEG + let img = image::ImageReader::open(source) + .map_err(|e| format!("Failed to open image: {}", e))? + .decode() + .map_err(|e| format!("Failed to decode image: {}", e))?; + let resized = img.resize(1000, 1000, image::imageops::FilterType::Lanczos3); + resized + .save(&dest_path) + .map_err(|e| format!("Failed to save resized image: {}", e))?; + + // Generate 500x500 thumbnail using qbz-library + let thumbnail_path = thumbnails::generate_thumbnail(&dest_path) + .map_err(|e| format!("Failed to generate thumbnail: {}", e))?; + + // Update database let guard = state.db.lock().await; let db = guard.as_ref().ok_or("No active session - please log in")?; db.cache_artist_image( @@ -4254,7 +4283,67 @@ pub async fn v2_library_set_custom_artist_image( "custom", Some(&dest_path.to_string_lossy()), ) - .map_err(|e| e.to_string()) + .map_err(|e| e.to_string())?; + + Ok(CustomArtistImageResult { + image_path: dest_path.to_string_lossy().into_owned(), + thumbnail_path: thumbnail_path.to_string_lossy().into_owned(), + }) +} + +#[tauri::command] +pub async fn v2_library_remove_custom_artist_image( + artist_name: String, + state: State<'_, LibraryState>, +) -> Result<(), String> { + // Get current info to find file paths to delete + let guard = state.db.lock().await; + let db = guard.as_ref().ok_or("No active session - please log in")?; + let info = db.get_artist_image(&artist_name).map_err(|e| e.to_string())?; + + if let Some(info) = info { + // Delete custom image file if it exists + if let Some(ref path) = info.custom_image_path { + let p = std::path::Path::new(path); + if p.exists() { + // Also remove thumbnail + if let Ok(thumb) = thumbnails::get_thumbnail_path(p) { + let _ = std::fs::remove_file(thumb); + } + let _ = std::fs::remove_file(p); + } + } + + // Reset to original image (clear custom_image_path, keep image_url) + db.cache_artist_image( + &artist_name, + info.image_url.as_deref(), + info.source.as_deref().unwrap_or("qobuz"), + None, + ) + .map_err(|e| e.to_string())?; + } + + Ok(()) +} + +#[tauri::command] +pub async fn v2_library_get_artist_image( + artist_name: String, + state: State<'_, LibraryState>, +) -> Result, String> { + let guard = state.db.lock().await; + let db = guard.as_ref().ok_or("No active session - please log in")?; + db.get_artist_image(&artist_name).map_err(|e| e.to_string()) +} + +#[tauri::command] +pub async fn v2_library_get_all_custom_artist_images( + state: State<'_, LibraryState>, +) -> Result, String> { + let guard = state.db.lock().await; + let db = guard.as_ref().ok_or("No active session - please log in")?; + db.get_all_custom_artist_images().map_err(|e| e.to_string()) } #[tauri::command] diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 6bac40f34..abe5a8dcf 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1107,6 +1107,9 @@ pub fn run() { commands_v2::v2_library_update_folder_path, commands_v2::v2_library_cache_artist_image, commands_v2::v2_library_set_custom_artist_image, + commands_v2::v2_library_remove_custom_artist_image, + commands_v2::v2_library_get_artist_image, + commands_v2::v2_library_get_all_custom_artist_images, commands_v2::v2_show_track_notification, commands_v2::v2_subscribe_playlist, commands_v2::v2_cache_track_for_offline, diff --git a/src/lib/components/ImageLightbox.svelte b/src/lib/components/ImageLightbox.svelte new file mode 100644 index 000000000..b4377fdcc --- /dev/null +++ b/src/lib/components/ImageLightbox.svelte @@ -0,0 +1,111 @@ + + + + +{#if isOpen} + +{/if} + + diff --git a/src/lib/components/views/AlbumDetailView.svelte b/src/lib/components/views/AlbumDetailView.svelte index 3ca0ab4ef..a6740d05a 100644 --- a/src/lib/components/views/AlbumDetailView.svelte +++ b/src/lib/components/views/AlbumDetailView.svelte @@ -16,6 +16,7 @@ toggleAlbumFavorite } from '$lib/stores/albumFavoritesStore'; import { isBlacklisted as isArtistBlacklisted } from '$lib/stores/artistBlacklistStore'; + import ImageLightbox from '../ImageLightbox.svelte'; interface Track { id: number; @@ -153,6 +154,7 @@ let isFavorite = $state(false); let isFavoriteLoading = $state(false); + let lightboxOpen = $state(false); let scrollContainer: HTMLDivElement | null = $state(null); // Carousel state for "By the same artist" section @@ -327,7 +329,13 @@
-
+
lightboxOpen = true} + onkeydown={(e) => { if (e.key === 'Enter') lightboxOpen = true; }} + role="button" + tabindex="0" + > {album.title}
@@ -572,6 +580,13 @@
+ lightboxOpen = false} + src={album.artwork} + alt={album.title} +/> + diff --git a/src/lib/components/views/FavoritesView.svelte b/src/lib/components/views/FavoritesView.svelte index 1f845fb93..5f841b908 100644 --- a/src/lib/components/views/FavoritesView.svelte +++ b/src/lib/components/views/FavoritesView.svelte @@ -1,5 +1,6 @@