PhotoCraft: An open-source, photo editor written by scratch in pure Rust
File names: superscript COM/LPT ports, and `_` before the dot in web exports
Eddi Andree Salazar Matos196238fe95f2
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rsindex d312100a2..3abe51996 100644--- a/crates/engine/src/file_cmds.rs+++ b/crates/engine/src/file_cmds.rs@@ -273,25 +273,30 @@ pub(crate) fn join(dir: &str, name: &str) -> String {pub(crate) fn sanitize(name: &str) -> String {let s: String = name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_' | ' ' | '.') { c } else { '_' }).collect();let s = s.trim().trim_matches('.').to_string();- if s.is_empty() {- return "layer".into();- }- if !is_windows_device_name(&s) {- return s;+ if s.is_empty() { "layer".into() } else { portable_device_name(s) }+}++/// `name` with a `_` before its first dot when it is a Windows device name (`nul` → `nul_`,+/// `com1.v2` → `com1_.v2`); a `_` at the end would leave the device stem in place.+pub(crate) fn portable_device_name(name: String) -> String {+ if !is_windows_device_name(&name) {+ return name;}- match s.split_once('.') {+ match name.split_once('.') {Some((device, rest)) => format!("{device}_.{rest}"),- None => format!("{s}_"),+ None => format!("{name}_"),}}/// Whether `component` names a Windows device (`CON`, `NUL`, `COM1`…, with any extension):-/// Windows opens the device instead of a file or folder of that name.+/// Windows opens the device instead of a file or folder of that name. `COM` and `LPT` take the+/// superscript digits ¹ ² ³ as well, as Windows does.pub(crate) fn is_windows_device_name(component: &str) -> bool {let stem = component.split('.').next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase();+ let port = |n: &str| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³");matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$")- || stem.strip_prefix("COM").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"))- || stem.strip_prefix("LPT").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"))+ || stem.strip_prefix("COM").is_some_and(port)+ || stem.strip_prefix("LPT").is_some_and(port)}pub(crate) fn import(name: &str, bytes: &[u8]) -> Result<Document> {diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rsindex eba3a771f..3f7633f1d 100644--- a/crates/engine/src/file_cmds/tests.rs+++ b/crates/engine/src/file_cmds/tests.rs@@ -1435,11 +1435,13 @@ fn sanitize_keeps_windows_device_names_off_the_disk() {assert_eq!(sanitize("CON"), "CON_");assert_eq!(sanitize("com1.v2"), "com1_.v2");assert_eq!(sanitize("lpt9 .final"), "lpt9 _.final");- for name in ["nul", "CON", "com1.v2", "lpt9 .final", "aux", "prn", "conin$", "clock$"] {+ assert_eq!(sanitize("COM¹"), "COM¹_");+ assert_eq!(sanitize("LPT².txt"), "LPT²_.txt");+ for name in ["nul", "CON", "com1.v2", "lpt9 .final", "aux", "prn", "conin$", "clock$", "COM¹", "lpt³.x"] {assert!(!is_windows_device_name(&sanitize(name)), "{name} -> {}", sanitize(name));}// Names that only start like a device are files like any other.- for name in ["console", "normal", "com10", "lpt0", "nullify", "con_"] {+ for name in ["console", "normal", "com10", "lpt0", "nullify", "con_", "com⁴", "com¹¹"] {assert_eq!(sanitize(name), name);}}diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rsindex 60db3a3b5..5f7433fb4 100644--- a/crates/engine/src/web_cmds.rs+++ b/crates/engine/src/web_cmds.rs@@ -19,7 +19,7 @@ use photocraft_geom::Rect;use serde_json::{Value, json};use crate::commands::CommandSpec;-use crate::file_cmds::{is_windows_device_name, join, native_doc, stem, write_file};+use crate::file_cmds::{is_windows_device_name, join, native_doc, portable_device_name, stem, write_file};use crate::{EngineError, Result, Session};fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {@@ -732,7 +732,7 @@ fn slice_file(name: &str) -> String {/// Generated filenames should remain usable on Windows even when a document or slice is named/// after a reserved device. Caller-supplied path components are still rejected below.fn portable_generated_stem(stem: String) -> String {- if is_windows_device_name(&stem) { format!("{stem}_") } else { stem }+ portable_device_name(stem)}fn html_escape(s: &str) -> String {diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rsindex 5791e5f27..abc849e1b 100644--- a/crates/engine/src/web_cmds/tests.rs+++ b/crates/engine/src/web_cmds/tests.rs@@ -167,6 +167,19 @@ fn reserved_document_and_slice_names_export_with_portable_names() {assert!(std::path::Path::new(result["html"].as_str().unwrap()).is_file());}+#[test]+fn a_dotted_device_file_name_exports_without_the_device_stem() {+ // `nul.v2_` would still be the NUL device on Windows; `nul_.v2` is a file.+ let dir = tmp("reserved-dotted-name");+ let mut s = session(8);+ s.execute("slice.new", json!({"rect": [0, 0, 16, 16], "name": "logo"})).unwrap();+ let path = format!("{dir}/nul.v2.png");+ let result = s.execute("file.export.saveForWebLegacy", json!({"path": path, "slices": "all", "format": "png24", "html": true})).unwrap();+ let html = result["html"].as_str().unwrap();+ assert!(html.ends_with("/nul_.v2.html"), "{html}");+ assert!(std::path::Path::new(html).is_file());+}+#[test]fn platform_writer_failure_stops_export_and_preserves_saved_settings() {let mut s = session(8);