* Engine: open a design a feature at a time (#10198) OpenSteps splits doc.open of a design into steps for a caller that must keep working meanwhile (the web app, which has one thread): start reads the file, each step evaluates the next feature by moving a copy's history marker one feature on (so every result before it is reused), and finish puts the design in the session's place with doc.open's reply. The next feature's imported STEP text is offered first, so a caller can read it in parts. Session::opened and opened_at are split into small helpers so both paths build the session the same way; doc.open is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Web app: open a design a few features a frame, with progress and Cancel (#10198) On the web, a design to open is now a staged file load like a STEP file (#1376): its features are evaluated for about 30 ms a frame through OpenSteps, a feature's imported STEP file is read a solid per frame first and held so the feature finds it read, the progress card counts the design's features, Cancel works between steps, and the meshes are made a few per frame afterwards. The desktop app keeps its worker thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Web app: design links and the files window open designs with progress and Cancel (#10198) Design links (?open=URL, as embedded viewers use), the files window and designs opened from the computer went through the one-shot open_path, which froze the page on a large design. They now use the staged open; a design link becomes an unsaved copy under the link's name once the open's reply arrives (SolveApp::open_path_answered). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
7.5 KiB
Rust
162 lines
7.5 KiB
Rust
//! Bounded HTTPS or same-origin downloads, handed to the normal design-open command as unsaved copies.
|
|
use std::{cell::RefCell, rc::Rc};
|
|
|
|
use js_sys::{Reflect, Uint8Array};
|
|
use solidcraft_engine::io::vfs;
|
|
use solidcraft_ui_egui::{SolveApp, ui_kit};
|
|
use wasm_bindgen::{JsCast as _, JsValue};
|
|
use wasm_bindgen_futures::{JsFuture, spawn_local};
|
|
|
|
use crate::{check_download_size, design_url};
|
|
|
|
type Reply = Result<(String, Vec<u8>), String>;
|
|
|
|
#[derive(Default)]
|
|
pub struct Download {
|
|
reply: Rc<RefCell<Option<Reply>>>,
|
|
pending: Option<(String, Vec<u8>)>,
|
|
/// The downloaded design being opened (a few features a frame, #10198): the answer to come,
|
|
/// the name it takes and the file it was read from.
|
|
opening: Option<(std::sync::mpsc::Receiver<serde_json::Value>, String, String)>,
|
|
}
|
|
|
|
impl Download {
|
|
pub fn start(url: Option<&str>, ctx: &egui::Context) -> Self {
|
|
let download = Self::default();
|
|
if let Some(url) = url {
|
|
let url = url.to_owned();
|
|
let slot = download.reply.clone();
|
|
let ctx = ctx.clone();
|
|
spawn_local(async move {
|
|
*slot.borrow_mut() = Some(fetch(&url).await);
|
|
ctx.request_repaint();
|
|
});
|
|
}
|
|
download
|
|
}
|
|
|
|
pub fn tick(&mut self, app: &mut SolveApp, ctx: &egui::Context) {
|
|
self.opened(app);
|
|
if let Some(reply) = self.reply.borrow_mut().take() {
|
|
match reply {
|
|
Ok(file) => self.pending = Some(file),
|
|
Err(error) => app.set_status(format!("Could not open design link: {error}"), true),
|
|
}
|
|
}
|
|
if self.pending.is_none() || self.opening.is_some() || app.file_picker_busy() {
|
|
return;
|
|
}
|
|
if solidcraft_ui_egui::documents::any_dirty(app) {
|
|
let mut answer = None;
|
|
let closed = ui_kit::Window::new(egui::Id::new("open_link"), "status_warning", solidcraft_ui_egui::i18n::tr("web.open_link"))
|
|
.required()
|
|
.show_body(ctx, |ui| {
|
|
ui.label(solidcraft_ui_egui::i18n::tr("web.open_link_confirm"));
|
|
ui.horizontal(|ui| {
|
|
if ui_kit::button(ui, solidcraft_ui_egui::i18n::tr("ui.open"), ui_kit::Button::Normal).clicked() {
|
|
answer = Some(true);
|
|
}
|
|
if ui_kit::button(ui, solidcraft_ui_egui::i18n::tr("ui.cancel"), ui_kit::Button::Normal).clicked() {
|
|
answer = Some(false);
|
|
}
|
|
});
|
|
});
|
|
match answer {
|
|
Some(true) => {}
|
|
Some(false) => {
|
|
self.pending = None;
|
|
return;
|
|
}
|
|
None if closed => {
|
|
self.pending = None;
|
|
return;
|
|
}
|
|
None => return,
|
|
}
|
|
}
|
|
let Some((name, bytes)) = self.pending.take() else { return };
|
|
// The extension selects the design loader even when the URL has no extension. Named
|
|
// after the link, as the progress card shows the file's name.
|
|
let file: String = name.chars().map(|c| if matches!(c, '/' | '\\') { '_' } else { c }).collect();
|
|
let path = format!("/linked/{}.{}", if file.is_empty() { "design" } else { &file }, solidcraft_engine::io::DESIGN_EXT);
|
|
vfs::insert(&path, bytes);
|
|
// Opened as any design is, with its progress and Cancel; finished in `opened`.
|
|
self.opening = Some((app.open_path_answered(&path), name, path));
|
|
self.opened(app);
|
|
}
|
|
|
|
/// The downloaded design is in: it is an unsaved copy under the link's name.
|
|
fn opened(&mut self, app: &mut SolveApp) {
|
|
let Some((rx, _, _)) = &self.opening else { return };
|
|
let (ok, cancelled) = match rx.try_recv() {
|
|
Ok(reply) => (reply["ok"].as_bool() == Some(true), reply["error"].as_str().is_some_and(|e| e.contains("cancelled"))),
|
|
Err(std::sync::mpsc::TryRecvError::Empty) => return,
|
|
Err(std::sync::mpsc::TryRecvError::Disconnected) => (false, false),
|
|
};
|
|
let Some((_, name, path)) = self.opening.take() else { return };
|
|
if ok && app.session.path.as_deref() == Some(path.as_str()) {
|
|
app.session.path = None;
|
|
app.session.doc_mut().name = name;
|
|
app.session.mark_unsaved();
|
|
} else if !ok && !cancelled {
|
|
app.set_status("Could not open design link: the downloaded file is not a valid SolidCraft design", true);
|
|
}
|
|
let _ = vfs::remove(&path);
|
|
}
|
|
}
|
|
|
|
async fn fetch(value: &str) -> Reply {
|
|
let window = web_sys::window().ok_or("No browser window")?;
|
|
let page_origin = window.location().origin().map_err(|_| "Cannot read page origin")?;
|
|
let url = design_url(value, &page_origin)?;
|
|
let name = url.path_segments().and_then(|mut segments| segments.next_back()).filter(|name| !name.is_empty()).unwrap_or("Design");
|
|
let decoded = js_sys::decode_uri_component(name).ok().and_then(|name| name.as_string()).unwrap_or_else(|| name.to_owned());
|
|
// `arm.sc` (or a legacy `arm.solidcraft`) is named `arm`.
|
|
let stem = match decoded.rsplit_once('.') {
|
|
Some((stem, ext)) if !stem.is_empty() && solidcraft_engine::io::is_design_ext(ext) => stem,
|
|
_ => decoded.as_str(),
|
|
};
|
|
let name: String = stem.chars().take(255).collect();
|
|
let response = JsFuture::from(window.fetch_with_str(url.as_str())).await.map_err(|_| "Network request failed or the host refused CORS access")?;
|
|
let response: web_sys::Response = response.dyn_into().map_err(|_| "Invalid download response")?;
|
|
design_url(&response.url(), &page_origin).map_err(|_| "Design redirected to a URL that does not use HTTPS or the page's own origin")?;
|
|
if !response.ok() {
|
|
return Err(format!("HTTP {} {}", response.status(), response.status_text()));
|
|
}
|
|
let length = response
|
|
.headers()
|
|
.get("Content-Length")
|
|
.map_err(|_| "Cannot read download size")?
|
|
.map(|value| value.parse::<u64>().map_err(|_| "Invalid Content-Length"))
|
|
.transpose()?;
|
|
if let Some(length) = length {
|
|
check_download_size(0, length)?;
|
|
}
|
|
let stream = response.body().ok_or("Download has no body")?;
|
|
let reader: web_sys::ReadableStreamDefaultReader = stream.get_reader().dyn_into().map_err(|_| "Cannot read download stream")?;
|
|
let result = read(&reader).await;
|
|
if result.is_err() {
|
|
let _ = JsFuture::from(reader.cancel()).await;
|
|
}
|
|
// Fetch rejects interrupted HTTP bodies and decodes Content-Encoding. Content-Length
|
|
// can describe compressed bytes, so the streamed limit also checks the decoded size.
|
|
let bytes = result?;
|
|
Ok((name, bytes))
|
|
}
|
|
|
|
async fn read(reader: &web_sys::ReadableStreamDefaultReader) -> Result<Vec<u8>, String> {
|
|
let mut bytes = Vec::new();
|
|
// Bound even a stream which keeps returning empty chunks.
|
|
for _ in 0..1_000_000 {
|
|
let chunk = JsFuture::from(reader.read()).await.map_err(|_| "Download interrupted")?;
|
|
if Reflect::get(&chunk, &JsValue::from_str("done")).map_err(|_| "Invalid download stream")?.as_bool() == Some(true) {
|
|
return Ok(bytes);
|
|
}
|
|
let value = Reflect::get(&chunk, &JsValue::from_str("value")).map_err(|_| "Invalid download chunk")?;
|
|
let chunk: Uint8Array = value.dyn_into().map_err(|_| "Invalid download bytes")?;
|
|
check_download_size(bytes.len(), u64::from(chunk.length()))?;
|
|
bytes.extend_from_slice(&chunk.to_vec());
|
|
}
|
|
Err("Download returned too many chunks".into())
|
|
}
|