diff --git a/rust_backend/crates/link/src/catalog.rs b/rust_backend/crates/link/src/catalog.rs index 3619d5245..9ea8f1695 100644 --- a/rust_backend/crates/link/src/catalog.rs +++ b/rust_backend/crates/link/src/catalog.rs @@ -81,6 +81,9 @@ pub(crate) struct Song { pub(crate) isrc: Option, pub(crate) match_key: Option, pub(crate) cover_path: Option, + /// The desktop's copy of the song's web cover, used only when no cover + /// file or embedded picture serves. + pub(crate) web_cover_path: Option, pub(crate) added_ms: Option, pub(crate) track_gain: Option, pub(crate) album_gain: Option, @@ -117,6 +120,12 @@ impl Song { pub(crate) fn embedded_cover_format(&self) -> Option<&'static str> { cover_format(&self.suffix) } + + /// Whether the song has a cover file or a container that can embed a + /// picture, the sources tried before a web copy. + fn has_own_cover(&self) -> bool { + self.cover_path.is_some() || self.embedded_cover_format().is_some() + } } /// The `mornye_core::tags::extract_cover` format for a file with `suffix` @@ -138,7 +147,8 @@ pub(crate) fn cover_format(suffix: &str) -> Option<&'static str> { } /// Where the image behind a `coverArt` id comes from: the first of `files` -/// still on disk, else the first picture embedded in `embedded`. +/// still on disk, else the first picture embedded in `embedded`, else the +/// first of `web` still on disk. #[derive(Debug)] pub(crate) struct CoverSource<'a> { /// Separate image files, tried in order. @@ -146,6 +156,8 @@ pub(crate) struct CoverSource<'a> { /// Pictures embedded in these audio files (path and /// [`Song::embedded_cover_format`]), tried in order. pub(crate) embedded: Vec<(&'a Path, &'static str)>, + /// Copies of web covers ([`Song::web_cover_path`]), tried in order. + pub(crate) web: Vec<&'a Path>, } /// Songs grouped by album key. @@ -159,7 +171,7 @@ pub(crate) struct Album { /// Song indexes by disc, track, then title. pub(crate) songs: Vec, /// The first song (in album order) with a cover file, else the first - /// whose container can embed a cover. + /// whose container can embed a cover, else the first with a web copy. pub(crate) cover: Option, pub(crate) year: Option, pub(crate) genre: Option, @@ -179,7 +191,8 @@ pub(crate) struct Artist { pub(crate) albums: Vec, /// Albums with at least one song by this artist. pub(crate) appears_on: Vec, - /// The first owned (else appeared-on) album with a cover. + /// The first owned (else appeared-on) album with a cover; an album whose + /// only cover is a web copy only when no other has one. pub(crate) cover_album: Option, /// Folded name, for sorting and search. sort_name: Box, @@ -331,7 +344,7 @@ impl Catalog { let album = &self.albums[song.album]; if album.cover.is_some() { Some(&album.id) - } else if song.cover_path.is_some() { + } else if song.cover_path.is_some() || song.web_cover_path.is_some() { Some(&song.id) } else { None @@ -353,7 +366,8 @@ impl Catalog { /// The image behind a `coverArt` id (album, artist or song id): a song's /// own cover file, else its album's cover file, else the pictures /// embedded in the song itself and then in its album's songs, in album - /// order. A listed file that is gone falls through to the next. + /// order, else the song's own web copy, else its album's first (album + /// order). A listed file that is gone falls through to the next. pub(crate) fn cover_source(&self, id: &str) -> Option> { let (album, own) = if let Some(index) = self.album_index(id) { (index, None) @@ -390,7 +404,23 @@ impl Catalog { }) .take(EMBEDDED_COVER_TRIES) .collect(); - (!files.is_empty() || !embedded.is_empty()).then_some(CoverSource { files, embedded }) + let mut web: Vec<&Path> = own + .and_then(|index| self.songs[index].web_cover_path.as_deref()) + .into_iter() + .collect(); + if let Some(path) = album + .songs + .iter() + .find_map(|index| self.songs[*index].web_cover_path.as_deref()) + && !web.contains(&path) + { + web.push(path); + } + (!files.is_empty() || !embedded.is_empty() || !web.is_empty()).then_some(CoverSource { + files, + embedded, + web, + }) } /// Songs matching every term (title, artist or album), export order. @@ -542,6 +572,7 @@ impl Builder { isrc: non_empty(row.isrc), match_key: non_empty(row.match_key), cover_path: non_empty(row.cover_path).map(PathBuf::from), + web_cover_path: non_empty(row.web_cover_path).map(PathBuf::from), added_ms: row.added_ms, track_gain: row.track_gain, album_gain: row.album_gain, @@ -663,6 +694,13 @@ impl Builder { .iter() .copied() .find(|index| songs[*index].embedded_cover_format().is_some()) + }) + .or_else(|| { + album + .songs + .iter() + .copied() + .find(|index| songs[*index].web_cover_path.is_some()) }); album.year = members.clone().find_map(|song| song.year); album.genre = members.clone().find_map(|song| song.genre.clone()); @@ -695,11 +733,15 @@ impl Builder { &albums[*b].id, )) }); - artist.cover_album = artist - .browse_albums() + // An album whose only cover is a web copy comes last. + let cover = + |album: &usize| albums[*album].cover.map(|song| songs[song].has_own_cover()); + let browse = artist.browse_albums(); + artist.cover_album = browse .iter() .copied() - .find(|album| albums[*album].cover.is_some()); + .find(|album| cover(album) == Some(true)) + .or_else(|| browse.iter().copied().find(|album| cover(album).is_some())); } let mut genres: Vec = Vec::new(); diff --git a/rust_backend/crates/link/src/catalog/parse.rs b/rust_backend/crates/link/src/catalog/parse.rs index e8d2678fb..49446366f 100644 --- a/rust_backend/crates/link/src/catalog/parse.rs +++ b/rust_backend/crates/link/src/catalog/parse.rs @@ -85,6 +85,8 @@ pub(super) struct Row { pub(super) match_key: Option, #[serde(deserialize_with = "text")] pub(super) cover_path: Option, + #[serde(deserialize_with = "text")] + pub(super) web_cover_path: Option, #[serde(deserialize_with = "integer")] pub(super) added_ms: Option, #[serde(deserialize_with = "decimal")] diff --git a/rust_backend/crates/link/src/catalog/tests.rs b/rust_backend/crates/link/src/catalog/tests.rs index 32e369dde..a61547080 100644 --- a/rust_backend/crates/link/src/catalog/tests.rs +++ b/rust_backend/crates/link/src/catalog/tests.rs @@ -500,6 +500,110 @@ fn a_cover_source_lists_every_fallback_in_order() { ); } +/// The web copies tried last for `id`. +fn web_copies<'a>(catalog: &'a Catalog, id: &str) -> Vec<&'a Path> { + catalog + .cover_source(id) + .map(|source| source.web) + .unwrap_or_default() +} + +#[test] +fn web_copies_come_after_every_other_cover() { + let web = |name: &str| json!(format!("/web/{name}.png")); + let catalog = catalog(&[ + // A line from before web copies, and ones without a copy. + row("h:1", "One", "Band", json!({ "album": "H", "track": 1 })), + row( + "h:2", + "Two", + "Band", + json!({ "album": "H", "track": 2, "webCoverPath": null }), + ), + row( + "h:3", + "Three", + "Band", + json!({ "album": "H", "track": 3, "webCoverPath": "" }), + ), + // A copy ahead of a cover file and of an embeddable song. + row( + "i:1", + "One", + "Band", + json!({ "album": "I", "track": 1, "suffix": "dsf", "webCoverPath": web("i1") }), + ), + row( + "i:2", + "Two", + "Band", + json!({ "album": "I", "track": 2, "coverPath": "/c/i2.jpg" }), + ), + row( + "j:1", + "One", + "Band", + json!({ "album": "J", "track": 1, "suffix": "dsf", "webCoverPath": web("j1") }), + ), + row( + "j:2", + "Two", + "Band", + json!({ "album": "J", "track": 2, "webCoverPath": web("j2") }), + ), + // Only web copies, in a format that cannot embed a picture. + row( + "k:1", + "One", + "Solo", + json!({ "album": "K", "track": 1, "suffix": "dsf", "webCoverPath": web("k1") }), + ), + row( + "k:2", + "Two", + "Solo", + json!({ "album": "K", "track": 2, "suffix": "dsf", "webCoverPath": web("k2") }), + ), + // The artist's first album has only a copy, a later one its own. + row( + "m:1", + "One", + "Mixed", + json!({ "album": "A First", "suffix": "dsf", "webCoverPath": web("m1") }), + ), + row("m:2", "Two", "Mixed", json!({ "album": "B Second" })), + ]); + for id in ["h:1", "h:2", "h:3"] { + assert_eq!(catalog.song(id).expect("song").web_cover_path, None); + } + assert!(web_copies(&catalog, "h:1").is_empty()); + + assert_eq!(album_of(&catalog, "i:1").cover, catalog.song_index("i:2")); + assert_eq!(cover_files(&catalog, "i:1"), [Path::new("/c/i2.jpg")]); + assert_eq!(web_copies(&catalog, "i:1"), [Path::new("/web/i1.png")]); + assert_eq!(album_of(&catalog, "j:1").cover, catalog.song_index("j:2")); + assert_eq!( + web_copies(&catalog, "j:2"), + [Path::new("/web/j2.png"), Path::new("/web/j1.png")], + "its own copy, then its album's first" + ); + + let k = album_of(&catalog, "k:1"); + assert_eq!(k.cover, catalog.song_index("k:1")); + let two = catalog.song_index("k:2").expect("song"); + assert_eq!(catalog.song_cover_id(two), Some(k.id.as_str())); + assert!(embedded(&catalog, "k:2").is_empty()); + assert!(cover_files(&catalog, "k:2").is_empty()); + assert_eq!(web_copies(&catalog, &k.id), [Path::new("/web/k1.png")]); + // An artist's cover comes from an album with a cover of its own first. + let album = |id: &str| catalog.song(id).expect("song").album; + let artist = |id: &str| { + &catalog.artists()[catalog.songs()[catalog.song_index(id).expect("song")].artist_ref] + }; + assert_eq!(artist("m:1").cover_album, Some(album("m:2"))); + assert_eq!(artist("k:1").cover_album, Some(album("k:1"))); +} + /// Run with `cargo test --release -p mornye-link -- --ignored --nocapture`. #[test] #[ignore = "timing check for release builds"] diff --git a/rust_backend/crates/link/src/subsonic/covers.rs b/rust_backend/crates/link/src/subsonic/covers.rs index 8111c8ef3..d133b25ae 100644 --- a/rust_backend/crates/link/src/subsonic/covers.rs +++ b/rust_backend/crates/link/src/subsonic/covers.rs @@ -15,10 +15,11 @@ use http::{HeaderMap, Response}; use image::codecs::jpeg::JpegEncoder; use image::imageops::FilterType; use image::{ImageReader, Limits}; +use mornye_core::tags::CoverArt; use sha2::{Digest, Sha256}; use std::collections::HashSet; use std::fs; -use std::io::{Cursor, Read}; +use std::io::{self, Cursor, Read, Seek, SeekFrom}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{LazyLock, Mutex, MutexGuard, PoisonError}; @@ -57,9 +58,10 @@ pub(super) async fn cover_art( .into_iter() .map(|(path, format)| (path.to_owned(), format)) .collect(); + let web: Vec = source.web.into_iter().map(Path::to_owned).collect(); let dir = shared.config.temp_dir.join(CACHE_DIR); let cache = dir.clone(); - let image = tokio::task::spawn_blocking(move || find_image(&cache, &files, &candidates)) + let image = tokio::task::spawn_blocking(move || find_image(&cache, &files, &candidates, &web)) .await .ok() .flatten() @@ -117,27 +119,30 @@ struct Image { /// The first of `files` on disk, else the first picture embedded in /// `candidates` (audio file and extraction format), extracted into `dir` on -/// first use. Blocking. +/// first use, else the first of `web` on disk. Blocking. fn find_image( dir: &Path, files: &[PathBuf], candidates: &[(PathBuf, &'static str)], + web: &[PathBuf], ) -> Option { + let on_disk = |path: &PathBuf| { + let metadata = fs::metadata(path).ok().filter(fs::Metadata::is_file)?; + Some(Image { + path: path.clone(), + len: metadata.len(), + stamp: version_stamp(path, &metadata), + }) + }; files .iter() - .find_map(|path| { - let metadata = fs::metadata(path).ok().filter(fs::Metadata::is_file)?; - Some(Image { - path: path.clone(), - len: metadata.len(), - stamp: version_stamp(path, &metadata), - }) - }) + .find_map(on_disk) .or_else(|| { candidates .iter() .find_map(|(audio, format)| extract_cached(dir, audio, format)) }) + .or_else(|| web.iter().find_map(on_disk)) } /// Identifies one version of a file: its path, mtime and length. @@ -172,9 +177,10 @@ fn remember_without_picture(key: [u8; 16]) { } /// Whether the audio file at `path` embeds a picture `getCoverArt` would -/// serve; `None` when it is not a readable file. A file whose format cannot -/// embed one has none. Reads the picture without decoding it, and remembers -/// a file without one as `getCoverArt` does. Blocking. +/// serve; `None` when it is not a readable file or reading it failed. A file +/// whose format cannot embed one has none. Reads the picture without +/// decoding it, and remembers a file read without one as `getCoverArt` +/// does. Blocking. pub fn embeds_picture(path: &Path) -> Option { let metadata = fs::metadata(path).ok().filter(fs::Metadata::is_file)?; let suffix = path @@ -189,19 +195,64 @@ pub fn embeds_picture(path: &Path) -> Option { if without_picture().contains(&key) { return Some(false); } - let mut file = fs::File::open(path).ok()?; - match mornye_core::tags::extract_cover(&mut file, format, &|| Ok(())) { - Ok(cover) if !cover.data.is_empty() => Some(true), - _ => { + match read_picture(fs::File::open(path).ok()?, format).ok()? { + Some(_) => Some(true), + None => { remember_without_picture(key); Some(false) } } } +/// The picture embedded in `file`: `Ok(None)` when it has none +/// `extract_cover` can read, `Err` when reading the file failed, which is +/// not an answer to remember. +pub(super) fn read_picture(file: impl Read + Seek, format: &str) -> Result, ()> { + let mut file = Watched { + file, + failed: false, + }; + match mornye_core::tags::extract_cover(&mut file, format, &|| Ok(())) { + Ok(cover) if !cover.data.is_empty() => Ok(Some(cover)), + _ if file.failed => Err(()), + _ => Ok(None), + } +} + +/// A file that notes whether a read or seek failed. +struct Watched { + file: F, + failed: bool, +} + +impl Watched { + fn note(&mut self, result: io::Result) -> io::Result { + if let Err(error) = &result + && error.kind() != io::ErrorKind::Interrupted + { + self.failed = true; + } + result + } +} + +impl Read for Watched { + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + let result = self.file.read(buffer); + self.note(result) + } +} + +impl Seek for Watched { + fn seek(&mut self, position: SeekFrom) -> io::Result { + let result = self.file.seek(position); + self.note(result) + } +} + /// The cached picture of `audio`, extracted when missing. `None` when the -/// file is unreadable or embeds no picture (remembered), or the cache is -/// unwritable. +/// file is unreadable, or embeds no picture (remembered once read), or the +/// cache is unwritable. fn extract_cached(dir: &Path, audio: &Path, format: &str) -> Option { let metadata = fs::metadata(audio).ok().filter(fs::Metadata::is_file)?; // The picture is a version of the audio file, whatever its cached copy. @@ -224,10 +275,9 @@ fn extract_cached(dir: &Path, audio: &Path, format: &str) -> Option { { return Some(Image { path, len, stamp }); } - let mut file = fs::File::open(audio).ok()?; - let cover = match mornye_core::tags::extract_cover(&mut file, format, &|| Ok(())) { - Ok(cover) if !cover.data.is_empty() => cover, - _ => { + let cover = match read_picture(fs::File::open(audio).ok()?, format).ok()? { + Some(cover) => cover, + None => { remember_without_picture(key); return None; } diff --git a/rust_backend/crates/link/src/subsonic/tests.rs b/rust_backend/crates/link/src/subsonic/tests.rs index a106c0d5a..620fed950 100644 --- a/rust_backend/crates/link/src/subsonic/tests.rs +++ b/rust_backend/crates/link/src/subsonic/tests.rs @@ -7,6 +7,7 @@ mod covers; mod forward; mod lyrics; mod media; +mod web_covers; mod xml; use super::*; diff --git a/rust_backend/crates/link/src/subsonic/tests/covers.rs b/rust_backend/crates/link/src/subsonic/tests/covers.rs index 2b7a352a1..51c0d2375 100644 --- a/rust_backend/crates/link/src/subsonic/tests/covers.rs +++ b/rust_backend/crates/link/src/subsonic/tests/covers.rs @@ -1,5 +1,6 @@ use super::*; -use crate::subsonic::covers::{CACHE_DIR, embeds_picture, trim_cache}; +use crate::subsonic::covers::{CACHE_DIR, embeds_picture, read_picture, trim_cache}; +use std::io::{Cursor, Read, Seek, SeekFrom}; fn cached_files(fixture: &Fixture) -> Vec { std::fs::read_dir(fixture.shared.config.temp_dir.join(CACHE_DIR)) @@ -203,7 +204,7 @@ fn picture_block(image: &[u8]) -> Vec { /// A FLAC with `image` embedded, and one of the same length without a /// picture (a PADDING block instead). -fn flac_pair(image: &[u8]) -> (Vec, Vec) { +pub(super) fn flac_pair(image: &[u8]) -> (Vec, Vec) { let picture = picture_block(image); (flac(6, &picture), flac(1, &vec![0; picture.len()])) } @@ -300,6 +301,49 @@ fn embeds_picture_tells_files_with_a_picture_from_the_rest() { assert_eq!(embeds_picture(dir.path()), None); } +/// Bytes whose reads fail from `after` on, like a disk that went away. +struct Failing { + bytes: Cursor>, + after: u64, +} + +impl Read for Failing { + fn read(&mut self, buffer: &mut [u8]) -> std::io::Result { + let left = self.after.saturating_sub(self.bytes.position()); + if left == 0 { + return Err(std::io::Error::other("disk gone")); + } + let take = buffer + .len() + .min(usize::try_from(left).unwrap_or(usize::MAX)); + self.bytes.read(&mut buffer[..take]) + } +} + +impl Seek for Failing { + fn seek(&mut self, position: SeekFrom) -> std::io::Result { + self.bytes.seek(position) + } +} + +#[test] +fn a_failed_read_is_not_a_file_without_a_picture() { + let (with, without) = flac_pair(&png(8, 8)); + assert!(matches!( + read_picture(Cursor::new(with.clone()), "flac"), + Ok(Some(_)) + )); + assert!(matches!( + read_picture(Cursor::new(without), "flac"), + Ok(None) + )); + let failing = Failing { + bytes: Cursor::new(with), + after: 12, + }; + assert!(read_picture(failing, "flac").is_err(), "not remembered"); +} + #[tokio::test] async fn a_missing_cover_file_falls_back_to_the_embedded_picture() { let original = png(40, 20); @@ -392,7 +436,7 @@ async fn small_cover(fixture: &Fixture, id: &str, headers: HeaderMap) -> Reply { .await } -async fn json_cover(fixture: &Fixture, id: &str) -> Reply { +pub(super) async fn json_cover(fixture: &Fixture, id: &str) -> Reply { fixture .raw( "getCoverArt", diff --git a/rust_backend/crates/link/src/subsonic/tests/web_covers.rs b/rust_backend/crates/link/src/subsonic/tests/web_covers.rs new file mode 100644 index 000000000..d672e7719 --- /dev/null +++ b/rust_backend/crates/link/src/subsonic/tests/web_covers.rs @@ -0,0 +1,96 @@ +//! Web cover copies (`webCoverPath`, contract section 5) rank after every +//! cover file and embedded picture. + +use super::covers::{flac_pair, json_cover}; +use super::*; + +/// The body `getCoverArt` serves for each of `ids`. +async fn served(fixture: &Fixture, ids: &[&str]) -> Vec> { + let mut bodies = Vec::new(); + for id in ids { + let reply = json_cover(fixture, id).await; + assert_eq!(reply.status, 200, "{id}"); + bodies.push(reply.body.to_vec()); + } + bodies +} + +#[tokio::test] +async fn a_mixed_album_serves_its_embedded_picture_before_a_web_copy() { + let picture = png(40, 20); + let copy = png(16, 16); + let fixture = Fixture::new(|dir| { + let (with, without) = flac_pair(&picture); + let bare = write_file(dir, "1.flac", &without); + let embedded = write_file(dir, "2.flac", &with); + let web = write_file(dir, "web.png", ©); + vec![ + json!({ "id": "s1", "title": "One", "artist": "A", "album": "X", "track": 1, + "path": bare, "webCoverPath": web }), + json!({ "id": "s2", "title": "Two", "artist": "A", "album": "X", "track": 2, + "path": embedded }), + ] + }); + let album = fixture.album_id("s1"); + let artist = fixture.artist_id("A"); + assert_eq!( + served(&fixture, &[&album, "s1", "s2", &artist]).await, + vec![picture.clone(); 4], + "the album's own picture, as without the copy" + ); +} + +#[tokio::test] +async fn a_web_copy_serves_only_when_nothing_else_exists() { + let first = png(16, 16); + let second = png(24, 12); + let fixture = Fixture::new(|dir| { + let (_, without) = flac_pair(&png(8, 8)); + let bare = write_file(dir, "bare.flac", &without); + let one = write_file(dir, "one.png", &first); + let two = write_file(dir, "two.png", &second); + vec![ + json!({ "id": "s1", "title": "One", "artist": "A", "album": "X", "track": 1, + "path": bare, "webCoverPath": one }), + json!({ "id": "s2", "title": "Two", "artist": "A", "album": "X", "track": 2, + "path": bare, "webCoverPath": two }), + json!({ "id": "s3", "title": "Three", "artist": "A", "album": "X", "track": 3, + "path": bare, "webCoverPath": dir.join("pruned.png").to_string_lossy() }), + json!({ "id": "s4", "title": "Four", "artist": "B", "album": "Y", "path": bare }), + ] + }); + let album = fixture.album_id("s1"); + let song = fixture.ok("getSong", &[("id", "s2")], "song").await; + assert_eq!(song["coverArt"], album.as_str()); + // A song its own copy, then the album's; the album its first song's. + assert_eq!( + served(&fixture, &[&album, "s1", "s2", "s3"]).await, + vec![first.clone(), first.clone(), second, first] + ); + // A song with neither a picture nor a copy still has no cover. + assert_eq!(json_cover(&fixture, "s4").await.status, 404); +} + +#[tokio::test] +async fn a_local_cover_file_after_a_web_only_song_keeps_the_album_cover() { + let cover = png(30, 30); + let copy = png(16, 16); + let fixture = Fixture::new(|dir| { + let (_, without) = flac_pair(&png(8, 8)); + let bare = write_file(dir, "bare.flac", &without); + let file = write_file(dir, "cover.png", &cover); + let web = write_file(dir, "web.png", ©); + vec![ + json!({ "id": "s1", "title": "One", "artist": "A", "album": "X", "track": 1, + "path": bare, "webCoverPath": web }), + json!({ "id": "s2", "title": "Two", "artist": "A", "album": "X", "track": 2, + "path": bare, "coverPath": file }), + ] + }); + let album = fixture.album_id("s1"); + assert_eq!( + served(&fixture, &[&album, "s1", "s2"]).await, + vec![cover.clone(); 3], + "the cover file, as without the copy" + ); +}