diff --git a/src/main.rs b/src/main.rs index bf1d579..ac2fb9a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use clap::{Parser, Subcommand}; +//use ini::Ini; use lofty::file::TaggedFileExt; use lofty::probe::Probe; use std::ffi::OsStr; @@ -34,6 +35,22 @@ enum Commands { } fn main() -> Result<(), Box> { + //let mut conf = Ini::new(); + //conf.with_section(Some("General")) + // .set("target_directory", ""); + ////conf.write_to_file("conf.ini").unwrap(); + // + //let conf = Ini::load_from_file("conf.ini").unwrap(); + // + //let section = conf.section(Some("General")).unwrap(); + //println!("target_dir={:?}", section.get("target_directory")); + // + //for (_sec, prop) in &conf { + // for (key, value) in prop.iter() { + // println!("{:?}:{:?}", key, value); + // } + //} + // let args = Args::parse(); let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string); diff --git a/src/move_files.rs b/src/move_files.rs index 3af86ab..771a5fe 100644 --- a/src/move_files.rs +++ b/src/move_files.rs @@ -36,20 +36,21 @@ pub fn move_files( return Ok(()); }; - let dir_to_create = target_dir.join(artist_dir).join(album_dir.as_ref()); - create_dir_all(&dir_to_create)?; - let target = target_dir.join(artist_dir).join(album_dir.as_ref()); + let artist_dir = fix_filename(artist_dir); + let album_dir = fix_filename(&album_dir); + let target = target_dir.join(artist_dir).join(album_dir); + create_dir_all(&target)?; let file_name = if let Some(track_number) = track_number { let number: u32 = track_number.parse()?; - PathBuf::from(format!( - "{:02}. {} - {}.flac", - number, - artist.unwrap(), - title.unwrap(), - )) + let artist = fix_filename(&artist.unwrap()); + let title = fix_filename(&title.unwrap()); + + PathBuf::from(format!("{:02}. {} - {}.flac", number, artist, title,)) } else { - PathBuf::from(format!("01. {} - {}.flac", artist.unwrap(), title.unwrap(),)) + let artist = fix_filename(&artist.unwrap()); + let title = fix_filename(&title.unwrap()); + PathBuf::from(format!("01. {} - {}.flac", artist, title)) }; if should_copy { @@ -86,6 +87,15 @@ pub fn move_files( Ok(()) } +fn fix_filename(name: &str) -> String { + name.chars() + .map(|c| match c { + '/' => '_', + c => c, + }) + .collect() +} + fn write_log(file_path: PathBuf) -> Result<(), Box> { let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set")); let data_dir = home_dir.join(".local").join("share").join("music-sort");