Merge pull request #10636 from dotCooCoo/fix/script-image-read-once

Scripts: placing or relinking an image reads its file once
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-03 07:32:03 -08:00
commit 9a21dae940
9 files changed
+107 -11

No files matched your search

+18 -10
View File
@@ -302,8 +302,8 @@ fn may_read(h: &Host, path: &str) -> bool {
/// The real path of the `File` `d` gives as an image to place or relink: one the script may read
/// ([`super::super::files::Disk::open_target`], as `app.open` reads its file), a regular file of
/// at most [`MAX_PLACED_BYTES`] that holds an image. Its size is read before any of it is, and no
/// error names the path.
/// at most [`MAX_PLACED_BYTES`]. Its size is read before any of it is, and no error names the path.
/// What the file holds is checked by the command that places or relinks it, which reads it once.
fn image_path(h: &Host, d: &Data, what: &str) -> Result<String, HostError> {
let path = file_ref_path(d).ok_or_else(|| HostError::Type(format!("{what} must be a File")))?;
if path.len() > super::super::files::MAX_PATH_CHARS {
@@ -321,13 +321,21 @@ fn image_path(h: &Host, d: &Data, what: &str) -> Result<String, HostError> {
if meta.len() > MAX_PLACED_BYTES {
return Err(HostError::Range(format!("{what} is larger than {} MiB", MAX_PLACED_BYTES >> 20)));
}
let path = crate::cmd::findfiles::plain(real).to_string_lossy().into_owned();
// What the file holds, read the way `file.place` reads it, before the document changes.
let info = command(h, "file.place.info", &json!({"path": path, "link": true}))?;
if field(&info, "pixelWidth").is_none() {
return Err(HostError::Error(NOT_AN_IMAGE.into()));
Ok(crate::cmd::findfiles::plain(real).to_string_lossy().into_owned())
}
/// Runs `f`, which places or relinks a file `image_path` gave, with `file.place` and `links.relink`
/// refusing a file that holds no image before the document changes: that refusal is
/// [`NOT_AN_IMAGE`], which does not name the path. The command reads the file once, for its check
/// and for the image it places.
fn image_only<T>(h: &Host, f: impl FnOnce(&Host) -> Result<T, HostError>) -> Result<T, HostError> {
let kept = h.session.borrow().image_only.replace(Some(true));
let r = f(h);
let refused = h.session.borrow().image_only.replace(kept) == Some(false);
match r {
Err(HostError::Error(_)) if refused => Err(HostError::Error(NOT_AN_IMAGE.into())),
r => r,
}
Ok(path)
}
// ---- PlacedItem ----
@@ -397,7 +405,7 @@ fn placed_set_file(h: &Host, a: &[Data]) -> Answer {
// The placement and its check run under one savepoint, so an error after the place leaves
// no linked image behind.
let id = super::atomically(h, |h| {
let (_, placed) = on_doc(h, doc.uid, true, |h| command_value(h, "file.place", &json!({"path": path, "link": true})))?;
let (_, placed) = image_only(h, |h| on_doc(h, doc.uid, true, |h| command_value(h, "file.place", &json!({"path": path, "link": true}))))?;
let first = |v: &serde_json::Value| v.as_u64().or_else(|| v.as_array().and_then(|a| a.first()).and_then(serde_json::Value::as_u64));
let id = first(&placed["image"]).or_else(|| first(&placed["ids"])).map(NodeId);
let linked =
@@ -414,7 +422,7 @@ fn placed_set_file(h: &Host, a: &[Data]) -> Answer {
fn placed_relink(h: &Host, a: &[Data]) -> Answer {
let item = this_of(h, a, "PlacedItem")?;
let path = image_path(h, arg(a, 1), "relink's file")?;
edit_item(h, item, "links.relink", &json!({"ids": [item.id.0], "path": path}))?;
image_only(h, |h| edit_item(h, item, "links.relink", &json!({"ids": [item.id.0], "path": path})))?;
Ok(Data::Undefined)
}