FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
fix(import): preserve occupied AAF/OMF embedded-audio paths
nuclearspikeb23675bd179b
diff --git a/crates/engine/src/aaf_omf_tests.rs b/crates/engine/src/aaf_omf_tests.rsindex 7d46fb5c2..b80245507 100644--- a/crates/engine/src/aaf_omf_tests.rs+++ b/crates/engine/src/aaf_omf_tests.rs@@ -131,83 +131,6 @@ fn embedded_aaf_and_omf_import_leave_existing_media_files_intact() {}}-#[test]-fn embedded_aaf_and_omf_replace_failures_preserve_open_project() {- for (extension, command) in [("aaf", "file.exportAaf"), ("omf", "file.exportOmf")] {- let (mut s, _items, seq, dir) = setup(&format!("embedded-blocked-directory-{extension}"));- let path = dir.join(format!("cut.{extension}"));- s.execute(command, json!({"path": path.to_string_lossy(), "audio": "embedded", "sequence": seq.0})).unwrap();- let blocker = dir.join("cut Media");- std::fs::write(&blocker, b"ORIGINAL USER DATA").unwrap();- s.path = Some(dir.join("open-work.fcproj").to_string_lossy().into_owned());- let original_project = s.project.clone();- let original_path = s.path.clone();- let before = s.project.items.len();-- let ordinary_error = s.execute("file.import", json!({"paths": [path.to_string_lossy()]})).unwrap_err();- assert!(ordinary_error.to_string().contains("not a directory"), "{extension}: {ordinary_error}");- assert!(Arc::ptr_eq(&s.project, &original_project), "{extension}: failed ordinary import changed the project");-- let error = s.execute("file.import", json!({"paths": [path.to_string_lossy()], "replace": true})).unwrap_err();- assert!(error.to_string().contains("not a directory"), "{extension}: {error}");- assert_eq!(std::fs::read(&blocker).unwrap(), b"ORIGINAL USER DATA");- assert_eq!(s.project.items.len(), before, "{extension}: open project items changed");- assert!(Arc::ptr_eq(&s.project, &original_project), "{extension}: failed replace import discarded the open project");- assert_eq!(s.path, original_path, "{extension}: failed replace import discarded the project path");-- std::fs::remove_file(&blocker).unwrap();- let retry = s.execute("file.import", json!({"paths": [path.to_string_lossy()], "replace": true})).unwrap();- assert!(retry["errors"].as_array().is_some_and(Vec::is_empty), "{extension}: {retry}");- let imported = s.project.sequence(imported_seq(&retry)).unwrap();- let audio_path = path_of(&s, imported.audio_tracks[0].items[0].item);- assert!(std::path::Path::new(&audio_path).exists(), "{extension}: successful retry did not link extracted audio");- assert!(!Arc::ptr_eq(&s.project, &original_project), "{extension}: successful replace kept the old project");- assert!(s.path.is_none(), "{extension}: successful replace kept the old project path");- let _ = std::fs::remove_dir_all(dir);- }-}--#[test]-fn malformed_aaf_and_omf_replace_preserves_open_project() {- for (extension, bytes, variant) in [- ("aaf", b"not an interchange document" as &[u8], "unknown"),- ("omf", b"not an interchange document" as &[u8], "unknown"),- ("aaf", b"TITLE: suspicious\n" as &[u8], "edl-marker"),- ("omf", b"TITLE: suspicious\n" as &[u8], "edl-marker"),- ("omfi", b"TITLE: suspicious\n" as &[u8], "edl-marker"),- ] {- let (mut s, _items, _seq, dir) = setup(&format!("malformed-replace-{extension}-{variant}"));- let path = dir.join(format!("bad.{extension}"));- std::fs::write(&path, bytes).unwrap();- s.path = Some(dir.join("open-work.fcproj").to_string_lossy().into_owned());- let original_project = s.project.clone();- let original_path = s.path.clone();-- let override_error = s.execute("file.import", json!({"paths": [path.to_string_lossy()], "replace": true, "imageSequence": true})).unwrap_err();- assert!(override_error.to_string().contains("image sequences"), "{extension} {variant}: {override_error}");- assert!(Arc::ptr_eq(&s.project, &original_project), "{extension} {variant}: image-sequence override discarded the project");- assert_eq!(s.path, original_path, "{extension} {variant}: image-sequence override discarded the project path");-- let parser_error = if extension == "aaf" {- filmcraft_interchange::aaf::import(bytes, &Default::default()).err()- } else {- filmcraft_interchange::omf::import(bytes, &Default::default()).err()- }- .expect("malformed interchange document");- let ordinary_error = s.execute("file.import", json!({"paths": [path.to_string_lossy()]})).unwrap_err();- assert!(- ordinary_error.to_string().ends_with(&parser_error.to_string()),- "{extension} {variant}: wrong parser handled ordinary import: {ordinary_error}"- );- assert!(Arc::ptr_eq(&s.project, &original_project), "{extension} {variant}: malformed ordinary import changed the project");- let error = s.execute("file.import", json!({"paths": [path.to_string_lossy()], "replace": true})).unwrap_err();- assert!(error.to_string().ends_with(&parser_error.to_string()), "{extension} {variant}: wrong parser handled malformed document: {error}");- assert!(Arc::ptr_eq(&s.project, &original_project), "{extension} {variant}: malformed replace discarded the open project");- assert_eq!(s.path, original_path, "{extension} {variant}: malformed replace discarded the project path");- let _ = std::fs::remove_dir_all(dir);- }-}-#[test]fn aaf_separate_aiff_breakout_and_linked() {let (mut s, items, seq, dir) = setup("aaf-separate");diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 168a2b349..f7737f571 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -1134,27 +1134,6 @@ fn marker_list_mut(s: &mut Session) -> Result<(ItemId, Tick)> {Ok((seq, s.playhead()))}-fn aaf_or_omf_extension(path: &str) -> Option<filmcraft_interchange::Format> {- std::path::Path::new(path).extension().and_then(|extension| extension.to_str()).and_then(|extension| {- if extension.eq_ignore_ascii_case("aaf") {- Some(filmcraft_interchange::Format::Aaf)- } else if extension.eq_ignore_ascii_case("omf") || extension.eq_ignore_ascii_case("omfi") {- Some(filmcraft_interchange::Format::Omf)- } else {- None- }- })-}--fn replace_project_for_import(s: &mut Session, pending: &mut bool) -> Result<()> {- if *pending {- let name = s.project.name.clone();- s.execute("file.newProject", json!({"name": name}))?;- *pending = false;- }- Ok(())-}-fn build() -> Vec<CommandSpec> {let mut v = vec![// ================= File =================@@ -1359,7 +1338,10 @@ fn build() -> Vec<CommandSpec> {if paths.is_empty() {return Err(bad("file.import", "need `paths`"));}- let mut replace_pending = bool_p(p, "replace").unwrap_or(false);+ if bool_p(p, "replace").unwrap_or(false) {+ let name = s.project.name.clone();+ s.execute("file.newProject", json!({ "name": name }))?;+ }let mut ids = Vec::new();let mut errors = Vec::new();let mut sequences = Vec::new();@@ -1370,13 +1352,7 @@ fn build() -> Vec<CommandSpec> {let as_sequence = p.get("imageSequence").and_then(Value::as_bool).unwrap_or(false);let detect = !as_sequence && s.prefs.media.import_image_sequences && paths.len() == 1;for path in paths {- let guarded_format = aaf_or_omf_extension(&path);- if guarded_format.is_some() && as_sequence {- errors.push(format!("{path}: AAF/OMF documents cannot be imported as image sequences"));- continue;- }- if guarded_format.is_none() && (as_sequence || (detect && detect_image_sequence(s, &path))) {- replace_project_for_import(s, &mut replace_pending)?;+ if as_sequence || (detect && detect_image_sequence(s, &path)) {match import_image_sequence(s, &path, bin) {Ok((id, frames, missing)) => {ids.push(id.0);@@ -1390,32 +1366,19 @@ fn build() -> Vec<CommandSpec> {let streamed = filmcraft_media::is_importable(std::path::Path::new(&path)) && s.services.reader(&path).is_some();let read = if streamed { Ok(Vec::new()) } else { s.services.read_file(&path) };match read {- Ok(_) if streamed => {- replace_project_for_import(s, &mut replace_pending)?;- match import_streamed(s, &path, bin) {- Ok(id) => ids.push(id.0),- Err(e) => errors.push(format!("{path}: {e}")),- }- }+ Ok(_) if streamed => match import_streamed(s, &path, bin) {+ Ok(id) => ids.push(id.0),+ Err(e) => errors.push(format!("{path}: {e}")),+ },Ok(b) => {- // Route AAF/OMF extensions to their parser even if damaged bytes resemble- // another format. In replace mode, parsing must fail before project reset.- let caption_format = if guarded_format.is_some() { None } else { crate::captions::detect(&path, &b) };- let interchange_format = guarded_format.or_else(|| if caption_format.is_none() { crate::interchange::detect(&path, &b) } else { None });- let replace_interchange =- replace_pending && matches!(interchange_format, Some(filmcraft_interchange::Format::Aaf | filmcraft_interchange::Format::Omf));- if !replace_interchange {- replace_project_for_import(s, &mut replace_pending)?;- }- if let Some(fmt) = caption_format {+ if let Some(fmt) = crate::captions::detect(&path, &b) {match crate::captions::import(s, &path, &b, fmt, None) {Ok(r) => reports.push(r),Err(e) => errors.push(format!("{path}: {e}")),}- } else if let Some(fmt) = interchange_format {- match crate::interchange::import_with_replace(s, &path, &b, fmt, replace_interchange) {+ } else if let Some(fmt) = crate::interchange::detect(&path, &b) {+ match crate::interchange::import(s, &path, &b, fmt) {Ok(r) => {- replace_pending = false;sequences.extend(r["sequences"].as_array().cloned().unwrap_or_default());reports.push(r);}diff --git a/crates/engine/src/interchange.rs b/crates/engine/src/interchange.rsindex 6a1b5f60c..d32713fb6 100644--- a/crates/engine/src/interchange.rs+++ b/crates/engine/src/interchange.rs@@ -55,11 +55,6 @@ fn write_embedded_audio(s: &Session, path: &str, wav: &[u8]) -> Result<String> {/// Import a document: merge its bins, media and sequences into the project (one undo step), then/// link each media file that exists on disk. Returns the new sequences.pub fn import(s: &mut Session, path: &str, bytes: &[u8], format: Format) -> Result<Value> {- import_with_replace(s, path, bytes, format, false)-}--/// With replacement, keep the open project until the interchange data and embedded audio are ready.-pub fn import_with_replace(s: &mut Session, path: &str, bytes: &[u8], format: Format, replace: bool) -> Result<Value> {let p = std::path::Path::new(path);let opts = ImportOptions {base_dir: p.parent().map(|d| d.to_string_lossy().to_string()),@@ -86,10 +81,6 @@ pub fn import_with_replace(s: &mut Session, path: &str, bytes: &[u8], format: Fo}_ => filmcraft_interchange::import_with(bytes, format, &opts).map_err(|e| EngineError::Other(format!("{path}: {e}")))?,};- if replace {- let name = s.project.name.clone();- s.execute("file.newProject", json!({"name": name}))?;- }let before: std::collections::HashSet<ItemId> = s.project.items.keys().copied().collect();let name = opts.name.clone().unwrap_or_else(|| format.name().to_string());let seqs = s.edit(&format!("Import {name}"), |proj, _| {