Skip to content

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

Import: numbered embedded-audio names keep the path's separators on Windows (#2988)

.CooCoo1f6567021d78
diff --git a/crates/engine/src/interchange.rs b/crates/engine/src/interchange.rs
index e0f3e82e9..a0ce36778 100644
--- a/crates/engine/src/interchange.rs
+++ b/crates/engine/src/interchange.rs
@@ -1,7 +1,5 @@
//! Interchange documents (CMX 3600 EDL, FCP7 XML, FCPXML, OTIO, AAF, OMF) ↔ the session's project.
-use std::path::Path;
-
use serde_json::{Value, json};
use filmcraft_interchange::{ExportOptions, Format, ImportOptions};
@@ -26,27 +24,23 @@ fn is_file_media(item: &filmcraft_project::ProjectItem) -> bool {
matches!(&item.kind, ItemKind::Media(m) if matches!(m.media, MediaRef::File { .. }))
}
-/// Write embedded audio without replacing an occupied path; return the path used.
+/// Write embedded audio without replacing an occupied path; return the path used. A numbered
+/// name changes only the file name: the folder keeps its separators as written (`/` or `\`).
fn write_embedded_audio(s: &Session, path: &str, wav: &[u8]) -> Result<String> {
- let original = Path::new(path);
- let stem = original.file_stem().ok_or_else(|| EngineError::Other(format!("{path}: missing audio file name")))?;
+ let (folder, name) = path.split_at(path.rfind(['/', '\\']).map_or(0, |i| i + 1));
+ if matches!(name, "" | "." | "..") {
+ return Err(EngineError::Other(format!("{path}: missing audio file name")));
+ }
+ let (stem, extension) = match name.rfind('.') {
+ Some(i) if i > 0 => name.split_at(i),
+ _ => (name, ""),
+ };
for number in 1..=1_000 {
- let candidate = if number == 1 {
- original.to_path_buf()
- } else {
- let mut name = stem.to_os_string();
- name.push(format!(" ({number})"));
- if let Some(extension) = original.extension() {
- name.push(".");
- name.push(extension);
- }
- original.with_file_name(name)
- };
- let candidate_text = candidate.to_string_lossy();
- match s.services.write_file_new(&candidate_text, wav) {
- Ok(()) => return Ok(candidate_text.into_owned()),
+ let candidate = if number == 1 { path.to_owned() } else { format!("{folder}{stem} ({number}){extension}") };
+ match s.services.write_file_new(&candidate, wav) {
+ Ok(()) => return Ok(candidate),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
- Err(e) => return Err(EngineError::Other(format!("{candidate_text}: {e}"))),
+ Err(e) => return Err(EngineError::Other(format!("{candidate}: {e}"))),
}
}
Err(EngineError::Other(format!("{path}: no unused embedded-audio filename after 1000 attempts")))
@@ -237,4 +231,16 @@ mod embedded_audio_tests {
assert_eq!(stored["/files/cut Audio (2).wav"], b"second");
assert_eq!(stored["/files/cut Audio (3).wav"], b"new audio");
}
+
+ #[test]
+ fn numbered_names_keep_the_path_separators() {
+ for path in ["C:/files/cut Audio.wav", "C:\\files\\cut Audio.wav"] {
+ let files = Arc::new(VirtualFiles::default());
+ files.0.lock().unwrap().insert(path.into(), b"first".to_vec());
+ let session = Session::new(files.clone());
+ let numbered = write_embedded_audio(&session, path, b"new audio").unwrap();
+ assert_eq!(numbered, path.replace("cut Audio.wav", "cut Audio (2).wav"));
+ assert_eq!(files.0.lock().unwrap()[path], b"first");
+ }
+ }
}