Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

An HTML page's files load on Windows (fixes #40, fixes #41, fixes #42). The HTML runtime was handed the page's folder as Windows wrote it: canonicalized to a verbatim \\?\D:\ path (#40), or as D:/..., which a URL reads as scheme d (#41). The cached Google Fonts face came back from html.read as C:\... (#42). Its backslashes became CSS escapes in url(), and the drive a URL scheme in src. Now the folder is canonicalized without the verbatim prefix (raea_io_home::plain), and the runtime gets every path in URL form: raea_io_home::url_path, /D:/work/page on Windows, which the page's file:/// base joins into file:///D:/work/page. That covers the page folder, the font folder and html.read's answers. What the runtime names back (html.read's urls, a video's file, a sound's src) is read through from_url_path. Nothing changes on Unix, where both are the path itself. io/home's test checks the forms on any system, and joins them to file:/// as Blitz does: the result is a file URL, not scheme d

shinyobjectzd45b38ef6038
diff --git a/Cargo.lock b/Cargo.lock
index 22931658..0f18cc46 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14203,6 +14203,7 @@ name = "raea-io-home"
version = "0.1.0"
dependencies = [
"serde_json",
+ "url",
]
[[package]]
diff --git a/io/home/Cargo.toml b/io/home/Cargo.toml
index 2419897d..17e18f8f 100644
--- a/io/home/Cargo.toml
+++ b/io/home/Cargo.toml
@@ -8,3 +8,7 @@ description = "Where raea's own files are ($RAEA_HOME, an install's share/raea,
[dependencies]
serde_json = "1"
+
+[dev-dependencies]
+# a page's file:/// base joined as Blitz joins it (the url_path test)
+url = "2"
diff --git a/io/home/src/lib.rs b/io/home/src/lib.rs
index 53b1976f..35dcf463 100644
--- a/io/home/src/lib.rs
+++ b/io/home/src/lib.rs
@@ -101,11 +101,37 @@ pub fn src(p: &std::path::Path) -> String {
/// `src` of the path `s` as Windows writes it (`windows`) or as Unix does, whichever system asks (a test of Windows'
/// form runs on the Mac too)
pub fn src_on(s: &str, windows: bool) -> String {
+ let u = url_path_on(s, windows);
+ if u != s && u.as_bytes().get(2) == Some(&b':') { format!("file://{u}") } else { s.to_string() }
+}
+
+/// A local file as a page names it in a URL's path: its path on Unix; on Windows `/C:/x/y` (the path of the file URL
+/// file:///C:/x/y, which a page's file:/// base joins it to), its verbatim prefix gone and its backslashes slashes.
+/// Written `C:\x`, a URL reads `c` as its scheme and CSS reads the backslashes as escapes (#40, #41, #42).
+pub fn url_path(p: &std::path::Path) -> String {
+ url_path_on(&p.display().to_string(), cfg!(windows))
+}
+
+/// `url_path` of the path `s` as Windows writes it (`windows`) or as Unix does, whichever system asks
+pub fn url_path_on(s: &str, windows: bool) -> String {
if !windows {
return s.to_string();
}
let s = drive(s).unwrap_or(s);
- if s.as_bytes().get(1) == Some(&b':') { format!("file:///{}", s.replace('\\', "/")) } else { s.to_string() }
+ if s.as_bytes().get(1) == Some(&b':') { format!("/{}", s.replace('\\', "/")) } else { s.to_string() }
+}
+
+/// A path a page named (`url_path`'s `/C:/x`, or any other) as the file it is
+pub fn from_url_path(s: &str) -> std::path::PathBuf {
+ from_url_path_on(s, cfg!(windows)).into()
+}
+
+/// `from_url_path` as Windows reads it (`windows`) or as Unix does
+pub fn from_url_path_on(s: &str, windows: bool) -> &str {
+ match s.strip_prefix('/') {
+ Some(r) if windows && r.as_bytes().get(1) == Some(&b':') => r,
+ _ => s,
+ }
}
#[cfg(test)]
@@ -118,6 +144,23 @@ mod tests {
assert_eq!(super::src_on("assets/m.png", true), "assets/m.png");
}
+ #[test]
+ fn a_page_names_a_windows_file_by_its_url_path() {
+ use super::{from_url_path_on, url_path_on};
+ assert_eq!(url_path_on("/Users/a/page", false), "/Users/a/page");
+ assert_eq!(url_path_on(r"D:\work\page", true), "/D:/work/page");
+ assert_eq!(url_path_on(r"\\?\D:\06.AI\banana-hf5", true), "/D:/06.AI/banana-hf5");
+ assert_eq!(url_path_on(r"C:\Users\a\AppData\Local\raea\cache\f.woff2", true), "/C:/Users/a/AppData/Local/raea/cache/f.woff2");
+ assert_eq!(from_url_path_on("/D:/work/page/assets/hero.png", true), "D:/work/page/assets/hero.png");
+ assert_eq!(from_url_path_on("/D:/x", false), "/D:/x");
+ assert_eq!(from_url_path_on("assets/hero.png", true), "assets/hero.png");
+ // joined to a page's file:/// base as Blitz joins it, a drive's path is a file URL, not scheme `d` (#41)
+ for p in [r"D:\work\page", r"\\?\D:\work\page"] {
+ let u = url::Url::parse("file:///").unwrap().join(&format!("{}/assets/hero.png", url_path_on(p, true))).unwrap();
+ assert_eq!((u.scheme(), u.path()), ("file", "/D:/work/page/assets/hero.png"));
+ }
+ }
+
#[test]
fn home_is_no_verbatim_path() {
use std::path::PathBuf;
diff --git a/runtime/encode/src/html.rs b/runtime/encode/src/html.rs
index 374758f1..9778a532 100644
--- a/runtime/encode/src/html.rs
+++ b/runtime/encode/src/html.rs
@@ -37,7 +37,8 @@ fn local(dir: &Path, src: &str) -> Result<String, String> {
return Ok(raea_io_net::cache::fetch(src)?.to_string_lossy().into_owned());
}
let src = src.strip_prefix("file://").unwrap_or(src);
- Ok(dir.join(src).to_string_lossy().into_owned())
+ // (a path the runtime made absolute is url_path's, `/D:/x` on Windows)
+ Ok(dir.join(raea_io_home::from_url_path(src)).to_string_lossy().into_owned())
}
/// Lower an HTML composition: the page opened and probed for its length and sound, over a white canvas as a browser
diff --git a/runtime/js/src/html_page.rs b/runtime/js/src/html_page.rs
index cbbddc5f..6de1a3a9 100644
--- a/runtime/js/src/html_page.rs
+++ b/runtime/js/src/html_page.rs
@@ -35,14 +35,16 @@ pub fn read_answer(dir: &Path, args: &Value) -> R<Value> {
let path = if url.starts_with("http://") || url.starts_with("https://") {
raea_io_net::cache::fetch(url)?
} else {
- let rel = url.split(['?', '#']).next().unwrap_or(url);
- let p = if Path::new(rel).is_absolute() { PathBuf::from(rel) } else { dir.join(rel) };
+ // (a path the runtime made absolute is url_path's, `/C:/x` on Windows)
+ let rel = raea_io_home::from_url_path(url.split(['?', '#']).next().unwrap_or(url));
+ let p = if rel.is_absolute() { rel } else { dir.join(rel) };
if !p.is_file() {
return Err(format!("{url}: not found"));
}
p
};
- let mut out = json!({ "path": path.to_string_lossy() });
+ // as a page names it (url_path): the runtime writes it into url()s and src attributes (#42)
+ let mut out = json!({ "path": raea_io_home::url_path(&path) });
if !binary {
let bytes = std::fs::read(&path).map_err(|e| format!("{}: {e}", path.display()))?;
out["text"] = Value::String(String::from_utf8_lossy(&bytes).into_owned());
@@ -131,11 +133,12 @@ pub struct Prepared {
pub fn prepare(file: &Path, fps: f64, fonts: Option<&Path>, size: Option<(u32, u32)>, root: Option<&Path>) -> R<Prepared> {
let file = if file.is_dir() { file.join("index.html") } else { file.to_path_buf() };
let source = std::fs::read_to_string(&file).map_err(|e| format!("{}: {e}", file.display()))?;
- let file = file.canonicalize().unwrap_or(file);
+ // (plain: Windows canonicalizes to a verbatim `\\?\D:\` path, #40)
+ let file = file.canonicalize().map(raea_io_home::plain).unwrap_or(file);
// served: the page's folder is `root` and the page a path under it (`/x` is then root's x, as a server rooted
// there serves it); the project listing is left out (a WPT checkout is too big to list, and only a
// composition's sub-compositions use it)
- let served = root.and_then(|r| r.canonicalize().ok()).filter(|r| file.starts_with(r));
+ let served = root.and_then(|r| r.canonicalize().ok()).map(raea_io_home::plain).filter(|r| file.starts_with(r));
let (dir, name, files) = match &served {
Some(r) => (r.clone(), file.strip_prefix(r).unwrap_or(&file).to_string_lossy().into_owned(), vec![]),
None => {
@@ -151,11 +154,12 @@ pub fn prepare(file: &Path, fps: f64, fonts: Option<&Path>, size: Option<(u32, u
.map(|rd| rd.flatten().map(|e| e.file_name().to_string_lossy().into_owned()).filter(|n| n.ends_with(".woff2")).collect())
.unwrap_or_default();
let globals = json!({
- "fonts": fonts.map(|d| d.to_string_lossy().into_owned()),
+ // the folders as a page names them in URLs (url_path: `/D:/x` on Windows, never `D:\x`, #40-#42)
+ "fonts": fonts.map(raea_io_home::url_path),
"fontFiles": font_files,
"source": source,
"file": name,
- "dir": dir.to_string_lossy(),
+ "dir": raea_io_home::url_path(&dir),
"files": files,
"served": served.is_some(),
"fps": fps,
@@ -186,7 +190,7 @@ pub fn probe_of(file: &Path, ready: &Value, (w, h): (u32, u32)) -> R<Probe> {
pub fn videos_of(done: &Value) -> Vec<VideoAt> {
done["videos"]
.as_array()
- .map(|a| a.iter().filter_map(|v| Some(VideoAt { id: v["id"].as_u64()?, file: PathBuf::from(v["file"].as_str()?), at: v["at"].as_f64()?, fit: None })).collect())
+ .map(|a| a.iter().filter_map(|v| Some(VideoAt { id: v["id"].as_u64()?, file: raea_io_home::from_url_path(v["file"].as_str()?), at: v["at"].as_f64()?, fit: None })).collect())
.unwrap_or_default()
}