feat: copy extra files
This commit is contained in:
+36
-12
@@ -28,8 +28,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
match &args.command {
|
||||
Commands::Import { file } => {
|
||||
println!("Importing file: {:?}", file);
|
||||
if file.is_dir() {
|
||||
println!("Importing folder: {:?}", file);
|
||||
for music_file in read_dir(file)? {
|
||||
let path = music_file?.path();
|
||||
if path
|
||||
@@ -40,11 +40,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
continue;
|
||||
}
|
||||
let tag = get_metadata(&path);
|
||||
let _ = move_files(path, tag.clone());
|
||||
let _ = move_files(path, tag.clone(), extra_files.clone());
|
||||
}
|
||||
} else {
|
||||
println!("Importing file: {:?}", 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 } => {
|
||||
@@ -73,8 +74,11 @@ fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
|
||||
tag.clone()
|
||||
}
|
||||
|
||||
fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("{:?}", file_path);
|
||||
fn move_files(
|
||||
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 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 _ = create_dir_all(dir_to_create);
|
||||
let target = target_dir
|
||||
.join(artist_dir)
|
||||
.join(album_dir.as_ref())
|
||||
.join(file_path.file_name().unwrap());
|
||||
let target = target_dir.join(artist_dir).join(album_dir.as_ref());
|
||||
|
||||
println!("{:?}", target);
|
||||
copy(file_path, &target)?;
|
||||
//rename(file_path, target)?;
|
||||
//copy(&file_path, target.join(file_path.file_name().unwrap()))?;
|
||||
let should_copy = true;
|
||||
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user