diff --git a/README.md b/README.md index 5d81b09..a176d69 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Options: By default files are saved in the current directory. -S, --skip-albums - Skip downloading these albums, note that albums need to be delimited by ',' eg: -s 'one,two' or --skip-albums=one,two + Skip downloading these albums, note that albums need to be delimited by ',' eg: -S 'one,two' or --skip-albums=one,two -l, --list-available List albums/tracks available for download diff --git a/src/cli.rs b/src/cli.rs index be4c6ea..9900270 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -44,7 +44,7 @@ note that `.mp3` is appended automatically.")] pub(crate) track_format: Option, /// Skip downloading these albums, note that albums need to be delimited by ',' - /// eg: -s 'one,two' or --skip-albums=one,two + /// eg: -S 'one,two' or --skip-albums=one,two #[clap(short = 'S', long, value_name = "ALBUMS", value_delimiter = ',')] pub(crate) skip_albums: Option>, @@ -95,7 +95,9 @@ impl Default for Config { fn validate_format(f: &str) -> Result { let vars = format_container("", "", "", ""); - strfmt(f, &vars).map_err(|err| err.to_string()) + strfmt(f, &vars).map_err(|err| err.to_string())?; + + Ok(f.to_string()) } pub fn expand_tilde(p: &str) -> PathBuf { diff --git a/src/lib/spider.rs b/src/lib/spider.rs index 8ea5e24..8f2f1ed 100644 --- a/src/lib/spider.rs +++ b/src/lib/spider.rs @@ -86,7 +86,7 @@ fn scrape_by_application_ld_json(dom: &Html) -> Option { vec![Track { num: 1, - name: track_name.to_string().replace("/", ":"), + name: track_name.to_string(), url, lyrics: None, album: album.clone(), @@ -113,9 +113,7 @@ fn scrape_by_application_ld_json(dom: &Html) -> Option { Some(Track { num: track.get("position").i32(), - name: decode_html_entities(&track.get("item.name").to_string()) - .replace("/", ":") - .into(), + name: decode_html_entities(&track.get("item.name").to_string()).into(), url: decode_html_entities(&url).to_string(), lyrics: Some(track.get("item.recordingOf.lyrics.text").to_string()), album: album.clone(), diff --git a/src/lib/utils.rs b/src/lib/utils.rs index 1952f79..39b937e 100644 --- a/src/lib/utils.rs +++ b/src/lib/utils.rs @@ -13,12 +13,23 @@ use std::{ use super::models::{Album, Track}; +// Function to sanitize filenames by replacing invalid characters +fn sanitize_filename(filename: &str) -> String { + filename.replace(|c: char| { + c == '<' || c == '>' || c == ':' || c == '"' || c == '/' || + c == '\\' || c == '|' || c == '?' || c == '*' + }, "_") +} + pub fn prepare_directory(path: Option<&PathBuf>, album: &Album) -> Result { + let artist = sanitize_filename(&album.artist); + let album_name = sanitize_filename(&album.album); + let path = match path { - Some(path) => Path::new(path).join(&album.artist), - None => PathBuf::from(&album.artist), + Some(path) => Path::new(path).join(artist), + None => PathBuf::from(artist), } - .join(&album.album); + .join(album_name); if !path.exists() { fs::create_dir_all(&path)?; @@ -35,7 +46,8 @@ pub fn make_path(track: &Track, root: &Path, track_format: &String) -> PathBuf { parse_track_template(track_format, track) }; - root.join(file_name).with_extension("mp3") + let sanitized_file_name = sanitize_filename(&file_name); + root.join(sanitized_file_name).with_extension("mp3") } pub fn track_path(track: &Track, root: &Path, track_format: &String) -> Result { @@ -45,7 +57,8 @@ pub fn track_path(track: &Track, root: &Path, track_format: &String) -> Result