mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-17 23:58:24 +00:00
Merge pull request #2391 from MMexicann/patch-3
fix: escape Windows device names in generated exports
This commit is contained in:
5 files changed
+76
-3
No files matched your search
@@ -242,7 +242,10 @@ pub fn info(path: &str, as_json: bool) -> CliResult<ExitCode> {
|
||||
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());
|
||||
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::<String>();
|
||||
// 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.
|
||||
@@ -543,6 +546,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} ~é");
|
||||
@@ -553,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");
|
||||
|
||||
@@ -397,6 +397,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::<Vec<_>>().join("/")
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static ROOTS: RefCell<Option<Arc<Roots>>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
@@ -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: `<tmp>/figcraft-roots-<name>-<pid>`, holding `root/`, `root2/`
|
||||
/// (a sibling whose name starts like the root's) and `outside/`.
|
||||
struct Fixture(PathBuf);
|
||||
|
||||
@@ -295,7 +295,7 @@ pub fn jobs_for(items: impl IntoIterator<Item = (NodeId, Vec<ExportSetting>)>, 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<Value> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -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`…
|
||||
- `<out>` 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.
|
||||
|
||||
Reference in new issue
Block a user