From 507add66b87c240ada3f498fbe249b2ca145900d Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 16 Oct 2023 20:24:35 -0400 Subject: [PATCH] Fix & improve adjacent cover image detection --- player/src/os_controls.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/player/src/os_controls.rs b/player/src/os_controls.rs index fbe0145..5710900 100644 --- a/player/src/os_controls.rs +++ b/player/src/os_controls.rs @@ -165,13 +165,24 @@ impl SystemControlWrapper { }*/ } + fn guess_cover_name(dir: &std::path::Path) -> Option { + const COVER_OPTIONS: [&str; 4] = ["cover.png", "cover.jpg", "folder.png", "folder.jpg"]; + for cover_file in COVER_OPTIONS { + let cover_path = dir.join(cover_file); + if cover_path.exists() { + return Some(cover_path); + } + } + None + } + fn build_metadata(item: Item, duration: Option<&std::time::Duration>) -> Metadata { let file_uri = item.field("filename").and_then(|x| x.to_owned().to_str()); let cover_art = item.field("cover").and_then(|x| x.to_owned().to_str()); let cover_url = if let Some(art) = &cover_art { const DATA_START: usize = 23; const DATA_PREVIEW: usize = 32; - const DATA_PREVIEW_OFFSET: usize = 128; + const DATA_PREVIEW_OFFSET: usize = 1024; let preview_slice_start = (DATA_START + DATA_PREVIEW_OFFSET).clamp(0, art.len() - 2); let preview_slide_end = (DATA_START + DATA_PREVIEW + DATA_PREVIEW_OFFSET) .clamp(preview_slice_start, art.len()); @@ -195,10 +206,20 @@ impl SystemControlWrapper { .ok() .map(|file| (decoded, file)) }) - .and_then(|(decoded, mut file)| file.write(&decoded).ok()) + .and_then(|(decoded, mut file)| file.write(&decoded).and_then(|_| file.flush()).ok()) .map(|_| path) } else { - None + if let Some(filepath) = &file_uri { + let filepath = std::path::PathBuf::from(filepath.strip_prefix("file://").unwrap_or(filepath)); + if let Some(parent_dir) = filepath.parent() { + Self::guess_cover_name(parent_dir).map(|p| p.to_string_lossy().into_owned()) + } else { + None + } + } else { + None + } + }; Metadata { length: duration.map(|duration| duration.as_secs_f64().round() as i64 * 1_000_000),