Merge pull request #2391 from MMexicann/patch-3

fix: escape Windows device names in generated exports
This commit is contained in:
BFlat authored and GitHub committed 2026-11-17 18:25:15 +07:00
commit bf29f7731e
5 files changed
+76 -3

No files matched your search

@@ -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);
+29 -1
View File
@@ -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));
}
}