fix: sanitize untitled Image Processor export names

This commit is contained in:
Tsieg committed 2026-11-10 21:48:31 +07:00
1 parent e0cb757a64
commit e191f5e41e
4 files changed
+44 -2

No files matched your search

+1
View File
@@ -332,3 +332,4 @@ sync_github_name = false
[people.se343r]
display_name = "Tsieg"
sync_github_name = true
+4 -2
View File
@@ -1,5 +1,5 @@
use super::open::import_at_raw_ppi;
use super::paths::{extension, file_name, join, list_files, list_images, read_file, stem, write_file};
use super::paths::{extension, file_name, join, list_files, list_images, read_file, sanitize, stem, write_file};
use super::processing::parse_steps;
use super::save::{SaveOpts, encode, save_doc, save_doc_as};
use super::{Arc, Document, EngineError, Result, Session, Value, batch_naming, json, str_param};
@@ -263,6 +263,7 @@ type PrepareInput<'a> = &'a dyn Fn(&mut Session, &str) -> Result<()>;
/// Process the current in-memory images in scratch sessions: unsaved edits are included and
/// resizing never touches the open documents' pixels, selection, history or save state.
/// Untitled names use the shared filename sanitizer before becoming output stems.
pub(super) fn process_document_copies(
s: &Session,
documents: &[&crate::DocState],
@@ -277,7 +278,8 @@ pub(super) fn process_document_copies(
for state in documents {
// Export resolves stored links against this source, including non-active documents.
let _document = crate::doc_path::scope(state.path.as_deref().map(std::path::Path::new));
let path = state.path.clone().unwrap_or_else(|| format!("{}.psd", state.doc.name));
// An untitled document's name is a label, not a path: keep it as one portable component.
let path = state.path.clone().unwrap_or_else(|| format!("{}.psd", sanitize(&state.doc.name)));
let ext = if format == "same" { same_format(&path) } else { format.trim_start_matches('.').to_ascii_lowercase() };
let out = join(output, &format!("{}.{ext}", stem(&path)));
// Copies must not overwrite any open source file, including aliases of its path.
@@ -242,3 +242,39 @@ fn image_processor_open_embeds_relative_links_from_each_documents_own_folder() {
assert!(Arc::ptr_eq(&state.doc, original));
}
}
/// Untitled names are labels rather than paths: preserve the full name through the shared
/// sanitizer, keep the original document untouched and use PSD for the `same` format.
#[test]
fn image_processor_untitled_names_use_the_shared_filename_sanitizer() {
for source in ["current", "open"] {
for (format, extension) in [("png", "png"), ("same", "psd")] {
for (name, safe_name) in [
("foo/bar", "foo_bar"),
(r"foo\bar", "foo_bar"),
("../outside", "_outside"),
("../../outside", "_.._outside"),
("..", "layer"),
("CON", "CON_"),
("nul.v2", "nul_.v2"),
("trailing. ", "trailing"),
("chapter.v2", "chapter.v2"),
] {
let dir = tempfile::tempdir().unwrap();
let output = dir.path().join("processed");
let mut s = Session::new();
add(&mut s, name, 8, "#ff0000").unwrap();
let original = s.active().unwrap().doc.clone();
let result = s.execute("file.scripts.imageProcessor", json!({"source": source, "output": output, "format": format})).unwrap();
assert!(result["errors"].as_array().unwrap().is_empty(), "{source}, {format}, {name:?}: {result}");
assert_eq!(result["files"], json!([output.join(format!("{safe_name}.{extension}"))]), "{source}, {format}, {name:?}");
assert_eq!(exported(&result, 0).unwrap().size, original.size);
let state = s.active().unwrap();
assert!(Arc::ptr_eq(&state.doc, &original));
assert_eq!(state.doc.name, name);
assert!(state.path.is_none());
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1, "only the output folder is created");
}
}
}
}
+3
View File
@@ -67,6 +67,9 @@ copies; the open documents, their history and their source files remain intact.
separate output folder. Duplicate output names appear in the per-file errors instead of
replacing an earlier result.
Untitled document names follow the portable filename rules used for layer exports: for
example, `foo/bar` becomes `foo_bar`. The document's displayed name stays unchanged.
Agents can use the same engine command:
```json