diff --git a/src-tauri/src/library/commands.rs b/src-tauri/src/library/commands.rs index 730244220..7bf4798a2 100644 --- a/src-tauri/src/library/commands.rs +++ b/src-tauri/src/library/commands.rs @@ -161,13 +161,20 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String> match MetadataExtractor::extract(audio_path) { Ok(mut track) => { - // Try to extract embedded artwork, fallback to folder artwork + // Try to extract embedded artwork, fallback to cached folder artwork let artwork_cache = get_artwork_cache_dir(); - if let Some(artwork_path) = MetadataExtractor::extract_artwork(audio_path, &artwork_cache) { - track.artwork_path = Some(artwork_path); - } else if let Some(folder_art) = MetadataExtractor::find_folder_artwork(audio_path) { - track.artwork_path = Some(folder_art); + let mut artwork_path = + MetadataExtractor::extract_artwork(audio_path, &artwork_cache); + if artwork_path.is_none() { + if let Some(folder_art) = MetadataExtractor::find_folder_artwork(audio_path) + { + artwork_path = MetadataExtractor::cache_artwork_file( + Path::new(&folder_art), + &artwork_cache, + ); + } } + track.artwork_path = artwork_path; let db = state.db.lock().await; if let Err(e) = db.insert_track(&track) { @@ -223,7 +230,24 @@ async fn process_cue_file(cue_path: &Path, state: &State<'_, LibraryState>) -> R let format = MetadataExtractor::detect_format(audio_path); // Convert CUE to tracks - let tracks = cue_to_tracks(&cue, properties.duration_secs, format, &properties); + let mut tracks = cue_to_tracks(&cue, properties.duration_secs, format, &properties); + + let artwork_cache = get_artwork_cache_dir(); + let mut artwork_path = + MetadataExtractor::extract_artwork(audio_path, &artwork_cache); + if artwork_path.is_none() { + if let Some(folder_art) = MetadataExtractor::find_folder_artwork(audio_path) { + artwork_path = MetadataExtractor::cache_artwork_file( + Path::new(&folder_art), + &artwork_cache, + ); + } + } + if let Some(path) = artwork_path { + for track in tracks.iter_mut() { + track.artwork_path = Some(path.clone()); + } + } // Insert tracks let db = state.db.lock().await; diff --git a/src-tauri/src/library/metadata.rs b/src-tauri/src/library/metadata.rs index 1a9c56a00..ed2d30f0c 100644 --- a/src-tauri/src/library/metadata.rs +++ b/src-tauri/src/library/metadata.rs @@ -293,6 +293,39 @@ impl MetadataExtractor { Some(artwork_path.to_string_lossy().to_string()) } + /// Copy an existing artwork file into the cache directory + /// Returns path to cached artwork or None + pub fn cache_artwork_file(artwork_path: &Path, cache_dir: &Path) -> Option { + if !artwork_path.is_file() { + return None; + } + + let ext = artwork_path + .extension() + .and_then(|e| e.to_str()) + .map(|s| s.to_lowercase()) + .unwrap_or_else(|| "jpg".to_string()); + + let normalized_ext = match ext.as_str() { + "jpeg" => "jpg", + "png" | "jpg" | "gif" | "bmp" => ext.as_str(), + _ => "jpg", + }; + + let hash = Self::simple_hash(&artwork_path.to_string_lossy()); + let cached_name = format!("local_{:x}.{}", hash, normalized_ext); + let cached_path = cache_dir.join(cached_name); + + if cached_path.exists() { + return Some(cached_path.to_string_lossy().to_string()); + } + + fs::create_dir_all(cache_dir).ok()?; + fs::copy(artwork_path, &cached_path).ok()?; + + Some(cached_path.to_string_lossy().to_string()) + } + /// Simple hash function for generating filenames fn simple_hash(s: &str) -> u64 { let mut hash: u64 = 5381;