mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 04:02:00 -06:00
Extend function to get free file
This commit is contained in:
parent
e9b3088cde
commit
afab3826c9
6 changed files with 37 additions and 17 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -292,6 +292,7 @@ dependencies = [
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
"regex",
|
"regex",
|
||||||
"rustls-native-certs",
|
"rustls-native-certs",
|
||||||
|
"sanitize-filename",
|
||||||
"signal-hook",
|
"signal-hook",
|
||||||
"sys-locale",
|
"sys-locale",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
|
|
@ -1361,6 +1362,16 @@ version = "1.0.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
|
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]]
|
[[package]]
|
||||||
name = "schannel"
|
name = "schannel"
|
||||||
version = "0.1.20"
|
version = "0.1.20"
|
||||||
|
|
|
||||||
11
crunchy-cli-core/Cargo.lock
generated
11
crunchy-cli-core/Cargo.lock
generated
|
|
@ -260,6 +260,7 @@ dependencies = [
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
"regex",
|
"regex",
|
||||||
"rustls-native-certs",
|
"rustls-native-certs",
|
||||||
|
"sanitize-filename",
|
||||||
"signal-hook",
|
"signal-hook",
|
||||||
"sys-locale",
|
"sys-locale",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
|
|
@ -1323,6 +1324,16 @@ version = "1.0.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
|
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]]
|
[[package]]
|
||||||
name = "schannel"
|
name = "schannel"
|
||||||
version = "0.1.20"
|
version = "0.1.20"
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ isahc = { git = "https://github.com/sagebind/isahc", rev = "c39f6f8" }
|
||||||
log = { version = "0.4", features = ["std"] }
|
log = { version = "0.4", features = ["std"] }
|
||||||
num_cpus = "1.13"
|
num_cpus = "1.13"
|
||||||
regex = "1.6"
|
regex = "1.6"
|
||||||
|
sanitize-filename = "0.4"
|
||||||
signal-hook = "0.3"
|
signal-hook = "0.3"
|
||||||
tempfile = "3.3"
|
tempfile = "3.3"
|
||||||
terminal_size = "0.2"
|
terminal_size = "0.2"
|
||||||
|
|
|
||||||
|
|
@ -206,8 +206,7 @@ impl Execute for Archive {
|
||||||
.to_string(),
|
.to_string(),
|
||||||
primary,
|
primary,
|
||||||
)),
|
)),
|
||||||
)
|
);
|
||||||
.0;
|
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Downloading {} to '{}'",
|
"Downloading {} to '{}'",
|
||||||
|
|
@ -387,7 +386,7 @@ async fn download_video(ctx: &Context, format: &Format, only_audio: bool) -> Res
|
||||||
ctx,
|
ctx,
|
||||||
&mut ffmpeg.stdin.unwrap(),
|
&mut ffmpeg.stdin.unwrap(),
|
||||||
Some(format!("Download {}", format.audio)),
|
Some(format!("Download {}", format.audio)),
|
||||||
format.stream.clone()
|
format.stream.clone(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -178,8 +178,7 @@ impl Execute for Download {
|
||||||
.to_string(),
|
.to_string(),
|
||||||
&format,
|
&format,
|
||||||
)),
|
)),
|
||||||
)
|
);
|
||||||
.0;
|
|
||||||
|
|
||||||
let use_ffmpeg = if let Some(extension) = path.extension() {
|
let use_ffmpeg = if let Some(extension) = path.extension() {
|
||||||
if extension != "ts" {
|
if extension != "ts" {
|
||||||
|
|
@ -228,11 +227,7 @@ impl Execute for Download {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn download_ffmpeg(
|
async fn download_ffmpeg(ctx: &Context, variant_data: VariantData, target: &Path) -> Result<()> {
|
||||||
ctx: &Context,
|
|
||||||
variant_data: VariantData,
|
|
||||||
target: &Path,
|
|
||||||
) -> Result<()> {
|
|
||||||
let ffmpeg = Command::new("ffmpeg")
|
let ffmpeg = Command::new("ffmpeg")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
|
|
|
||||||
|
|
@ -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
|
/// 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
|
/// 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.
|
/// setting the wrong prefix if done manually.
|
||||||
|
|
@ -36,17 +36,20 @@ pub fn tempfile<S: AsRef<str>>(suffix: S) -> io::Result<NamedTempFile> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the given path exists and rename it until the new (renamed) file does not exist.
|
/// 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;
|
let mut i = 0;
|
||||||
while path.exists() {
|
while path.exists() {
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
||||||
let ext = path.extension().unwrap().to_str().unwrap();
|
let ext = path.extension().unwrap().to_string_lossy();
|
||||||
let mut filename = path.file_name().unwrap().to_str().unwrap();
|
let filename = path.file_stem().unwrap().to_string_lossy();
|
||||||
|
|
||||||
filename = &filename[0..filename.len() - ext.len() - 1];
|
|
||||||
|
|
||||||
path.set_file_name(format!("{} ({}).{}", filename, i, ext))
|
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()))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue