Skip to content

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

Windows: export paths expand ~, accept trailing \ and verbatim paths

Tom2b843d3d810b
diff --git a/crates/engine/src/export_tests.rs b/crates/engine/src/export_tests.rs
index 474b466a3..a934969d6 100644
--- a/crates/engine/src/export_tests.rs
+++ b/crates/engine/src/export_tests.rs
@@ -484,6 +484,23 @@ fn queue_exports_several_sequences_and_ranges() {
assert_eq!(len("parts_2.wav"), 44 + 12_000 * 4, "0.25 s of 16-bit stereo");
}
+#[test]
+fn queue_folder_paths_expand_home_and_take_either_separator() {
+ let mut s = demo();
+ let first = s.state.active_sequence.unwrap();
+ let second = s.execute("file.newSequence", json!({"name": "Second Cut", "width": 640, "height": 360, "fps": 25})).unwrap()["sequence"].as_u64().unwrap();
+ let preset = "Waveform Audio 48 kHz 16-bit";
+ // a folder that does not exist yet, named by a trailing `\`: each sequence keeps its own name
+ s.execute("export.queue.add", json!({"preset": preset, "path": "not-yet-made\\", "sequences": [first.0, second]})).unwrap();
+ let paths: Vec<String> = queue(&mut s).iter().map(|i| i["path"].as_str().unwrap().to_string()).collect();
+ assert!(paths.iter().any(|p| p.ends_with("Second Cut.wav")), "{paths:?}");
+ if let Some(home) = crate::media_browser::std_home_dir() {
+ s.execute("export.queue.add", json!({"preset": preset, "path": "~/Exports/", "sequence": first.0})).unwrap();
+ let last = queue(&mut s).last().unwrap()["path"].as_str().unwrap().to_string();
+ assert!(last.starts_with(&home), "{last}");
+ }
+}
+
#[test]
fn quick_export_uses_and_remembers_a_preset() {
let mut s = demo();
diff --git a/crates/engine/src/export_tools.rs b/crates/engine/src/export_tools.rs
index b36566c7f..4a1688ca1 100644
--- a/crates/engine/src/export_tools.rs
+++ b/crates/engine/src/export_tools.rs
@@ -304,7 +304,7 @@ pub fn default_export_dir(s: &Session) -> PathBuf {
// the web build's virtual file table: written files are offered as downloads
return PathBuf::from("/exports");
}
- if let Ok(h) = std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) {
+ if let Some(h) = crate::media_browser::std_home_dir() {
let movies = Path::new(&h).join("Movies");
return if movies.is_dir() { movies } else { PathBuf::from(h) };
}
@@ -327,11 +327,19 @@ fn with_extension(path: &str, settings: &ExportSettings) -> String {
}
}
-fn expand_home(p: &str) -> String {
- match (p.strip_prefix("~/"), std::env::var("HOME")) {
- (Some(rest), Ok(h)) => format!("{h}/{rest}"),
+/// `~/` expanded to the home directory (`USERPROFILE` on Windows). On Windows `/` becomes `\`:
+/// after a `\\?\` prefix (what `canonicalize` returns) `/` is not a separator.
+pub fn expand_home(p: &str) -> String {
+ let p = match (p.strip_prefix("~/"), crate::media_browser::std_home_dir()) {
+ (Some(rest), Some(h)) => format!("{h}/{rest}"),
_ => p.to_string(),
- }
+ };
+ if cfg!(windows) { p.replace('/', "\\") } else { p }
+}
+
+/// `x` names a folder: it ends in a separator or already exists as one.
+fn is_folder(x: &str) -> bool {
+ x.ends_with(['/', '\\']) || Path::new(x).is_dir()
}
/// Output path for `seq`: the `path` param (a file, or a directory ending in `/` or existing), else
@@ -339,7 +347,7 @@ fn expand_home(p: &str) -> String {
fn output_path(s: &Session, p: &Value, seq: ItemId, settings: &ExportSettings, suffix: Option<usize>) -> String {
let name = s.project.item(seq).map(|i| i.name.clone()).unwrap_or_else(|| "Sequence".into());
let base = match str_p(p, "path").map(expand_home) {
- Some(x) if x.ends_with('/') || x.ends_with('\\') || Path::new(&x).is_dir() => Path::new(&x).join(file_safe(&name)).to_string_lossy().to_string(),
+ Some(x) if is_folder(&x) => Path::new(&x).join(file_safe(&name)).to_string_lossy().to_string(),
Some(x) => x,
None => default_export_dir(s).join(file_safe(&name)).to_string_lossy().to_string(),
};
@@ -633,11 +641,7 @@ fn queue_add(s: &mut Session, p: &Value) -> Result<Value> {
};
st.range = range_param(s, &project, seq, *r, times, cmd)?;
// several ranges of one sequence get numbered names; several sequences their own names
- let suffix = if many && (ranges.len() > 1 || str_p(p, "path").is_some_and(|x| !x.ends_with('/') && !Path::new(&expand_home(x)).is_dir())) {
- Some(n)
- } else {
- None
- };
+ let suffix = if many && (ranges.len() > 1 || str_p(p, "path").is_some_and(|x| !is_folder(&expand_home(x)))) { Some(n) } else { None };
st.path = output_path(s, p, seq, &st, suffix);
n += 1;
items.push((seq, st));
diff --git a/crates/engine/src/media_browser.rs b/crates/engine/src/media_browser.rs
index 60cc6c244..b7922fd34 100644
--- a/crates/engine/src/media_browser.rs
+++ b/crates/engine/src/media_browser.rs
@@ -663,7 +663,9 @@ pub fn std_volumes() -> Vec<Volume> {
v
}
-/// The desktop user's home directory.
+/// The desktop user's home directory. Windows prefers `USERPROFILE`: shells like Git Bash set
+/// `HOME` to a Unix-style path (`/c/Users/…`) that native file APIs cannot open.
pub fn std_home_dir() -> Option<String> {
- std::env::var("HOME").ok().or_else(|| std::env::var("USERPROFILE").ok()).filter(|h| !h.is_empty())
+ let vars = if cfg!(windows) { ["USERPROFILE", "HOME"] } else { ["HOME", "USERPROFILE"] };
+ vars.iter().find_map(|v| std::env::var(v).ok().filter(|h| !h.is_empty()))
}
diff --git a/crates/ui-egui/src/panels/export_mode.rs b/crates/ui-egui/src/panels/export_mode.rs
index 23d3d7e97..606611cda 100644
--- a/crates/ui-egui/src/panels/export_mode.rs
+++ b/crates/ui-egui/src/panels/export_mode.rs
@@ -129,7 +129,7 @@ pub fn apply_preset(app: &mut FilmcraftApp, name: &str) -> bool {
/// Full output path of the current settings.
pub fn output_path(ex: &ExportUi) -> String {
let name = if ex.file_name.trim().is_empty() { "Export" } else { ex.file_name.trim() };
- let dir = expand_home(ex.location.trim());
+ let dir = filmcraft_engine::export_tools::expand_home(ex.location.trim());
let file = format!("{name}.{}", ex.settings.extension());
if dir.is_empty() { file } else { std::path::Path::new(&dir).join(file).to_string_lossy().to_string() }
}
@@ -1288,10 +1288,3 @@ fn list_row(ui: &mut egui::Ui, t: &Tokens, selected: bool, text: &str, width: f3
ui.painter().text(pos2(r.min.x + 6.0, r.center().y), Align2::LEFT_CENTER, text, Tokens::ui(12.5), if selected { Color32::WHITE } else { t.text });
resp
}
-
-fn expand_home(p: &str) -> String {
- match (p.strip_prefix("~/"), std::env::var("HOME")) {
- (Some(rest), Ok(h)) => format!("{h}/{rest}"),
- _ => p.to_string(),
- }
-}