refactor: move move_files into separate module
This commit is contained in:
+4
-62
@@ -1,14 +1,13 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use glob::glob;
|
|
||||||
use lofty::file::TaggedFileExt;
|
use lofty::file::TaggedFileExt;
|
||||||
use lofty::prelude::ItemKey;
|
|
||||||
use lofty::probe::Probe;
|
use lofty::probe::Probe;
|
||||||
use lofty::tag::Accessor;
|
|
||||||
use lofty::tag::Tag;
|
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs::{copy, create_dir_all, read_dir, rename};
|
use std::fs::read_dir;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
mod move_files;
|
||||||
|
use move_files::move_files;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
@@ -73,60 +72,3 @@ fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
|
|||||||
};
|
};
|
||||||
tag.clone()
|
tag.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
let album = tag.album();
|
|
||||||
let title = tag.title();
|
|
||||||
let album_artist_path = tag.get_string(ItemKey::AlbumArtist).unwrap_or("None");
|
|
||||||
|
|
||||||
let artist_dir = if !album_artist_path.is_empty() {
|
|
||||||
Some(album_artist_path)
|
|
||||||
} else {
|
|
||||||
artist.as_deref()
|
|
||||||
};
|
|
||||||
|
|
||||||
let album_dir = album.or(title);
|
|
||||||
|
|
||||||
let (Some(artist_dir), Some(album_dir)) = (artist_dir, album_dir) else {
|
|
||||||
return Ok(());
|
|
||||||
};
|
|
||||||
|
|
||||||
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());
|
|
||||||
|
|
||||||
//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(())
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
use glob::glob;
|
||||||
|
use lofty::prelude::ItemKey;
|
||||||
|
use lofty::tag::Accessor;
|
||||||
|
use lofty::tag::Tag;
|
||||||
|
use std::fs::{copy, create_dir_all, rename};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub 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();
|
||||||
|
let album = tag.album();
|
||||||
|
let title = tag.title();
|
||||||
|
let album_artist_path = tag.get_string(ItemKey::AlbumArtist).unwrap_or("None");
|
||||||
|
|
||||||
|
let artist_dir = if !album_artist_path.is_empty() {
|
||||||
|
Some(album_artist_path)
|
||||||
|
} else {
|
||||||
|
artist.as_deref()
|
||||||
|
};
|
||||||
|
|
||||||
|
let album_dir = album.or(title);
|
||||||
|
|
||||||
|
let (Some(artist_dir), Some(album_dir)) = (artist_dir, album_dir) else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
|
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