Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#379: preset export and import go through the host, so the web build no longer traps

MarekSpodBiedry55e43e6147e2
diff --git a/crates/engine/src/presets.rs b/crates/engine/src/presets.rs
index 07de7812a..75f711447 100644
--- a/crates/engine/src/presets.rs
+++ b/crates/engine/src/presets.rs
@@ -550,13 +550,14 @@ fn export(s: &mut Session, p: &Value) -> Result<Value> {
if chosen.is_empty() {
return Err(bad("presets.export", "no presets to export"));
}
- filmcraft_format::atomic_write(Path::new(path), &file_bytes(&chosen)).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
+ // through the host: atomic on the desktop, a download on the web (no filesystem there)
+ s.services.write_file(path, &file_bytes(&chosen)).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
Ok(json!({"path": path, "count": chosen.len()}))
}
fn import(s: &mut Session, p: &Value) -> Result<Value> {
let path = str_p(p, "path").ok_or_else(|| bad("presets.import", "need `path`"))?;
- let bytes = std::fs::read(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
+ let bytes = s.services.read_file(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
let presets = parse_file(&bytes).map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
let names: Vec<String> = presets.iter().map(|x| x.name.clone()).collect();
for pr in presets {
diff --git a/crates/engine/src/presets_tests.rs b/crates/engine/src/presets_tests.rs
index 57c642e7d..db8065b37 100644
--- a/crates/engine/src/presets_tests.rs
+++ b/crates/engine/src/presets_tests.rs
@@ -1,7 +1,10 @@
+use std::collections::BTreeMap;
+use std::sync::{Arc, Mutex};
+
use serde_json::json;
-use crate::Session;
use crate::presets::{KeyframeMode, retime};
+use crate::{Services, Session};
use filmcraft_project::{ClipId, ParamValue, TrackItem};
use filmcraft_time::{TICKS_PER_SECOND, Tick};
@@ -203,3 +206,35 @@ fn mask_geometry_scales_to_the_target_frame_size() {
assert!((c.x - 480.0).abs() < 1e-6 && (c.y - 270.0).abs() < 1e-6, "{c:?}");
assert_eq!(m.feather.value, ParamValue::Float(80.0));
}
+
+/// A host without a filesystem (the web): files exist only through `Services`.
+#[derive(Default)]
+struct MemFs(Mutex<BTreeMap<String, Vec<u8>>>);
+
+impl Services for MemFs {
+ fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
+ self.0.lock().unwrap().get(path).cloned().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, path.to_string()))
+ }
+ fn write_file(&self, path: &str, data: &[u8]) -> std::io::Result<()> {
+ self.0.lock().unwrap().insert(path.into(), data.to_vec());
+ Ok(())
+ }
+}
+
+/// #379: export and import go through the host's services, not `std::fs` (which traps on the web).
+#[test]
+fn export_and_import_go_through_host_services() {
+ let fs = Arc::new(MemFs::default());
+ let mut s = Session::new(fs.clone());
+ let path = "/exports/owned-preset.prfpset";
+ let r = s.execute("presets.export", json!({"path": path, "names": ["Fade In"]})).unwrap();
+ assert_eq!(r["count"], 1);
+ let bytes = fs.read_file(path).unwrap();
+ assert!(String::from_utf8_lossy(&bytes).contains("filmcraft.effect-presets"));
+ // a second session on the same host imports it from there
+ let mut s2 = Session::new(fs);
+ let r = s2.execute("presets.import", json!({"path": path})).unwrap();
+ assert_eq!(r["imported"], json!(["Fade In"]));
+ assert_eq!(s2.presets.user.len(), 1);
+ assert!(s2.execute("presets.import", json!({"path": "/exports/missing.prfpset"})).is_err());
+}