Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

feat: implement filename sanitization function to clean path components

Tonho8fc4b4ead0f6
diff --git a/src-tauri/src/core/filename.rs b/src-tauri/src/core/filename.rs
index 587712d5..a0abe6a3 100644
--- a/src-tauri/src/core/filename.rs
+++ b/src-tauri/src/core/filename.rs
@@ -1 +1,28 @@
-// TODO: Implement filename templating and sanitization
+use regex::Regex;
+
+pub fn sanitize_path_component(name: &str) -> String {
+ let name = name.trim().replace('\t', "").replace('\n', "");
+ let name = Regex::new(r"\s+").unwrap().replace_all(&name, " ");
+ let name = name.replace(" | ", "|");
+
+ let name = name.trim_end_matches(|c| matches!(c, ' ' | '-' | '.' | ';'));
+
+ let forbidden: &[(char, char)] = &[
+ ('<', '<'),
+ ('>', '>'),
+ (':', '꞉'),
+ ('"', '"'),
+ ('/', '⧸'),
+ ('\\', '\'),
+ ('|', '|'),
+ ('?', '?'),
+ ('*', ' '),
+ ];
+
+ let mut result = name.to_string();
+ for (from, to) in forbidden {
+ result = result.replace(*from, &to.to_string());
+ }
+
+ result.trim().to_string()
+}