From 8244acce2d37c0e6dc1b25ffa45c7de251020cd3 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:58:13 +0100 Subject: [PATCH 1/6] cli: escape device names in generated export filenames Use the shared device-name escaping rule for generated CLI export basenames, before existing length caps. --- apps/figcraft-cli/src/files.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/figcraft-cli/src/files.rs b/apps/figcraft-cli/src/files.rs index 165057086..8c606236f 100644 --- a/apps/figcraft-cli/src/files.rs +++ b/apps/figcraft-cli/src/files.rs @@ -242,6 +242,7 @@ pub fn info(path: &str, as_json: bool) -> CliResult { fn sanitize(name: &str) -> String { let s = figcraft_engine::cmd::file::safe_file_name(name); let s = s.trim_matches(|c: char| c == '.' || c.is_whitespace()); + let s = figcraft_engine::file_access::escape_device_names(s); if s.is_empty() { "layer".into() } else { s.char_indices().take(100).take_while(|(i, c)| i + c.len_utf8() <= 200).map(|(_, c)| c).collect() } } @@ -543,6 +544,10 @@ mod tests { assert_eq!(sanitize("Home / Hero"), "Home _ Hero"); assert_eq!(sanitize("../.."), "_"); assert_eq!(sanitize(""), "layer"); + assert_eq!(sanitize("CON"), "_CON"); + assert_eq!(sanitize(" .nul.txt. "), "_nul.txt"); + assert_eq!(sanitize("COM1"), "_COM1"); + assert_eq!(sanitize("HomeCON"), "HomeCON"); // The same characters the app's export keeps (#1154): only what file systems refuse goes. assert_eq!(sanitize("Homepage - desktop (live + verbeteringen)"), "Homepage - desktop (live + verbeteringen)"); assert_eq!(sanitize("Q&A, 50% #1 [v2] {x} ~é"), "Q&A, 50% #1 [v2] {x} ~é"); From af87503d04a1411caa145bed0b393aff7f83dccc Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:59:34 +0100 Subject: [PATCH 2/6] Implement escape_device_names function Prefix generated path components using the existing device classifier, after filtering and suffix construction. --- crates/engine/src/automation/file_access.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/engine/src/automation/file_access.rs b/crates/engine/src/automation/file_access.rs index 249d3483c..8d04488e6 100644 --- a/crates/engine/src/automation/file_access.rs +++ b/crates/engine/src/automation/file_access.rs @@ -345,6 +345,12 @@ fn device_name(n: &str) -> bool { && stem.chars().last().is_some_and(|c| c.is_ascii_digit() || matches!(c, '¹' | '²' | '³'))) } +/// Prefix Windows device-name components of a generated export path with `_`. +/// Apply after character filtering and suffix construction, so a safe suffix stays unchanged. +pub fn escape_device_names(path: &str) -> String { + path.split('/').map(|name| if device_name(name) { format!("_{name}") } else { name.to_string() }).collect::>().join("/") +} + thread_local! { static ROOTS: RefCell>> = const { RefCell::new(None) }; } From 19a4a309e4a8d4db9d23d404f6e1f7a9393d0a87 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:01:13 +0100 Subject: [PATCH 3/6] test: cover generated device-name path components Exercise device names, extensions, nested components and safe near-matches using the existing device-name classification. --- .../src/automation/file_access/tests.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/engine/src/automation/file_access/tests.rs b/crates/engine/src/automation/file_access/tests.rs index d642befb1..0e80de0f6 100644 --- a/crates/engine/src/automation/file_access/tests.rs +++ b/crates/engine/src/automation/file_access/tests.rs @@ -9,6 +9,27 @@ use serde_json::json; use super::{Access, Roots, allow, confined, current}; use crate::Session; +#[test] +fn generated_export_paths_escape_device_components_and_preserve_other_names() { + for (path, expected) in [ + ("CON", "_CON"), + ("CoN.txt", "_CoN.txt"), + ("nul.txt", "_nul.txt"), + ("PRN/icon", "_PRN/icon"), + ("folder/COM1", "folder/_COM1"), + ("LPT9.png", "_LPT9.png"), + ("COM¹", "_COM¹"), + ("LPT²/icon", "_LPT²/icon"), + ("CONIN$", "_CONIN$"), + ("HomeCON", "HomeCON"), + ("COM10", "COM10"), + ("CON@2x", "CON@2x"), + ("folder/.NUL", "folder/.NUL"), + ] { + assert_eq!(super::escape_device_names(path), expected, "{path}"); + } +} + /// A fresh folder for one test: `/figcraft-roots--`, holding `root/`, `root2/` /// (a sibling whose name starts like the root's) and `outside/`. struct Fixture(PathBuf); From 71cd63a21b5db85277db55bb01f0ec5577beba65 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:02:12 +0100 Subject: [PATCH 4/6] engine: escape device components after export suffixes Apply the shared device-name rule to completed folder/name/suffix components before collision numbering, with regression coverage. --- crates/engine/src/cmd/file/export.rs | 30 +++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/file/export.rs b/crates/engine/src/cmd/file/export.rs index bbc52e1b5..ac8353c30 100644 --- a/crates/engine/src/cmd/file/export.rs +++ b/crates/engine/src/cmd/file/export.rs @@ -295,7 +295,7 @@ pub fn jobs_for(items: impl IntoIterator)>, d if out.len() >= MAX_EXPORT_FILES { return out; } - let stem = format!("{base}{}", safe_file_name(&e.file_suffix())); + let stem = crate::file_access::escape_device_names(&format!("{base}{}", safe_file_name(&e.file_suffix()))); let ext = e.format.ext(); let file = (1..).map(|k| if k == 1 { format!("{stem}.{ext}") } else { format!("{stem} {k}.{ext}") }).find(|f| used.insert(f.to_lowercase())); @@ -439,3 +439,31 @@ pub(super) fn export_frames_pdf(s: &mut Session, p: &Value) -> Result { None => Ok(json!({"format": "pdf", "pages": pages, "data": base64::engine::general_purpose::STANDARD.encode(&bytes)})), } } + +#[cfg(test)] +mod filename_tests { + use super::*; + + #[test] + fn export_jobs_escape_device_names_after_suffixes_and_before_numbering() { + let mut doc = Document::new("Export names"); + let page = doc.first_page().unwrap(); + let cases = [ + ("C", "ON", "_CON.png"), + ("_con", "", "_con 2.png"), + ("CON/icon", "", "_CON/icon.png"), + ("Home", "CON", "HomeCON.png"), + ("COM", "1", "_COM1.png"), + ("COM1", "@2x", "COM1@2x.png"), + ]; + let items: Vec<_> = cases + .iter() + .map(|(name, suffix, _)| { + let id = doc.create(page, NodeKind::Frame, name, 0.0, 0.0, 10.0, 10.0).unwrap(); + (id, vec![ExportSetting { suffix: (*suffix).into(), ..Default::default() }]) + }) + .collect(); + let files: Vec<_> = jobs_for(items, &doc).into_iter().map(|job| job.file).collect(); + assert_eq!(files, cases.map(|(_, _, expected)| expected)); + } +} From 4382b9b5919b762d622b1541ee2b16abf87f006c Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:27:19 +0100 Subject: [PATCH 5/6] docs: explain generated device-name prefixes Document the reserved Windows name prefix alongside the existing generated export filename rules. --- docs/cli.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/cli.md b/docs/cli.md index a83fbe5c3..07b19e58b 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -118,7 +118,8 @@ page 1 "Screens": 380 nodes, 4 top-level after the frame (`Home.png`, `Settings.png`…); with no frames, one file per top-level layer. The name is kept as it is, except for the characters file systems refuse (`/ \ : * ? " < > |` and control characters), which become `_`, and leading or trailing spaces and dots, which are - dropped. A name is cut at 100 characters. The app's export replaces the same characters (there a + dropped. Reserved Windows device names such as `CON` get a leading `_`. + A name is cut at 100 characters. The app's export replaces the same characters (there a `/` makes a folder). Two layers with the same name get `Name 2`, `Name 3`… - `` has an image extension: one file with the given `--node`, or the whole page composited. - `--node` takes a layer id (`12` or `0:12`) or a layer name. From 14bdeff5916e0d56c51ae9638bb7d88094737744 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:30:31 +0100 Subject: [PATCH 6/6] fix: check device names after filename length limits Cap generated CLI names before device-name escaping, then cap again so the prefix fits both limits. Cover character and byte truncation boundaries. --- apps/figcraft-cli/src/files.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/figcraft-cli/src/files.rs b/apps/figcraft-cli/src/files.rs index 8c606236f..20cca566d 100644 --- a/apps/figcraft-cli/src/files.rs +++ b/apps/figcraft-cli/src/files.rs @@ -242,8 +242,10 @@ pub fn info(path: &str, as_json: bool) -> CliResult { fn sanitize(name: &str) -> String { let s = figcraft_engine::cmd::file::safe_file_name(name); let s = s.trim_matches(|c: char| c == '.' || c.is_whitespace()); - let s = figcraft_engine::file_access::escape_device_names(s); - if s.is_empty() { "layer".into() } else { s.char_indices().take(100).take_while(|(i, c)| i + c.len_utf8() <= 200).map(|(_, c)| c).collect() } + let cap = |s: &str| s.char_indices().take(100).take_while(|(i, c)| i + c.len_utf8() <= 200).map(|(_, c)| c).collect::(); + // Truncation can expose a device name; its escape must still fit the limits. + let s = figcraft_engine::file_access::escape_device_names(&cap(s)); + if s.is_empty() { "layer".into() } else { cap(&s) } } /// What `export` exports and how. @@ -558,6 +560,16 @@ mod tests { assert_eq!(sanitize(&"\u{1F600}".repeat(100)), "\u{1F600}".repeat(50), "at most 200 bytes, whole characters"); } + #[test] + fn sanitizes_names_that_become_devices_at_the_character_limit() { + assert_eq!(sanitize(&format!("CON{}X", " ".repeat(97))), format!("_CON{}", " ".repeat(96))); + } + + #[test] + fn sanitizes_names_that_become_devices_at_the_byte_limit() { + assert_eq!(sanitize(&format!("NUL{} X", "\u{2003}".repeat(65))), format!("_NUL{} ", "\u{2003}".repeat(65))); + } + #[test] fn default_page_skips_leading_divider() { let mut doc = Document::new("divider");