Skip to content

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

fix(engine): Edit Original opens only regular files with a media extension

aiongg3984ef90ffc0
diff --git a/crates/engine/src/project_tools.rs b/crates/engine/src/project_tools.rs
index bcda38356..dc9553af5 100644
--- a/crates/engine/src/project_tools.rs
+++ b/crates/engine/src/project_tools.rs
@@ -13,7 +13,7 @@
//! | `file.mediaPropertiesFile` / `file.mediaProperties` | File ▸ Get Media File Properties for ▸ File… / Selection… (⇧⌘H) |
//! | `file.projectSettings.general` / `.scratchDisks` | File ▸ Project Settings ▸ General… / Scratch Disks… |
//! | `edit.find` / `edit.findNext` | Edit ▸ Find… (⌘F) / Find Next |
-//! | `edit.editOriginal` | Edit ▸ Edit Original (⌘E): the frontend opens the files |
+//! | `edit.editOriginal` | Edit ▸ Edit Original (⌘E): the frontend opens the files (regular files with a media extension only) |
//! | `file.reveal` | Project panel ▸ Reveal in Finder / Show in Explorer: the frontend reveals the media file |
//! | `clip.editOffline` | Clip ▸ Edit Offline… |
//! | `clip.sourceSettings` | Clip ▸ Source Settings… |
@@ -1005,6 +1005,21 @@ fn project_settings_scratch(s: &mut Session, p: &Value) -> Result<Value> {
// Edit Original, clip items
// ---------------------------------------------------------------------------------------------
+/// Whether Edit Original may hand `path` to the system's default app: a regular file (after
+/// following links) whose name, and the name it links to, have an extension the importer takes.
+/// Anything else (a folder or app bundle, a script, a shortcut, a launcher) is refused.
+pub(crate) fn openable_original(path: &str) -> std::result::Result<(), String> {
+ let name = std::path::Path::new(path).file_name().map_or_else(|| path.to_string(), |n| n.to_string_lossy().into_owned());
+ let real = std::fs::canonicalize(path).map_err(|e| format!("can't open “{name}”: {e}"))?;
+ if !real.is_file() {
+ return Err(format!("“{name}” is not a file"));
+ }
+ if !filmcraft_media::is_importable(std::path::Path::new(path)) || !filmcraft_media::is_importable(&real) {
+ return Err(format!("Edit Original opens media files only; “{name}” is not one"));
+ }
+ Ok(())
+}
+
fn edit_original(s: &mut Session, p: &Value) -> Result<Value> {
let mut paths = Vec::new();
for id in targets(s, p) {
@@ -1017,6 +1032,9 @@ fn edit_original(s: &mut Session, p: &Value) -> Result<Value> {
if paths.is_empty() {
return Err(EngineError::Other("select a clip with a media file".into()));
}
+ for path in &paths {
+ openable_original(path).map_err(|e| bad("edit.editOriginal", e))?;
+ }
Ok(json!({"paths": paths}))
}
diff --git a/crates/engine/src/project_tools_tests.rs b/crates/engine/src/project_tools_tests.rs
index 90b1148ee..075525654 100644
--- a/crates/engine/src/project_tools_tests.rs
+++ b/crates/engine/src/project_tools_tests.rs
@@ -322,6 +322,46 @@ fn media_properties_settings_and_scratch_disks() {
assert!(s.execute("file.projectSettings.scratchDisks", json!({"captured": 5})).is_err());
}
+/// Edit Original hands only regular media files to the system opener.
+#[test]
+fn edit_original_opens_media_files_only() {
+ let d = tmp_dir("orig-kinds");
+ for f in ["clip.mov", "Still.PNG", "run.bat", "app.desktop", "clip.mov.lnk"] {
+ std::fs::write(d.join(f), b"").unwrap();
+ }
+ for f in ["Tool.app", "folder.mov"] {
+ std::fs::create_dir_all(d.join(f)).unwrap();
+ }
+ let ok = |f: &str| crate::project_tools::openable_original(&d.join(f).to_string_lossy()).is_ok();
+ assert!(ok("clip.mov") && ok("Still.PNG"));
+ for f in ["run.bat", "app.desktop", "clip.mov.lnk", "Tool.app", "folder.mov", "missing.mov", ""] {
+ assert!(!ok(f), "{f}");
+ }
+ #[cfg(unix)]
+ {
+ std::os::unix::fs::symlink(d.join("app.desktop"), d.join("link.mov")).unwrap();
+ assert!(!ok("link.mov"), "a media name linking to another kind of file");
+ std::os::unix::fs::symlink(d.join("clip.mov"), d.join("link.desktop")).unwrap();
+ assert!(!ok("link.desktop"), "another kind of name linking to a media file");
+ }
+ // The command refuses a clip whose file is not media, and returns no paths to open.
+ let path = d.join("take.mp4");
+ make_movie(&path, DemoScene::Aurora, 64, 36, 8);
+ let (mut s, items, _) = session_with(&[&path]);
+ let id = items[0];
+ let bat = d.join("run.bat").to_string_lossy().into_owned();
+ s.edit("relink", |pr, _| {
+ if let Some(ItemKind::Media(m)) = pr.item_mut(id).map(|it| &mut it.kind) {
+ m.media = filmcraft_project::MediaRef::File { path: bat };
+ }
+ Ok(())
+ })
+ .unwrap();
+ s.execute("project.select", json!({"items": [id.0]})).unwrap();
+ let e = s.execute("edit.editOriginal", json!({})).unwrap_err().to_string();
+ assert!(e.contains("run.bat"), "{e}");
+}
+
#[test]
fn edit_original_update_metadata_and_clip_items() {
let dir = tmp_dir("orig");