mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Scripts: placing or relinking an image reads its file once
PlacedItem.file and PlacedItem.relink() ran file.place.info on the file
to check that it held an image, then file.place or links.relink read and
decoded it again. The image check now runs inside file.place and
links.relink while a script places or relinks an image: a file in no
image format is refused before the document changes, with "The file is
not an image", and a damaged image keeps its own error. The double read
came in with fed96731e2 (#9077).
A test counts the engine's file reads and fails with 2 reads on main.
This commit is contained in:
1 parent
f207334070
commit
08a29802b8
9 files changed
+107
-11
No files matched your search
@@ -299,8 +299,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 {
|
||||
@@ -318,13 +318,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 ----
|
||||
@@ -394,7 +402,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 =
|
||||
@@ -411,7 +419,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)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user