mirror of
https://github.com/SinTan1729/movie-rename.git
synced 2024-12-25 11:48:37 -06:00
new: Try to sanitize the filename
This commit is contained in:
parent
7cb18202eb
commit
02fea4a71c
1 changed files with 16 additions and 2 deletions
|
@ -27,18 +27,32 @@ impl MovieEntry {
|
|||
|
||||
// Generate desired filename from movie entry
|
||||
pub fn rename_format(&self, mut format: String) -> String {
|
||||
format = format.replace("{title}", self.title.as_str());
|
||||
const PATTERN: &str = "^~#%$*+={}?@'`/\\\"><|:&!";
|
||||
// Try to sanitize the title to avoid some characters
|
||||
let mut title = self.title.clone();
|
||||
title.retain(|c| !PATTERN.contains(c));
|
||||
title.truncate(159);
|
||||
format = format.replace("{title}", title.as_str());
|
||||
|
||||
if self.year.as_str() != "N/A" {
|
||||
format = format.replace("{year}", self.year.as_str());
|
||||
} else {
|
||||
format = format.replace("{year}", "");
|
||||
}
|
||||
|
||||
if self.director.as_str() != "N/A" {
|
||||
format = format.replace("{director}", self.director.as_str());
|
||||
// Try to sanitize the director's name to avoid some characters
|
||||
let mut director = self.director.clone();
|
||||
director.retain(|c| !PATTERN.contains(c));
|
||||
director.truncate(63);
|
||||
format = format.replace("{director}", director.as_str());
|
||||
} else {
|
||||
format = format.replace("{director}", "");
|
||||
}
|
||||
format
|
||||
.trim_matches(|c| "- ".contains(c))
|
||||
.replace("--", "-")
|
||||
.replace("- ", "")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue