mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Fix file name sanitizing
This commit is contained in:
parent
67bbc00d87
commit
8bb2c9c750
4 changed files with 23 additions and 19 deletions
|
|
@ -237,6 +237,7 @@ impl Execute for Archive {
|
|||
}
|
||||
.to_string(),
|
||||
primary,
|
||||
true,
|
||||
)),
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -209,6 +209,7 @@ impl Execute for Download {
|
|||
}
|
||||
.to_string(),
|
||||
&format,
|
||||
true,
|
||||
)),
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -63,15 +63,24 @@ impl Format {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn format_string(s: String, format: &Format) -> String {
|
||||
s.replace("{title}", &format.title)
|
||||
.replace("{series_name}", &format.series_name)
|
||||
.replace("{season_name}", &format.season_title)
|
||||
.replace("{audio}", &format.audio.to_string())
|
||||
.replace("{resolution}", &format.stream.resolution.to_string())
|
||||
.replace("{season_number}", &format.season_number.to_string())
|
||||
.replace("{episode_number}", &format.number.to_string())
|
||||
.replace("{series_id}", &format.series_id)
|
||||
.replace("{season_id}", &format.season_id)
|
||||
.replace("{episode_id}", &format.id)
|
||||
/// Formats the given string if it has specific pattern in it. It's possible to sanitize it which
|
||||
/// removes characters which can cause failures if the output string is used as a file name.
|
||||
pub fn format_string(s: String, format: &Format, sanitize: bool) -> String {
|
||||
let sanitize_func = if sanitize {
|
||||
|s: &str| sanitize_filename::sanitize(s)
|
||||
} else {
|
||||
// converting this to a string is actually unnecessary
|
||||
|s: &str| s.to_string()
|
||||
};
|
||||
|
||||
s.replace("{title}", &sanitize_func(&format.title))
|
||||
.replace("{series_name}", &sanitize_func(&format.series_name))
|
||||
.replace("{season_name}", &sanitize_func(&format.season_title))
|
||||
.replace("{audio}", &sanitize_func(&format.audio.to_string()))
|
||||
.replace("{resolution}", &sanitize_func(&format.stream.resolution.to_string()))
|
||||
.replace("{season_number}", &sanitize_func(&format.season_number.to_string()))
|
||||
.replace("{episode_number}", &sanitize_func(&format.number.to_string()))
|
||||
.replace("{series_id}", &sanitize_func(&format.series_id))
|
||||
.replace("{season_id}", &sanitize_func(&format.season_id))
|
||||
.replace("{episode_id}", &sanitize_func(&format.id))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,12 +50,5 @@ pub fn free_file(mut path: PathBuf) -> PathBuf {
|
|||
|
||||
path.set_file_name(format!("{} ({}).{}", filename, i, ext))
|
||||
}
|
||||
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(),
|
||||
))
|
||||
path
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue