Sanitize the full output filename (#253)

This commit is contained in:
bytedream 2023-10-13 11:41:56 +02:00
parent e5db8e9504
commit 13335c020b

View file

@ -371,51 +371,43 @@ impl Format {
/// Formats the given string if it has specific pattern in it. It's possible to sanitize it which /// 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. /// removes characters which can cause failures if the output string is used as a file name.
pub fn format_path(&self, path: PathBuf, sanitize: bool) -> PathBuf { pub fn format_path(&self, path: PathBuf, sanitize: bool) -> PathBuf {
let sanitize_func = if sanitize { let path = path
|s: &str| sanitize_filename::sanitize(s) .to_string_lossy()
} else { .to_string()
// converting this to a string is actually unnecessary .replace("{title}", &self.title)
|s: &str| s.to_string()
};
let as_string = path.to_string_lossy().to_string();
PathBuf::from(
as_string
.replace("{title}", &sanitize_func(&self.title))
.replace( .replace(
"{audio}", "{audio}",
&sanitize_func(
&self &self
.locales .locales
.iter() .iter()
.map(|(a, _)| a.to_string()) .map(|(a, _)| a.to_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("|"), .join("|"),
),
) )
.replace("{resolution}", &sanitize_func(&self.resolution.to_string())) .replace("{resolution}", &self.resolution.to_string())
.replace("{series_id}", &sanitize_func(&self.series_id)) .replace("{series_id}", &self.series_id)
.replace("{series_name}", &sanitize_func(&self.series_name)) .replace("{series_name}", &self.series_name)
.replace("{season_id}", &sanitize_func(&self.season_id)) .replace("{season_id}", &self.season_id)
.replace("{season_name}", &sanitize_func(&self.season_title)) .replace("{season_name}", &self.season_title)
.replace( .replace(
"{season_number}", "{season_number}",
&sanitize_func(&format!("{:0>2}", self.season_number.to_string())), &format!("{:0>2}", self.season_number.to_string()),
) )
.replace("{episode_id}", &sanitize_func(&self.episode_id)) .replace("{episode_id}", &self.episode_id)
.replace( .replace(
"{episode_number}", "{episode_number}",
&sanitize_func(&format!("{:0>2}", self.episode_number.to_string())), &format!("{:0>2}", self.episode_number.to_string()),
) )
.replace( .replace(
"{relative_episode_number}", "{relative_episode_number}",
&sanitize_func(&format!( &self.relative_episode_number.unwrap_or_default().to_string(),
"{:0>2}", );
self.relative_episode_number.unwrap_or_default().to_string()
)), if sanitize {
), PathBuf::from(sanitize_filename::sanitize(path))
) } else {
PathBuf::from(path)
}
} }
pub fn visual_output(&self, dst: &Path) { pub fn visual_output(&self, dst: &Path) {