diff --git a/Cargo.lock b/Cargo.lock index 26b85fb..c97727b 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,6 +141,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + [[package]] name = "heck" version = "0.5.0" @@ -200,6 +206,7 @@ name = "music-sort" version = "0.1.0" dependencies = [ "clap", + "glob", "lofty", ] diff --git a/Cargo.toml b/Cargo.toml index 7c291bf..198cc00 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2024" [dependencies] clap = { version = "4.6.6", features = ["derive"] } +glob = "0.3.4" lofty = "0.25.1" diff --git a/src/main.rs b/src/main.rs index b871461..9a331ad 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ use clap::{Parser, Subcommand}; +use glob::glob; use lofty::file::TaggedFileExt; use lofty::prelude::ItemKey; use lofty::probe::Probe; use lofty::tag::Accessor; use lofty::tag::Tag; -use std::fs::{copy, create_dir_all, rename}; +use std::ffi::OsStr; +use std::fs::{copy, create_dir_all, read_dir, rename}; use std::path::PathBuf; #[derive(Parser)] @@ -22,16 +24,32 @@ enum Commands { fn main() -> Result<(), Box> { let args = Args::parse(); + let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string); match &args.command { Commands::Import { file } => { println!("Importing file: {:?}", file); - let tag = get_metadata(file.to_path_buf()); - let _ = move_files(file.clone(), tag.clone()); + if file.is_dir() { + for music_file in read_dir(file)? { + let path = music_file?.path(); + if path + .extension() + .and_then(|ext| ext.to_str()) + .is_some_and(|ext| extra_files.iter().any(|x| x == ext)) + { + continue; + } + let tag = get_metadata(&path); + let _ = move_files(path, tag.clone()); + } + } else { + let tag = get_metadata(file); + let _ = move_files(file.clone(), tag.clone()); + } } Commands::Show { file } => { println!("Show file: {:?}", file); - let tag = get_metadata(file.to_path_buf()); + let tag = get_metadata(file); for item in tag.items() { println!("{:?}", item) @@ -42,8 +60,8 @@ fn main() -> Result<(), Box> { Ok(()) } -fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag { - let tagged_file = Probe::open(&file_path) +fn get_metadata>(file_path: P) -> lofty::tag::Tag { + let tagged_file = Probe::open(file_path.as_ref()) .expect("ERROR: Bad path provided!") .read() .expect("ERROR: Failed to read file!"); @@ -56,7 +74,8 @@ fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag { } fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box> { - let target_dir = PathBuf::from("./"); + println!("{:?}", file_path); + let target_dir = PathBuf::from("/home/user/Desktop/coding/keks-sort"); let artist = tag.artist(); let album = tag.album(); @@ -80,10 +99,10 @@ fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box