From 98d719d8331289caf8bc6d7db597ad76a5bbd9b4 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 23 Feb 2026 02:19:08 -0600 Subject: [PATCH] feat(backend): resize and thumbnail custom artist images --- src-tauri/src/commands_v2.rs | 58 ++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index 2c5666ae4..75d2de5b1 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,12 @@ 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]