mirror of
https://github.com/storytold/cadcraft.git
synced 2026-10-25 03:16:50 -06:00
The web app ran CadApp with Services::default(), so OPEN found no file
picker and returned without doing anything: no picker, no message.
The web app now installs an open picker (apps/cadcraft-web/src/upload.rs,
web-sys only): a hidden <input type="file" accept=".dxf,.dwg"> is
clicked, the chosen file is read in the background (Blob.arrayBuffer)
and handed to the app on its next frame, which runs OPEN with
{data, name} through the new CadApp::open_bytes. Cancelling the picker
does nothing. Files dropped on the page, which eframe hands over unread
on the web and the UI ignored there, take the same route.
Files over 256 MB (CadApp::MAX_OPEN_BYTES; the browser holds the drawing
several times while decoding it) are refused before they are read.
Unreadable or rejected files report on the command line and in the
status bar; nothing panics.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
39 lines
2.0 KiB
Rust
39 lines
2.0 KiB
Rust
//! Opening a drawing from its contents, where the file has no path (the web's File > Open… picker
|
|
//! and files dropped on the page, #55).
|
|
|
|
use cadcraft_engine::Session;
|
|
use cadcraft_engine::cmd::file::{IoHooks, set_io};
|
|
use cadcraft_engine::doc::Drawing;
|
|
use cadcraft_ui_egui::{CadApp, Services};
|
|
|
|
#[test]
|
|
fn open_bytes_opens_the_named_drawing_and_reports_failures() {
|
|
// A reader that only accepts what a DXF starts with: proves the bytes arrive unchanged.
|
|
set_io(IoHooks {
|
|
read: |b, _| if b.starts_with(b"0\nSECTION\n") { Ok(Drawing::new_imperial()) } else { Err("not a drawing".into()) },
|
|
write: |_, _| Ok(Vec::new()),
|
|
plot: None,
|
|
});
|
|
let mut app = CadApp::new(Session::empty(), Services::default());
|
|
app.ui.start_tab = true;
|
|
|
|
app.open_bytes("Bracket.dwg", b"0\nSECTION\n2\nHEADER\n");
|
|
assert_eq!(app.session.docs.len(), 1);
|
|
assert_eq!(app.session.state().map(|s| s.title.clone()).ok().as_deref(), Some("Bracket.dwg"));
|
|
assert!(!app.ui.start_tab && app.canvas.zoom_pending, "shown and zoomed like a drawing opened from a path");
|
|
|
|
// A file the reader rejects: nothing opens; the error reaches the command line and status bar.
|
|
app.open_bytes("junk.dxf", b"junk");
|
|
assert_eq!(app.session.docs.len(), 1);
|
|
assert!(app.session.log.last().is_some_and(|l| l.contains("not a drawing")), "{:?}", app.session.log.last());
|
|
assert!(app.status.as_ref().is_some_and(|(s, _)| s.contains("not a drawing")));
|
|
|
|
// Too large to hold in memory: refused before anything is read, with the limit named.
|
|
assert!(CadApp::open_size_error("big.dwg", CadApp::MAX_OPEN_BYTES).is_none());
|
|
let e = CadApp::open_size_error("huge.dwg", CadApp::MAX_OPEN_BYTES * 2);
|
|
assert_eq!(e.as_deref(), Some("Open: huge.dwg is 512.0 MB, larger than the 256 MB CADCraft opens in the browser"));
|
|
app.open_failed(e.unwrap_or_default());
|
|
assert_eq!(app.session.log.last().map(String::as_str), Some("Open: huge.dwg is 512.0 MB, larger than the 256 MB CADCraft opens in the browser"));
|
|
assert_eq!(app.session.docs.len(), 1);
|
|
}
|