From 762d8bdabea8ef25b1cdbf81109fb144bc86b5b6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 12 Jan 2022 21:07:31 +0900 Subject: [PATCH] Fix second album art fetch method on decode fail Previous implementation did not try second fetch method on actual decode fail, only on unknown mime type. Version bumped to 0.2.13. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/display.rs | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8455de4..5e8010b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1427,7 +1427,7 @@ dependencies = [ [[package]] name = "mpd_info_screen" -version = "0.2.12" +version = "0.2.13" dependencies = [ "ggez", "image", diff --git a/Cargo.toml b/Cargo.toml index 57aab27..2bfaf59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mpd_info_screen" -version = "0.2.12" +version = "0.2.13" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/display.rs b/src/display.rs index 9e503d4..e532137 100644 --- a/src/display.rs +++ b/src/display.rs @@ -220,29 +220,52 @@ impl MPDDisplay { match read_guard_opt.as_ref().unwrap().get_art_type().as_str() { "image/png" => image_format = image::ImageFormat::Png, - "image/jpg" | "image/jpeg" => image_format = image::ImageFormat::Jpeg, + "image/jpg" | "image/jpeg" | "JPG" => image_format = image::ImageFormat::Jpeg, "image/gif" => image_format = image::ImageFormat::Gif, _ => is_unknown_format = true, } - #[allow(unused_assignments)] - if is_unknown_format && !self.tried_album_art_in_dir { - self.tried_album_art_in_dir = true; - self.album_art = None; + let try_second_art_fetch_method = |tried_in_dir: &mut bool, + album_art: &mut Option, + read_guard_opt: &mut Option< + RwLockReadGuard<'_, MPDHandlerState>, + >, + mpd_handler: &Option| + -> Result<(), String> { + *tried_in_dir = true; + album_art.take(); // Drop the "read_guard" so that the "force_try_other_album_art()" // can get a "write_guard" - read_guard_opt = None; - self.mpd_handler + read_guard_opt.take(); + mpd_handler .as_ref() .unwrap() .force_try_other_album_art() .map_err(|_| String::from("Failed to force try other album art fetching method"))?; return Err("Got unknown format album art image".into()); + }; + + if is_unknown_format && !self.tried_album_art_in_dir { + return try_second_art_fetch_method( + &mut self.tried_album_art_in_dir, + &mut self.album_art, + &mut read_guard_opt, + &self.mpd_handler, + ); } - let img = ImageReader::with_format(Cursor::new(&image_ref), image_format) + let img_result = ImageReader::with_format(Cursor::new(&image_ref), image_format) .decode() - .map_err(|e| format!("ERROR: Failed to decode album art image: {}", e))?; + .map_err(|e| format!("ERROR: Failed to decode album art image: {}", e)); + if img_result.is_err() && !self.tried_album_art_in_dir { + return try_second_art_fetch_method( + &mut self.tried_album_art_in_dir, + &mut self.album_art, + &mut read_guard_opt, + &self.mpd_handler, + ); + } + let img = img_result?; let rgba8 = img.to_rgba8(); let ggez_img = Image::from_rgba8( ctx,