refactor: minor refactoring
This commit is contained in:
parent
a4693e3da9
commit
f9ea3ae1c8
1 changed files with 49 additions and 37 deletions
86
src/main.rs
86
src/main.rs
|
@ -65,49 +65,61 @@ fn rename_files(directory: &str, base: &str, args: &Arguments) -> Result<i32> {
|
||||||
if let Some(sub_dir) = path.to_str() {
|
if let Some(sub_dir) = path.to_str() {
|
||||||
count += rename_files(sub_dir, base, args).context("Failed to rename files")?;
|
count += rename_files(sub_dir, base, args).context("Failed to rename files")?;
|
||||||
}
|
}
|
||||||
}
|
} else if path.is_file() {
|
||||||
if !path.is_file() {
|
let Some((title, artist)) = find_tag_info(&path) else {
|
||||||
continue;
|
continue;
|
||||||
}
|
};
|
||||||
let Ok(file) = taglib::File::new(&path) else {
|
let (bucket, _) = artist.split_at(1);
|
||||||
continue;
|
let album = parse_album(&title);
|
||||||
};
|
count += 1;
|
||||||
let Ok(tag) = file.tag() else {
|
let new_name = if album.is_empty() {
|
||||||
continue;
|
format!("{bucket}/{artist}/{title}/{title}.m4b")
|
||||||
};
|
} else {
|
||||||
let (Some(title), Some(artist)) = (tag.title(), tag.artist()) else {
|
build_series_name(bucket, &artist, &album, &title)
|
||||||
continue;
|
};
|
||||||
};
|
println!("==============================");
|
||||||
let (bucket, _) = artist.split_at(1);
|
println!("- artist: {artist}");
|
||||||
let album = parse_album(&title);
|
println!("- album : {album}");
|
||||||
count += 1;
|
println!("- title : {title}");
|
||||||
let new_name = if album.is_empty() {
|
println!("> {new_name}");
|
||||||
format!("{bucket}/{artist}/{title}/{title}.m4b")
|
if args.not_dry_run() {
|
||||||
} else {
|
rename_file(base, new_name, path)?;
|
||||||
build_series_name(bucket, &artist, &album, &title)
|
|
||||||
};
|
|
||||||
println!("==============================");
|
|
||||||
println!("- artist: {artist}");
|
|
||||||
println!("- album : {album}");
|
|
||||||
println!("- title : {title}");
|
|
||||||
println!("> {new_name}");
|
|
||||||
if args.not_dry_run() {
|
|
||||||
let target_path = Path::new(&base).join(new_name);
|
|
||||||
if !target_path.exists() {
|
|
||||||
let dir = target_path
|
|
||||||
.parent()
|
|
||||||
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
|
||||||
create_dir_all(dir)
|
|
||||||
.with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rename(&path, &target_path)
|
|
||||||
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(count)
|
Ok(count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_tag_info(path: &std::path::PathBuf) -> Option<(String, String)> {
|
||||||
|
let Ok(file) = taglib::File::new(path) else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let Ok(tag) = file.tag() else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let (Some(title), Some(artist)) = (tag.title(), tag.artist()) else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
Some((title, artist))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rename_file(
|
||||||
|
base: &str,
|
||||||
|
new_name: String,
|
||||||
|
path: std::path::PathBuf,
|
||||||
|
) -> Result<(), anyhow::Error> {
|
||||||
|
let target_path = Path::new(&base).join(new_name);
|
||||||
|
if !target_path.exists() {
|
||||||
|
let dir = target_path
|
||||||
|
.parent()
|
||||||
|
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
||||||
|
create_dir_all(dir).with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
||||||
|
}
|
||||||
|
rename(&path, &target_path)
|
||||||
|
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn build_series_name(bucket: &str, artist: &str, album: &str, title: &str) -> String {
|
fn build_series_name(bucket: &str, artist: &str, album: &str, title: &str) -> String {
|
||||||
let index = parse_index(title);
|
let index = parse_index(title);
|
||||||
let title = parse_title(title);
|
let title = parse_title(title);
|
||||||
|
|
Loading…
Reference in a new issue