feat: add fallback if there is no album tag in file

This commit is contained in:
2026-08-22 19:23:56 +02:00
parent fa31b6f48d
commit abdb62850f
+35 -10
View File
@@ -3,7 +3,7 @@ use lofty::file::TaggedFileExt;
use lofty::probe::Probe; use lofty::probe::Probe;
use lofty::tag::Accessor; use lofty::tag::Accessor;
use lofty::tag::Tag; use lofty::tag::Tag;
use std::fs::{create_dir_all, rename}; use std::fs::{copy, create_dir_all, rename};
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Parser)] #[derive(Parser)]
@@ -31,7 +31,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Commands::Show { file } => { Commands::Show { file } => {
println!("Show file: {:?}", file); println!("Show file: {:?}", file);
let tag = get_metadata(file.to_path_buf()); let tag = get_metadata(file.to_path_buf());
println!("{}", tag.title().as_deref().unwrap_or("None"));
for item in tag.items() { for item in tag.items() {
println!("{:?}", item) 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<dyn std::error::Error>> { fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("./test"); let target_dir = PathBuf::from("./");
let artist = tag.artist(); let artist = tag.artist();
let mut final_target = PathBuf::new(); let album = tag.album();
if let Some(artist_path) = artist { let title = tag.title();
final_target = target_dir.join(artist_path.as_ref()).join(&file_path);
let dir_to_create = target_dir.join(artist_path.as_ref()); let mut target = PathBuf::new();
let _ = create_dir_all(dir_to_create);
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); println!("{:?}", target);
rename(file_path, final_target)?; //rename(file_path, target)?;
copy(file_path, target)?;
Ok(()) Ok(())
} }