Fix & improve adjacent cover image detection

This commit is contained in:
NGnius (Graham) 2023-10-16 20:24:35 -04:00
parent 9f5f8959fc
commit 507add66b8

View file

@ -165,13 +165,24 @@ impl SystemControlWrapper {
}*/ }*/
} }
fn guess_cover_name(dir: &std::path::Path) -> Option<std::path::PathBuf> {
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 { 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 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_art = item.field("cover").and_then(|x| x.to_owned().to_str());
let cover_url = if let Some(art) = &cover_art { let cover_url = if let Some(art) = &cover_art {
const DATA_START: usize = 23; const DATA_START: usize = 23;
const DATA_PREVIEW: usize = 32; 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_slice_start = (DATA_START + DATA_PREVIEW_OFFSET).clamp(0, art.len() - 2);
let preview_slide_end = (DATA_START + DATA_PREVIEW + DATA_PREVIEW_OFFSET) let preview_slide_end = (DATA_START + DATA_PREVIEW + DATA_PREVIEW_OFFSET)
.clamp(preview_slice_start, art.len()); .clamp(preview_slice_start, art.len());
@ -195,10 +206,20 @@ impl SystemControlWrapper {
.ok() .ok()
.map(|file| (decoded, file)) .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) .map(|_| path)
} else { } 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 { Metadata {
length: duration.map(|duration| duration.as_secs_f64().round() as i64 * 1_000_000), length: duration.map(|duration| duration.as_secs_f64().round() as i64 * 1_000_000),