From e739ed32941eeb382c59a0ac360232d5f939f1f8 Mon Sep 17 00:00:00 2001 From: KeksNino Date: Sat, 22 Aug 2026 01:58:13 +0200 Subject: [PATCH] feat: move files by artist tag --- .gitignore | 0 Cargo.lock | 0 Cargo.toml | 0 flake.lock | 0 flake.nix | 0 rust-toolchain.toml | 0 src/main.rs | 41 +++++++++++++++++++++++++++++++++++------ 7 files changed, 35 insertions(+), 6 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 Cargo.lock mode change 100644 => 100755 Cargo.toml mode change 100644 => 100755 flake.lock mode change 100644 => 100755 flake.nix mode change 100644 => 100755 rust-toolchain.toml mode change 100644 => 100755 src/main.rs diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/Cargo.lock b/Cargo.lock old mode 100644 new mode 100755 diff --git a/Cargo.toml b/Cargo.toml old mode 100644 new mode 100755 diff --git a/flake.lock b/flake.lock old mode 100644 new mode 100755 diff --git a/flake.nix b/flake.nix old mode 100644 new mode 100755 diff --git a/rust-toolchain.toml b/rust-toolchain.toml old mode 100644 new mode 100755 diff --git a/src/main.rs b/src/main.rs old mode 100644 new mode 100755 index 1caa2dc..e70d279 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,26 @@ use clap::Parser; use lofty::file::TaggedFileExt; use lofty::probe::Probe; -use std::fs::rename; +use lofty::tag::Accessor; +use lofty::tag::Tag; +use std::fs::{create_dir_all, rename}; use std::path::PathBuf; #[derive(Parser)] #[command(version, about, long_about = None)] struct Args { file: PathBuf, + + #[arg(short)] + import: bool, + + #[arg(short)] + show: bool, } fn main() -> Result<(), Box> { let args = Args::parse(); let file_path = args.file; - let target_dir = PathBuf::from("./test"); - let target = target_dir.join(&file_path); let tagged_file = Probe::open(&file_path) .expect("ERROR: Bad path provided!") @@ -22,6 +28,15 @@ fn main() -> Result<(), Box> { .expect("ERROR: Failed to read file!"); let id3v2 = tagged_file.primary_tag(); + let tag = match tagged_file.primary_tag() { + Some(primary_tag) => primary_tag, + None => tagged_file.first_tag().expect("ERROR: No tags found!"), + }; + + println!("{}", tag.title().as_deref().unwrap_or("None")); + + let album_artist = tag.get_string(lofty::tag::ItemKey::AlbumArtist); + println!("AlbumArtist: {:?}", album_artist); if let Some(tag) = id3v2 { for item in tag.items() { @@ -29,12 +44,26 @@ fn main() -> Result<(), Box> { } } - let _ = move_files(target, file_path); + if args.import { + let _ = move_files(file_path, tag.clone()); + } Ok(()) } -fn move_files(target: PathBuf, file_path: PathBuf) -> Result<(), Box> { - rename(file_path, target)?; +fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box> { + let target_dir = PathBuf::from("./test"); + 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); + } + + println!("{:?}", final_target); + rename(file_path, final_target)?; Ok(()) }