diff --git a/src/main.rs b/src/main.rs index f697275..7acd983 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use lofty::file::TaggedFileExt; use lofty::probe::Probe; use lofty::tag::Accessor; use lofty::tag::Tag; -use std::fs::{create_dir_all, rename}; +use std::fs::{copy, create_dir_all, rename}; use std::path::PathBuf; #[derive(Parser)] @@ -31,7 +31,6 @@ fn main() -> Result<(), Box> { Commands::Show { file } => { println!("Show file: {:?}", file); let tag = get_metadata(file.to_path_buf()); - println!("{}", tag.title().as_deref().unwrap_or("None")); for item in tag.items() { println!("{:?}", item) @@ -59,15 +58,41 @@ fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag { } fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box> { - let target_dir = PathBuf::from("./test"); + let target_dir = PathBuf::from("./"); + let artist = tag.artist(); - let mut final_target = PathBuf::new(); - if let Some(artist_path) = artist { - final_target = target_dir.join(artist_path.as_ref()).join(&file_path); - let dir_to_create = target_dir.join(artist_path.as_ref()); - let _ = create_dir_all(dir_to_create); + let album = tag.album(); + let title = tag.title(); + + let mut target = PathBuf::new(); + + if let (Some(artist_path), Some(album_path), Some(title_path)) = (artist, album, title) { + if album_path.is_empty() { + let dir_to_create = target_dir + .join(artist_path.as_ref()) + .join(title_path.as_ref()); + + let _ = create_dir_all(dir_to_create); + + target = target_dir + .join(artist_path.as_ref()) + .join(title_path.as_ref()) + .join(&file_path); + } else { + let dir_to_create = target_dir + .join(artist_path.as_ref()) + .join(album_path.as_ref()); + + let _ = create_dir_all(dir_to_create); + + target = target_dir + .join(artist_path.as_ref()) + .join(album_path.as_ref()) + .join(&file_path); + } } - println!("{:?}", final_target); - rename(file_path, final_target)?; + println!("{:?}", target); + //rename(file_path, target)?; + copy(file_path, target)?; Ok(()) }