diff --git a/crates/qbz-source/src/registry.rs b/crates/qbz-source/src/registry.rs index 95fadb5aa..e2c7793fc 100644 --- a/crates/qbz-source/src/registry.rs +++ b/crates/qbz-source/src/registry.rs @@ -177,6 +177,23 @@ impl SourceRegistry { } } + /// Interpret a RAW artwork token a row of `source` carried. + /// + /// The replacement for `artwork_qt::classify`: instead of one function + /// sniffing every url shape in the app — and therefore knowing about Plex + /// because somebody added an `is_thumb_path` arm to it — each source reads + /// its own tokens and nobody else's. + /// + /// An UNREGISTERED source is [`ArtRef::None`], not `Unavailable`: a row + /// whose source word this build does not know has no art to wait for, and + /// `Unavailable` would put it in the "retry when connected" bucket forever. + pub fn artwork_token(&self, source: SourceId, token: &str, size: ArtSize) -> ArtRef { + match self.get(source) { + Some(s) => s.artwork_token(token, size), + None => ArtRef::None, + } + } + /// The EXPENSIVE artwork phase, keyed by [`ArtRef`] rather than /// [`MediaRef`] — the windowed pipeline it serves has no item in scope. pub fn thumbnail(&self, source: SourceId, art: &ArtRef, size: ArtSize) -> ArtRef { diff --git a/crates/qbz-source/src/source.rs b/crates/qbz-source/src/source.rs index 6529b7ad2..ccabc1ecf 100644 --- a/crates/qbz-source/src/source.rs +++ b/crates/qbz-source/src/source.rs @@ -65,6 +65,32 @@ pub trait Source: Send + Sync + 'static { /// today (local_playback.rs:302, :313, :323). fn artwork(&self, item: &MediaRef, size: ArtSize) -> ArtRef; + /// What a RAW artwork token from one of this source's OWN rows means. + /// + /// This is the artwork analogue of [`Source::claim`], and it exists for the + /// same reason: the row mappers already hold the token + /// (`LocalTrack.artwork_path`, `LocalAlbum.artwork_path`, + /// `QueueTrack.artwork_url`) at the moment its provenance is still known, + /// and the alternative was to throw that away into a `String` and sniff it + /// back out later. `artwork_qt::classify` is that sniffing, and it is bug + /// 3: it recognises a Plex thumb because somebody bolted + /// `local_plex::is_thumb_path` onto it, so a Jellyfin token + /// (`/Items//Images/Primary`) reads as a FILESYSTEM PATH — it starts + /// with `/` — and the pipeline tries to open it from disk. Blank cover, no + /// error, nowhere to look. + /// + /// Distinct from [`Source::artwork`] because the caller has no + /// [`MediaRef`]: the windowed grid pipeline is keyed by `art_key` and holds + /// no item (design §3.4). Going through `artwork` would mean claiming once + /// per visible row AND letting the source look the token up again — for + /// Plex that is one `plex_cache.db` query per row on a cold grid, on the + /// exact grid whose scroll cost is already the open perf item. + /// + /// **MUST NOT touch the network, a database, or the filesystem.** The token + /// is the whole input; this is a pure interpretation, called once per row + /// per page load. + fn artwork_token(&self, token: &str, size: ArtSize) -> ArtRef; + /// EXPENSIVE phase: may decode + downscale. /// /// Takes an [`ArtRef`], NOT a [`MediaRef`] — the windowed pipeline it diff --git a/crates/qbz-source/src/sources/local.rs b/crates/qbz-source/src/sources/local.rs index 47b3f62fe..e4c9cbc37 100644 --- a/crates/qbz-source/src/sources/local.rs +++ b/crates/qbz-source/src/sources/local.rs @@ -647,6 +647,38 @@ impl Source for LocalSource { } } + fn artwork_token(&self, token: &str, _size: ArtSize) -> ArtRef { + // The `LocalFile` half of `artwork_qt::classify` (artwork_qt.rs:155-183), + // minus the guessing: this only ever sees a token a LOCAL row carried, + // so an absolute path is a file rather than "a shape that might be a + // Plex thumb". No `stat` — this must stay pure; the CHEAP-phase + // thumbnail lookup happens in the pipeline, exactly where it does + // today. + let token = token.trim(); + if token.is_empty() { + return ArtRef::None; + } + // A `file://` url is ALREADY on disk — reqwest cannot even build a + // request for it ("builder error for url"), so it must never be + // fetched. + if let Some(path) = token.strip_prefix("file://") { + return ArtRef::File(PathBuf::from(path)); + } + // An offline-download row can keep the CDN url its cover came from. + if token.starts_with("http://") || token.starts_with("https://") { + return ArtRef::Fetch { + url: token.to_string(), + cache_key: token.to_string(), + }; + } + if token.starts_with('/') { + return ArtRef::File(PathBuf::from(token)); + } + // A bare relative string is not something this source can resolve. + // `classify` returned `Empty` here too. + ArtRef::None + } + fn thumbnail(&self, art: &ArtRef, _size: ArtSize) -> ArtRef { // EXPENSIVE phase: this is where the decode+downscale happens, on the // caller's worker pool (`local_artwork::stream_cold`, @@ -872,4 +904,31 @@ mod tests { let err = claimed(&raw).unwrap_err(); assert!(matches!(err, SourceError::BadIdShape { by: SourceId::LOCAL, .. })); } + + // ── artwork_token: the LocalFile arm of the dead `classify` ───────────── + + /// The four shapes a local row's token really takes. `file://` must come + /// back as a PATH — reqwest cannot even build a request for that scheme, + /// so treating it as fetchable is a guaranteed miss. + #[test] + fn a_local_token_is_a_path_a_cdn_url_or_nothing() { + let l = LocalSource::new(); + assert_eq!( + l.artwork_token("file:///home/u/.cache/qbz/thumb.jpg", ArtSize::Card), + ArtRef::File(PathBuf::from("/home/u/.cache/qbz/thumb.jpg")) + ); + assert_eq!( + l.artwork_token("/home/u/Music/Album/cover.jpg", ArtSize::Card), + ArtRef::File(PathBuf::from("/home/u/Music/Album/cover.jpg")) + ); + // An offline-download row can keep the CDN url its cover came from. + assert!(matches!( + l.artwork_token("https://static.qobuz.com/a.jpg", ArtSize::Card), + ArtRef::Fetch { .. } + )); + assert_eq!(l.artwork_token("", ArtSize::Card), ArtRef::None); + assert_eq!(l.artwork_token(" ", ArtSize::Card), ArtRef::None); + // A bare relative string is not resolvable — `classify` said Empty too. + assert_eq!(l.artwork_token("covers/a.jpg", ArtSize::Card), ArtRef::None); + } } diff --git a/crates/qbz-source/src/sources/plex.rs b/crates/qbz-source/src/sources/plex.rs index a0ed4926c..57db0ab77 100644 --- a/crates/qbz-source/src/sources/plex.rs +++ b/crates/qbz-source/src/sources/plex.rs @@ -395,6 +395,27 @@ impl Source for PlexSource { } } + fn artwork_token(&self, token: &str, size: ArtSize) -> ArtRef { + // The Plex arm of `artwork_qt::classify` (artwork_qt.rs:168-178), now + // owned by the source that knows what a `/library/...` path is. The + // caller no longer has to have heard of `is_thumb_path`. + let token = token.trim(); + if token.is_empty() || !is_thumb_path(token) { + return ArtRef::None; + } + let Some((base, token_secret)) = self.server() else { + // Distinct from `None` so the miss is logged for what it is — + // `ArtUrl::PlexUnconfigured`'s reason for existing. + return ArtRef::Unavailable("plex is not connected"); + }; + ArtRef::Fetch { + url: qbz_models::plex_thumb_url(&base, &token_secret, token, size.plex_px()), + // The RAW `/library/...` path is the stable memo key; the tokenized + // url is rebuilt every pass (artwork_qt.rs:231-238). + cache_key: token.to_string(), + } + } + async fn playback( &self, item: &MediaRef, @@ -705,4 +726,38 @@ mod tests { ..Default::default() })); } + + // ── artwork_token: the Plex arm of the dead `classify` ────────────────── + + /// With no credentials a Plex thumb is UNAVAILABLE, never `None`: the art + /// exists, the server does not answer yet, and the two must stay + /// distinguishable or the miss is logged as a dead download. + #[test] + fn a_thumb_path_without_credentials_is_unavailable_not_none() { + let p = PlexSource::new(); + assert!(matches!( + p.artwork_token("/library/metadata/42/thumb/1", ArtSize::Card), + ArtRef::Unavailable(_) + )); + } + + /// Anything that is not one of the two declared thumb prefixes is not + /// Plex's to interpret — including an absolute path, which `classify` + /// would have called a LOCAL FILE purely because it starts with `/`. + #[test] + fn only_the_declared_thumb_prefixes_are_claimed() { + let p = PlexSource::new(); + assert!(matches!(p.artwork_token("", ArtSize::Card), ArtRef::None)); + assert!(matches!( + p.artwork_token("/home/u/Music/Album/cover.jpg", ArtSize::Card), + ArtRef::None + )); + assert!(matches!( + p.artwork_token("https://static.qobuz.com/a.jpg", ArtSize::Card), + ArtRef::None + )); + // Both prefixes the taxonomy declares. + assert!(is_thumb_path("/library/metadata/42/thumb/1")); + assert!(is_thumb_path("/photo/:/transcode")); + } } diff --git a/crates/qbz-source/src/sources/qobuz.rs b/crates/qbz-source/src/sources/qobuz.rs index 49d8b0827..b295f588c 100644 --- a/crates/qbz-source/src/sources/qobuz.rs +++ b/crates/qbz-source/src/sources/qobuz.rs @@ -405,6 +405,22 @@ impl Source for QobuzSource { } } + fn artwork_token(&self, token: &str, size: ArtSize) -> ArtRef { + // A Qobuz row carries its cover as a CDN url outright, so unlike + // `artwork` this needs no memo and is never `Unavailable`. + let token = token.trim(); + if !(token.starts_with("http://") || token.starts_with("https://")) { + return ArtRef::None; + } + ArtRef::Fetch { + url: Self::small_url(token, size.qobuz_px()), + // The RAW url is the stable key ACROSS sizes — downscaling is a + // query the CDN answers, not a different cover. Keying on the + // sized url would download the same art once per size class. + cache_key: token.to_string(), + } + } + async fn playback( &self, item: &MediaRef,