diff --git a/Cargo.lock b/Cargo.lock index ffdd71b..ff1f3dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,6 +292,7 @@ dependencies = [ "num_cpus", "regex", "rustls-native-certs", + "sanitize-filename", "signal-hook", "sys-locale", "tempfile", @@ -1361,6 +1362,16 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +[[package]] +name = "sanitize-filename" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c502bdb638f1396509467cb0580ef3b29aa2a45c5d43e5d84928241280296c" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "schannel" version = "0.1.20" diff --git a/crunchy-cli-core/Cargo.lock b/crunchy-cli-core/Cargo.lock index 4fa983a..b842f38 100644 --- a/crunchy-cli-core/Cargo.lock +++ b/crunchy-cli-core/Cargo.lock @@ -260,6 +260,7 @@ dependencies = [ "num_cpus", "regex", "rustls-native-certs", + "sanitize-filename", "signal-hook", "sys-locale", "tempfile", @@ -1323,6 +1324,16 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" +[[package]] +name = "sanitize-filename" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c502bdb638f1396509467cb0580ef3b29aa2a45c5d43e5d84928241280296c" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "schannel" version = "0.1.20" diff --git a/crunchy-cli-core/Cargo.toml b/crunchy-cli-core/Cargo.toml index 30e6d44..d797098 100644 --- a/crunchy-cli-core/Cargo.toml +++ b/crunchy-cli-core/Cargo.toml @@ -20,6 +20,7 @@ isahc = { git = "https://github.com/sagebind/isahc", rev = "c39f6f8" } log = { version = "0.4", features = ["std"] } num_cpus = "1.13" regex = "1.6" +sanitize-filename = "0.4" signal-hook = "0.3" tempfile = "3.3" terminal_size = "0.2" diff --git a/crunchy-cli-core/src/cli/archive.rs b/crunchy-cli-core/src/cli/archive.rs index a057aed..e798e75 100644 --- a/crunchy-cli-core/src/cli/archive.rs +++ b/crunchy-cli-core/src/cli/archive.rs @@ -206,8 +206,7 @@ impl Execute for Archive { .to_string(), primary, )), - ) - .0; + ); info!( "Downloading {} to '{}'", @@ -387,7 +386,7 @@ async fn download_video(ctx: &Context, format: &Format, only_audio: bool) -> Res ctx, &mut ffmpeg.stdin.unwrap(), Some(format!("Download {}", format.audio)), - format.stream.clone() + format.stream.clone(), ) .await?; diff --git a/crunchy-cli-core/src/cli/download.rs b/crunchy-cli-core/src/cli/download.rs index 8535869..acb086b 100644 --- a/crunchy-cli-core/src/cli/download.rs +++ b/crunchy-cli-core/src/cli/download.rs @@ -178,8 +178,7 @@ impl Execute for Download { .to_string(), &format, )), - ) - .0; + ); let use_ffmpeg = if let Some(extension) = path.extension() { if extension != "ts" { @@ -228,11 +227,7 @@ impl Execute for Download { } } -async fn download_ffmpeg( - ctx: &Context, - variant_data: VariantData, - target: &Path, -) -> Result<()> { +async fn download_ffmpeg(ctx: &Context, variant_data: VariantData, target: &Path) -> Result<()> { let ffmpeg = Command::new("ffmpeg") .stdin(Stdio::piped()) .stdout(Stdio::null()) diff --git a/crunchy-cli-core/src/utils/os.rs b/crunchy-cli-core/src/utils/os.rs index 317381d..92e4e9f 100644 --- a/crunchy-cli-core/src/utils/os.rs +++ b/crunchy-cli-core/src/utils/os.rs @@ -19,7 +19,7 @@ pub fn has_ffmpeg() -> bool { } } -/// Any tempfiles should be created with this function. The prefix and directory of every file +/// Any tempfile should be created with this function. The prefix and directory of every file /// created with this method stays the same which is helpful to query all existing tempfiles and /// e.g. remove them in a case of ctrl-c. Having one function also good to prevent mistakes like /// setting the wrong prefix if done manually. @@ -36,17 +36,20 @@ pub fn tempfile>(suffix: S) -> io::Result { } /// Check if the given path exists and rename it until the new (renamed) file does not exist. -pub fn free_file(mut path: PathBuf) -> (PathBuf, bool) { +pub fn free_file(mut path: PathBuf) -> PathBuf { let mut i = 0; while path.exists() { i += 1; - let ext = path.extension().unwrap().to_str().unwrap(); - let mut filename = path.file_name().unwrap().to_str().unwrap(); - - filename = &filename[0..filename.len() - ext.len() - 1]; + let ext = path.extension().unwrap().to_string_lossy(); + let filename = path.file_stem().unwrap().to_string_lossy(); path.set_file_name(format!("{} ({}).{}", filename, i, ext)) } - (path, i != 0) + sanitize_file(path) +} + +/// Sanitizes the given path to not contain any invalid file character. +pub fn sanitize_file(path: PathBuf) -> PathBuf { + path.with_file_name(sanitize_filename::sanitize(path.file_name().unwrap().to_string_lossy())) }