From c2e953043e02d6049ee8c92f1bbc53b6e071f5e1 Mon Sep 17 00:00:00 2001 From: ByteDream Date: Sat, 6 May 2023 22:06:52 +0200 Subject: [PATCH] Fix output filename if file stem is empty but file exists --- crunchy-cli-core/src/utils/os.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crunchy-cli-core/src/utils/os.rs b/crunchy-cli-core/src/utils/os.rs index bd30c2a..f6d9ad0 100644 --- a/crunchy-cli-core/src/utils/os.rs +++ b/crunchy-cli-core/src/utils/os.rs @@ -52,9 +52,18 @@ pub fn free_file(mut path: PathBuf) -> (PathBuf, bool) { while path.exists() { i += 1; - let ext = path.extension().unwrap_or_default().to_string_lossy(); + let mut ext = path.extension().unwrap_or_default().to_str().unwrap(); let mut filename = path.file_stem().unwrap_or_default().to_str().unwrap(); + // if the extension is empty, the filename without extension is probably empty + // (e.g. `.mp4`). in this case Rust assumes that `.mp4` is the file stem rather than the + // extension. if this is the case, set the extension to the file stem and make the file stem + // empty + if ext.is_empty() { + ext = filename; + filename = ""; + } + if filename.ends_with(&format!(" ({})", i - 1)) { filename = filename.strip_suffix(&format!(" ({})", i - 1)).unwrap(); }