Extend function to get free file

This commit is contained in:
ByteDream 2022-12-02 22:06:08 +01:00
parent e9b3088cde
commit afab3826c9
6 changed files with 37 additions and 17 deletions

View file

@ -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"

View file

@ -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"

View file

@ -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?;

View file

@ -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())

View file

@ -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<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.
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()))
}