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
+43
-15
@@ -1966,22 +1966,50 @@ impl LibraryDatabase {
|
||||
/// bundles, else the parent dir), and returns `<folder>/cover.jpg` when
|
||||
/// that file exists. Frontend-agnostic (no `tauri::State`).
|
||||
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 query = format!("SELECT file_path FROM local_tracks WHERE ({expr}) = ?1 LIMIT 1");
|
||||
let file_path: String = self
|
||||
.conn
|
||||
.query_row(&query, rusqlite::params![group_key], |row| row.get(0))
|
||||
.ok()?;
|
||||
let p = std::path::Path::new(&file_path);
|
||||
let folder = if p.is_dir() {
|
||||
p.to_path_buf()
|
||||
} else {
|
||||
p.parent()?.to_path_buf()
|
||||
};
|
||||
let cover = folder.join("cover.jpg");
|
||||
cover
|
||||
.is_file()
|
||||
.then(|| cover.to_string_lossy().into_owned())
|
||||
// Scan several tracks, not just one: a CMAF album keeps each track in
|
||||
// its own folder, and only some may carry a cover.jpg.
|
||||
let query = format!("SELECT file_path FROM local_tracks WHERE ({expr}) = ?1 LIMIT 12");
|
||||
let mut stmt = self.conn.prepare(&query).ok()?;
|
||||
let paths: Vec<String> = stmt
|
||||
.query_map(rusqlite::params![group_key], |row| row.get::<_, String>(0))
|
||||
.ok()?
|
||||
.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 {
|
||||
p.parent().map(|x| x.to_path_buf())
|
||||
}) else {
|
||||
continue;
|
||||
};
|
||||
// Check the folder and its parent (covers multi-disc layouts where
|
||||
// the art sits one level up).
|
||||
let dirs = [Some(folder.clone()), folder.parent().map(|x| x.to_path_buf())];
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user