Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(media): a path spelled another way is not imported twice on Windows (#440)

chiarorosa73d8e9d40765
diff --git a/src/crates/concat-project/src/commands/clips.rs b/src/crates/concat-project/src/commands/clips.rs
index c6fad09a..37756f73 100644
--- a/src/crates/concat-project/src/commands/clips.rs
+++ b/src/crates/concat-project/src/commands/clips.rs
@@ -688,7 +688,7 @@ pub(super) fn apply(
let media = project
.media
.iter()
- .find(|media| media.path == path)
+ .find(|media| super::media::same_path(&media.path, &path))
.expect("imported above");
let mut merged =
default_clip(first.id.clone(), first.track_id.clone(), media, first.start);
diff --git a/src/crates/concat-project/src/commands/media.rs b/src/crates/concat-project/src/commands/media.rs
index f1e0276b..1ec95877 100644
--- a/src/crates/concat-project/src/commands/media.rs
+++ b/src/crates/concat-project/src/commands/media.rs
@@ -8,6 +8,24 @@
use super::*;
+/// Whether two bin paths name the same file. Windows reads `\` and `/`
+/// alike and ignores case, so there `C:\v\a.mp4` and `c:/v/A.mp4` are one
+/// file; elsewhere the paths must match exactly.
+pub(super) fn same_path(a: &str, b: &str) -> bool {
+ same_path_as(a, b, cfg!(windows))
+}
+
+fn same_path_as(a: &str, b: &str, windows: bool) -> bool {
+ if a == b {
+ return true;
+ }
+ let fold = |byte: u8| match byte {
+ b'\\' => b'/',
+ other => other.to_ascii_lowercase(),
+ };
+ windows && a.len() == b.len() && a.bytes().zip(b.bytes()).all(|(x, y)| fold(x) == fold(y))
+}
+
/// Applies one of this module's commands. Any other is a routing error.
pub(super) fn apply(
project: &mut Project,
@@ -19,7 +37,7 @@ pub(super) fn apply(
if project
.media
.iter()
- .any(|existing| existing.path == item.path)
+ .any(|existing| same_path(&existing.path, &item.path))
{
return Ok(Outcome::default());
}
@@ -196,3 +214,22 @@ pub(super) fn apply(
_ => unreachable!("commands::apply routes only this module's commands here"),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::same_path_as;
+
+ #[test]
+ fn windows_reads_either_slash_and_ignores_case() {
+ assert!(same_path_as(r"C:\v\a.mp4", "c:/v/A.mp4", true));
+ assert!(!same_path_as(r"C:\v\a.mp4", r"C:\v\b.mp4", true));
+ assert!(!same_path_as(r"C:\v\a.mp4", r"C:\v\a.mp4.bak", true));
+ }
+
+ #[test]
+ fn elsewhere_a_path_matches_only_itself() {
+ assert!(same_path_as("/v/a.mp4", "/v/a.mp4", false));
+ assert!(!same_path_as(r"/v/a\b.mp4", "/v/a/b.mp4", false));
+ assert!(!same_path_as("/v/a.mp4", "/v/A.mp4", false));
+ }
+}
diff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rs
index 147c97ea..2e8c4de1 100644
--- a/src/crates/concat-project/src/lib.rs
+++ b/src/crates/concat-project/src/lib.rs
@@ -1509,6 +1509,19 @@ mod tests {
assert_eq!(editor.project().media.len(), 1);
}
+ #[cfg(windows)]
+ #[test]
+ fn a_path_spelled_another_way_is_not_imported_twice_on_windows() {
+ let mut editor = Editor::new();
+ editor
+ .apply(media(r"C:\Videos\a.mp4", 10.0, true))
+ .expect("adds");
+ editor
+ .apply(media("c:/videos/A.mp4", 10.0, true))
+ .expect("no-op");
+ assert_eq!(editor.project().media.len(), 1);
+ }
+
#[test]
fn replace_clip_media_can_move_the_in_point_to_the_copy() {
let (mut editor, _, clip_id) = fixture();