feat: copy extra files

This commit is contained in:
2026-08-23 15:10:03 +02:00
parent 6a91d20a76
commit 9b00f29094
+36 -12
View File
@@ -28,8 +28,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match &args.command { match &args.command {
Commands::Import { file } => { Commands::Import { file } => {
println!("Importing file: {:?}", file);
if file.is_dir() { if file.is_dir() {
println!("Importing folder: {:?}", file);
for music_file in read_dir(file)? { for music_file in read_dir(file)? {
let path = music_file?.path(); let path = music_file?.path();
if path if path
@@ -40,11 +40,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
continue; continue;
} }
let tag = get_metadata(&path); let tag = get_metadata(&path);
let _ = move_files(path, tag.clone()); let _ = move_files(path, tag.clone(), extra_files.clone());
} }
} else { } else {
println!("Importing file: {:?}", file);
let tag = get_metadata(file); let tag = get_metadata(file);
let _ = move_files(file.clone(), tag.clone()); let _ = move_files(file.clone(), tag.clone(), extra_files);
} }
} }
Commands::Show { file } => { Commands::Show { file } => {
@@ -73,8 +74,11 @@ fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
tag.clone() tag.clone()
} }
fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> { fn move_files(
println!("{:?}", file_path); file_path: PathBuf,
tag: Tag,
extra_files: [String; 8],
) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("/home/user/Desktop/coding/keks-sort"); let target_dir = PathBuf::from("/home/user/Desktop/coding/keks-sort");
let artist = tag.artist(); let artist = tag.artist();
@@ -96,13 +100,33 @@ fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Er
let dir_to_create = target_dir.join(artist_dir).join(album_dir.as_ref()); let dir_to_create = target_dir.join(artist_dir).join(album_dir.as_ref());
let _ = create_dir_all(dir_to_create); let _ = create_dir_all(dir_to_create);
let target = target_dir let target = target_dir.join(artist_dir).join(album_dir.as_ref());
.join(artist_dir)
.join(album_dir.as_ref())
.join(file_path.file_name().unwrap());
println!("{:?}", target); //copy(&file_path, target.join(file_path.file_name().unwrap()))?;
copy(file_path, &target)?; let should_copy = true;
//rename(file_path, target)?; if should_copy {
copy(&file_path, target.join(file_path.file_name().unwrap()))?;
} else {
rename(&file_path, target.join(file_path.file_name().unwrap()))?;
}
for ext in &extra_files {
let file = file_path.parent().unwrap().join(format!("*.{}", ext));
for slice in glob(file.to_str().unwrap())? {
let path = slice?;
let result = if should_copy {
copy(&path, target.join(path.file_name().unwrap())).map(|_| ())
} else {
rename(&path, target.join(path.file_name().unwrap()))
};
if let Err(e) = result {
println!("Copy failed: {e}");
}
}
}
println!("Imported file: {:?}", file_path.file_name().unwrap());
Ok(()) Ok(())
} }