fix(paths): one file:// helper and a shape-based absolute-path test
Seven sites built format!("file://{p}") by hand or tested starts_with('/')
for "absolute path". Both are Unix-only: file://C:/... parses the drive
letter as the URL HOST, and C:\... does not start with '/'. On Windows
that silently cost every album cover, every runtime-tinted icon and the
local folder tree - a URL that resolves to nothing renders as an empty
rectangle, not as an error, so nothing complained.
qbz_models::fs_url now owns file_url / local_path / is_local_abs_path,
deciding by path SHAPE (POSIX, drive, UNC) rather than by host, so the
tests run and fail everywhere rather than only on Windows.
Linux behaviour is unchanged, deliberately: the backslash-to-slash rewrite
fires only for a Windows-shaped input, because a backslash is a legal
filename character on Unix and rewriting it would point /home/v/AC\DC at
a directory that does not exist. There is a test for exactly that.
Three of the seven sites were not in the plan's list and were found by its
step 8 grep: local_ephemeral.rs, local_playlist_qt.rs, playback_qt.rs.
Two starts_with('/') hits stay and are not local-path tests:
qbz-library/src/mount_info.rs is cfg(linux) mount-point arithmetic, and
qbz-theme's is parsing a COSMIC desktop config that Windows never reads.
cc
This commit is contained in:
9 files changed
+203
-42
No files matched your search
@@ -0,0 +1,176 @@
|
||||
//! `file://` URLs and "is this an absolute local path" — ONE definition.
|
||||
//!
|
||||
//! Sites used to build `format!("file://{p}")` by hand and to test
|
||||
//! `starts_with('/')` for "absolute path". Both are Unix-only:
|
||||
//! `file://C:\Users\…` parses `C:` as a URL **host**, and `C:\…` does not
|
||||
//! start with `/`. Windows lost every local cover, the folder tree and the
|
||||
//! runtime-tinted icons — silently, because a URL that resolves to nothing
|
||||
//! renders as an empty rectangle, not as an error. Everything routes through
|
||||
//! here now.
|
||||
|
||||
/// Absolute local path, by SHAPE (so the tests run on every host):
|
||||
/// `/…` (POSIX), `X:\…` / `X:/…` (drive), `\\server\share…` (UNC).
|
||||
///
|
||||
/// Shape, not `Path::is_absolute`: that answers for the HOST it runs on, so a
|
||||
/// Linux CI run would call `C:\x` relative and the Windows-only regression
|
||||
/// would pass every test on the machine that runs them.
|
||||
pub fn is_local_abs_path(s: &str) -> bool {
|
||||
if s.starts_with('/') {
|
||||
return true;
|
||||
}
|
||||
let b = s.as_bytes();
|
||||
if b.len() >= 3 && b[0].is_ascii_alphabetic() && b[1] == b':' && (b[2] == b'\\' || b[2] == b'/')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
s.starts_with("\\\\")
|
||||
}
|
||||
|
||||
/// `file://` URL for a raw filesystem path. Idempotent for inputs that already
|
||||
/// carry the scheme.
|
||||
///
|
||||
/// Qt's `QUrl` parses `#` as a fragment and `?` as a query, so a cover under
|
||||
/// `…/Album #1/cover.jpg` resolves to nothing when concatenated raw.
|
||||
/// Percent-encode exactly those two plus `%` itself (first, or the escapes we
|
||||
/// add would be double-decoded). Spaces are left alone: `QUrl` accepts them.
|
||||
///
|
||||
/// Backslashes become `/` only for a Windows-SHAPED input (drive or UNC):
|
||||
/// on Unix a backslash is a legal filename character. A drive path gets
|
||||
/// the empty-authority form
|
||||
/// `file:///C:/…`, which is what `QUrl` and WinRT both accept. A UNC path puts
|
||||
/// the server in the authority — `file://nas/music/x.jpg` — because
|
||||
/// `file:////nas/…` is not a form anything resolves.
|
||||
pub fn file_url(path: &str) -> String {
|
||||
if path.starts_with("file://") {
|
||||
return path.to_string();
|
||||
}
|
||||
// Backslash-to-slash ONLY for Windows-shaped inputs. A backslash is a
|
||||
// LEGAL filename character on Unix, so rewriting it unconditionally would
|
||||
// point `/home/v/AC\DC/cover.jpg` at a directory that does not exist --
|
||||
// the same defect class as rewriting separators in a qrc alias.
|
||||
let win_shaped = path.starts_with("\\\\")
|
||||
|| {
|
||||
let b = path.as_bytes();
|
||||
b.len() >= 3 && b[0].is_ascii_alphabetic() && b[1] == b':'
|
||||
};
|
||||
let normalised = if win_shaped {
|
||||
path.replace('\\', "/")
|
||||
} else {
|
||||
path.to_string()
|
||||
};
|
||||
let mut out = String::with_capacity(normalised.len() + 8);
|
||||
out.push_str("file://");
|
||||
let body: &str = if let Some(unc) = normalised.strip_prefix("//") {
|
||||
unc
|
||||
} else if normalised.starts_with('/') {
|
||||
&normalised
|
||||
} else {
|
||||
// `C:/x` and any relative remnant: the empty authority needs the
|
||||
// third slash.
|
||||
out.push('/');
|
||||
&normalised
|
||||
};
|
||||
for ch in body.chars() {
|
||||
match ch {
|
||||
'%' => out.push_str("%25"),
|
||||
'#' => out.push_str("%23"),
|
||||
'?' => out.push_str("%3F"),
|
||||
_ => out.push(ch),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The inverse of [`file_url`]: a `file://` URL back to a filesystem path.
|
||||
///
|
||||
/// `trim_start_matches("file://")` is NOT the inverse — it leaves the three
|
||||
/// percent-escapes in place and, on Windows, a leading `/` before the drive,
|
||||
/// so the result names a file that does not exist. That silently broke
|
||||
/// whatever opened it (a tint decode, a copy) for exactly the paths the
|
||||
/// escaping exists to support.
|
||||
pub fn local_path(url: &str) -> String {
|
||||
let raw = url.strip_prefix("file://").unwrap_or(url);
|
||||
let raw = raw
|
||||
.replace("%23", "#")
|
||||
.replace("%3F", "?")
|
||||
.replace("%25", "%");
|
||||
// `file:///C:/x` → raw `/C:/x` → `C:/x`.
|
||||
let b = raw.as_bytes();
|
||||
if b.len() >= 3 && b[0] == b'/' && b[1].is_ascii_alphabetic() && b[2] == b':' {
|
||||
return raw[1..].to_string();
|
||||
}
|
||||
raw
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn posix_paths_round_trip_and_escape() {
|
||||
assert_eq!(
|
||||
file_url("/home/v/Album #1/cover?.jpg"),
|
||||
"file:///home/v/Album %231/cover%3F.jpg"
|
||||
);
|
||||
assert_eq!(
|
||||
local_path("file:///home/v/Album%20%231/c%25.jpg"),
|
||||
"/home/v/Album%20#1/c%.jpg"
|
||||
);
|
||||
assert_eq!(file_url("file:///already"), "file:///already");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drive_paths_get_the_empty_authority_form() {
|
||||
assert_eq!(
|
||||
file_url("C:\\Users\\v\\cover.jpg"),
|
||||
"file:///C:/Users/v/cover.jpg"
|
||||
);
|
||||
assert_eq!(
|
||||
file_url("C:/Users/v/cover.jpg"),
|
||||
"file:///C:/Users/v/cover.jpg"
|
||||
);
|
||||
assert_eq!(
|
||||
local_path("file:///C:/Users/v/cover.jpg"),
|
||||
"C:/Users/v/cover.jpg"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unc_paths_put_the_server_in_the_authority() {
|
||||
assert_eq!(
|
||||
file_url("\\\\nas\\music\\cover.jpg"),
|
||||
"file://nas/music/cover.jpg"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drive_paths_round_trip_through_both_directions() {
|
||||
// The regression that started this: a tinted icon directory.
|
||||
let p = "C:\\Users\\blitz\\AppData\\Local\\qbz\\icon-tints\\4285f4";
|
||||
let u = file_url(p);
|
||||
assert_eq!(u, "file:///C:/Users/blitz/AppData/Local/qbz/icon-tints/4285f4");
|
||||
assert_eq!(local_path(&u), "C:/Users/blitz/AppData/Local/qbz/icon-tints/4285f4");
|
||||
assert_eq!(file_url(&u), u, "file_url must be idempotent");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_backslash_in_a_unix_filename_is_not_a_separator() {
|
||||
// Legal on Unix; rewriting it would name a path that does not exist.
|
||||
assert_eq!(
|
||||
file_url("/home/v/AC\\DC/cover.jpg"),
|
||||
"file:///home/v/AC\\DC/cover.jpg"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absolute_path_shape_is_host_independent() {
|
||||
assert!(is_local_abs_path("/x"));
|
||||
assert!(is_local_abs_path("C:\\x"));
|
||||
assert!(is_local_abs_path("c:/x"));
|
||||
assert!(is_local_abs_path("\\\\nas\\x"));
|
||||
assert!(!is_local_abs_path("x/y"));
|
||||
assert!(!is_local_abs_path("https://x"));
|
||||
assert!(!is_local_abs_path("Album|Artist"));
|
||||
assert!(!is_local_abs_path("C:x")); // drive-relative is not absolute
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@
|
||||
|
||||
pub mod error;
|
||||
pub mod events;
|
||||
pub mod fs_url;
|
||||
pub mod lenient;
|
||||
pub mod mixtape;
|
||||
pub mod playback;
|
||||
|
||||
@@ -219,7 +219,7 @@ pub fn classify(url: &str) -> ArtUrl {
|
||||
ArtUrl::Plex(fetch)
|
||||
};
|
||||
}
|
||||
if url.starts_with('/') {
|
||||
if qbz_models::fs_url::is_local_abs_path(url) {
|
||||
return ArtUrl::LocalFile(url.to_string());
|
||||
}
|
||||
ArtUrl::Empty
|
||||
@@ -240,27 +240,11 @@ pub fn classify(url: &str) -> ArtUrl {
|
||||
/// result (a tint decode, a copy) for exactly the files the escaping exists to
|
||||
/// support.
|
||||
pub fn local_path(url: &str) -> String {
|
||||
let raw = url.strip_prefix("file://").unwrap_or(url);
|
||||
raw.replace("%23", "#")
|
||||
.replace("%3F", "?")
|
||||
.replace("%25", "%")
|
||||
qbz_models::fs_url::local_path(url)
|
||||
}
|
||||
|
||||
pub fn file_url(path: &str) -> String {
|
||||
if path.starts_with("file://") {
|
||||
return path.to_string();
|
||||
}
|
||||
let mut out = String::with_capacity(path.len() + 8);
|
||||
out.push_str("file://");
|
||||
for ch in path.chars() {
|
||||
match ch {
|
||||
'%' => out.push_str("%25"),
|
||||
'#' => out.push_str("%23"),
|
||||
'?' => out.push_str("%3F"),
|
||||
_ => out.push(ch),
|
||||
}
|
||||
}
|
||||
out
|
||||
qbz_models::fs_url::file_url(path)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -622,8 +622,12 @@ impl qbz_icon_tint::QbzIconTint {
|
||||
}
|
||||
}
|
||||
|
||||
// Absolute paths start with '/', so this yields file:///home/...
|
||||
QString::from(&format!("file://{}", root.join(&hex).display()))
|
||||
// NOT format!("file://{path}"): on Windows that yields
|
||||
// file://C:/... where the drive parses as the URL HOST, and the
|
||||
// icon renders as an empty rectangle rather than an error.
|
||||
QString::from(&qbz_models::fs_url::file_url(
|
||||
&root.join(&hex).to_string_lossy(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -791,7 +791,7 @@ pub fn set_session_artwork(path: &str) {
|
||||
// `file://`), because that is the form the whole taxonomy downstream
|
||||
// expects.
|
||||
let ids: Vec<u64> = tracks.iter().map(|t| t.id as u64).collect();
|
||||
let url = format!("file://{path}");
|
||||
let url = qbz_models::fs_url::file_url(&path);
|
||||
crate::spawn(async move {
|
||||
let runtime = crate::app();
|
||||
if runtime.core().patch_queue_artwork(&ids, &url).await {
|
||||
|
||||
@@ -976,9 +976,13 @@ mod tests {
|
||||
assert!(one.artwork_url.as_deref().unwrap().contains("Disc 01"));
|
||||
assert!(five.artwork_url.as_deref().unwrap().contains("Disc 05"));
|
||||
let one_resolved = crate::artwork_qt::cached_path(one.artwork_url.as_deref().unwrap());
|
||||
// Compare separator-normalised: a `file://` URL is always `/`-separated,
|
||||
// so the round trip through it cannot hand back a host's backslashes.
|
||||
// Both forms open the same file on Windows.
|
||||
let slash = |p: &str| p.replace('\\', "/");
|
||||
assert_eq!(
|
||||
crate::artwork_qt::local_path(&one_resolved),
|
||||
disc_one.join("cover.jpg").to_string_lossy()
|
||||
slash(&crate::artwork_qt::local_path(&one_resolved)),
|
||||
slash(&disc_one.join("cover.jpg").to_string_lossy())
|
||||
);
|
||||
|
||||
std::fs::remove_dir_all(&box_dir).unwrap();
|
||||
|
||||
@@ -719,13 +719,9 @@ pub(crate) fn row_to_display(item: &RowItem) -> (PlaylistTrackRow, Option<QueueT
|
||||
album: album.clone(),
|
||||
album_version: None,
|
||||
duration_secs: *duration_secs,
|
||||
artwork_url: artwork_path.clone().map(|path| {
|
||||
if path.starts_with("file://") {
|
||||
path
|
||||
} else {
|
||||
format!("file://{path}")
|
||||
}
|
||||
}),
|
||||
artwork_url: artwork_path
|
||||
.clone()
|
||||
.map(|path| qbz_models::fs_url::file_url(&path)),
|
||||
hires: bit_depth.map(|depth| depth >= 24).unwrap_or(false),
|
||||
bit_depth: *bit_depth,
|
||||
sample_rate: *sample_rate,
|
||||
|
||||
@@ -1168,13 +1168,7 @@ async fn offline_album_queue(album_id: &str) -> Result<Vec<QueueTrack>, String>
|
||||
let artwork = local
|
||||
.and_then(|row| row.artwork_path.clone())
|
||||
.or_else(|| cached.resolve_cover_path(&cache_path))
|
||||
.map(|path| {
|
||||
if path.starts_with("file://") {
|
||||
path
|
||||
} else {
|
||||
format!("file://{path}")
|
||||
}
|
||||
});
|
||||
.map(|path| qbz_models::fs_url::file_url(&path));
|
||||
let album = cached.album.clone().unwrap_or_default();
|
||||
let sample_rate = cached
|
||||
.sample_rate
|
||||
|
||||
@@ -73,10 +73,12 @@ pub fn local_queue_track(t: &LocalTrack) -> QueueTrack {
|
||||
// so the now-playing bar / queue resolve it from current creds.
|
||||
// `file://`-prefixing it poisons it into a local-read miss.
|
||||
let artwork_url = t.artwork_path.as_ref().map(|p| {
|
||||
if is_plex || p.starts_with("file://") {
|
||||
if is_plex {
|
||||
p.clone()
|
||||
} else {
|
||||
format!("file://{p}")
|
||||
// Idempotent, and drive/UNC-aware: format!("file://{p}") turned
|
||||
// C:/... into a URL whose HOST was the drive letter.
|
||||
qbz_models::fs_url::file_url(p)
|
||||
}
|
||||
});
|
||||
let sample_rate_khz = if t.sample_rate >= 1000.0 {
|
||||
@@ -238,7 +240,7 @@ impl LocalSource {
|
||||
/// `<album>|||<album_artist>` form), and the literal unknown bucket
|
||||
/// (`library_qt.rs:1053`).
|
||||
fn is_group_key(id: &str) -> bool {
|
||||
id.starts_with('/') || id.contains('|') || id == "__unknown_album__"
|
||||
qbz_models::fs_url::is_local_abs_path(id) || id.contains('|') || id == "__unknown_album__"
|
||||
}
|
||||
|
||||
/// True when `id` is a Plex namespaced row id (`local_plex.rs:37-42`).
|
||||
@@ -678,7 +680,7 @@ impl Source for LocalSource {
|
||||
cache_key: token.to_string(),
|
||||
};
|
||||
}
|
||||
if token.starts_with('/') {
|
||||
if qbz_models::fs_url::is_local_abs_path(token) {
|
||||
return ArtRef::File(PathBuf::from(token));
|
||||
}
|
||||
// A bare relative string is not something this source can resolve.
|
||||
|
||||
Reference in new issue
Block a user