fix(locallibrary): make the album folder-cover fallback robust
Generalize resolve_album_cover_fallback beyond a single track + cover.jpg: scan several of the album's tracks (CMAF keeps each track in its own folder, only some of which may carry a cover), check both the track folder and its parent (multi-disc layouts keep art one level up), and accept the common local cover filenames (cover/folder/front, jpg/png). Resolves more on-disk covers without touching the paged query. Albums whose offline bundle never saved a cover (segments only) still need an offline-cache cover re-fetch. cc
This commit is contained in:
1 file changed
+41
-13
@@ -1966,22 +1966,50 @@ impl LibraryDatabase {
|
|||||||
/// bundles, else the parent dir), and returns `<folder>/cover.jpg` when
|
/// bundles, else the parent dir), and returns `<folder>/cover.jpg` when
|
||||||
/// that file exists. Frontend-agnostic (no `tauri::State`).
|
/// that file exists. Frontend-agnostic (no `tauri::State`).
|
||||||
pub fn resolve_album_cover_fallback(&self, group_key: &str) -> Option<String> {
|
pub fn resolve_album_cover_fallback(&self, group_key: &str) -> Option<String> {
|
||||||
|
// Common on-disk cover filenames (the offline-cache writes cover.jpg;
|
||||||
|
// ripped/local folders often use folder.jpg / front.*).
|
||||||
|
const NAMES: [&str; 6] = [
|
||||||
|
"cover.jpg",
|
||||||
|
"cover.png",
|
||||||
|
"folder.jpg",
|
||||||
|
"Folder.jpg",
|
||||||
|
"front.jpg",
|
||||||
|
"front.png",
|
||||||
|
];
|
||||||
let expr = crate::album_grouping::metadata_group_key_sql_expression();
|
let expr = crate::album_grouping::metadata_group_key_sql_expression();
|
||||||
let query = format!("SELECT file_path FROM local_tracks WHERE ({expr}) = ?1 LIMIT 1");
|
// Scan several tracks, not just one: a CMAF album keeps each track in
|
||||||
let file_path: String = self
|
// its own folder, and only some may carry a cover.jpg.
|
||||||
.conn
|
let query = format!("SELECT file_path FROM local_tracks WHERE ({expr}) = ?1 LIMIT 12");
|
||||||
.query_row(&query, rusqlite::params![group_key], |row| row.get(0))
|
let mut stmt = self.conn.prepare(&query).ok()?;
|
||||||
.ok()?;
|
let paths: Vec<String> = stmt
|
||||||
let p = std::path::Path::new(&file_path);
|
.query_map(rusqlite::params![group_key], |row| row.get::<_, String>(0))
|
||||||
let folder = if p.is_dir() {
|
.ok()?
|
||||||
p.to_path_buf()
|
.filter_map(Result::ok)
|
||||||
|
.collect();
|
||||||
|
for fp in &paths {
|
||||||
|
let p = std::path::Path::new(fp);
|
||||||
|
// The track folder: the path itself for a CMAF bundle dir, else
|
||||||
|
// the parent of the audio file.
|
||||||
|
let Some(folder) = (if p.is_dir() {
|
||||||
|
Some(p.to_path_buf())
|
||||||
} else {
|
} else {
|
||||||
p.parent()?.to_path_buf()
|
p.parent().map(|x| x.to_path_buf())
|
||||||
|
}) else {
|
||||||
|
continue;
|
||||||
};
|
};
|
||||||
let cover = folder.join("cover.jpg");
|
// Check the folder and its parent (covers multi-disc layouts where
|
||||||
cover
|
// the art sits one level up).
|
||||||
.is_file()
|
let dirs = [Some(folder.clone()), folder.parent().map(|x| x.to_path_buf())];
|
||||||
.then(|| cover.to_string_lossy().into_owned())
|
for dir in dirs.into_iter().flatten() {
|
||||||
|
for name in NAMES {
|
||||||
|
let cover = dir.join(name);
|
||||||
|
if cover.is_file() {
|
||||||
|
return Some(cover.to_string_lossy().into_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_albums_metadata_page_inner(
|
fn get_albums_metadata_page_inner(
|
||||||
|
|||||||
Reference in new issue
Block a user