Skip to content

Commit

Permalink
Allow filename template ?, remove Invalid path warning from renamer
Browse files Browse the repository at this point in the history
  • Loading branch information
Marekkon5 committed Feb 24, 2024
1 parent bb1a012 commit ba4d5e0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions crates/onetagger-autotag/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,22 +475,23 @@ impl AudioFileInfoImpl for AudioFileInfo {

// Convert template into a regex
fn parse_template(template: &str) -> Option<Regex> {
// Regex reserved
let reserved = ".?+*$^()[]/|";
// Regex reserved (skip ? because can be used)
let reserved = ".+*$^()[]/|";
let mut template = template.to_string();
for c in reserved.chars() {
template = template.replace(c, &format!("\\{}", c));
};
// Replace variables
template = template
.replace("%title%", "(?P<title>.+)")
.replace("%artist%", "(?P<artists>.+)")
.replace("%artists%", "(?P<artists>.+)");
.replace("%title%", "(?P<title>.+?)")
.replace("%artist%", "(?P<artists>.+?)")
.replace("%artists%", "(?P<artists>.+?)");
// Remove all remaining variables
let re = Regex::new("%[a-zA-Z0-9 ]+%").unwrap();
template = re.replace_all(&template, "(.+)").to_string();
// Extension
template = format!("{}\\.[a-zA-Z0-9]{{2,4}}$", template).trim().to_string();
debug!("Filename template regex: `{template}`");
// Final regex
Regex::new(&template).ok()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/onetagger-ui/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ async fn handle_message(text: &str, websocket: &mut WebSocket, context: &mut Soc
// Generate new names but don't rename
Action::RenamerPreview { config } => {
let mut renamer = Renamer::new(TemplateParser::parse(&config.template));
let files = renamer.generate(&config, 3)?;
let files = renamer.generate(&config, 3).unwrap_or(vec![]);
send_socket(websocket, json!({
"action": "renamerPreview",
"files": files,
Expand Down

0 comments on commit ba4d5e0

Please sign in to comment.