fix: collect all extra files before renaming to fix failure of renaming

This commit is contained in:
2026-10-07 19:15:17 +02:00
parent 035e4ecc9b
commit 5e3bc38a11
3 changed files with 42 additions and 35 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::{fs::File, fs::read_dir, io::Read, process::Command}; use std::{fs::File, fs::read_dir, io::Read, process::Command};
pub fn edit(file_path: Vec<PathBuf>, extra_files: [String; 8]) -> std::io::Result<()> { pub fn edit(file_path: Vec<PathBuf>, extra_files: [String; 9]) -> std::io::Result<()> {
let tmp_file_path = PathBuf::from("/tmp/keks-sort-metadata"); let tmp_file_path = PathBuf::from("/tmp/keks-sort-metadata");
let mut tmp_file = File::create(&tmp_file_path)?; let mut tmp_file = File::create(&tmp_file_path)?;
let editor = var("EDITOR").unwrap(); let editor = var("EDITOR").unwrap();
+32 -27
View File
@@ -1,5 +1,4 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
//use ini::Ini;
use lofty::file::TaggedFileExt; use lofty::file::TaggedFileExt;
use lofty::probe::Probe; use lofty::probe::Probe;
use std::ffi::OsStr; use std::ffi::OsStr;
@@ -35,24 +34,11 @@ enum Commands {
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
//let mut conf = Ini::new();
//conf.with_section(Some("General"))
// .set("target_directory", "");
////conf.write_to_file("conf.ini").unwrap();
//
//let conf = Ini::load_from_file("conf.ini").unwrap();
//
//let section = conf.section(Some("General")).unwrap();
//println!("target_dir={:?}", section.get("target_directory"));
//
//for (_sec, prop) in &conf {
// for (key, value) in prop.iter() {
// println!("{:?}:{:?}", key, value);
// }
//}
//
let args = Args::parse(); let args = Args::parse();
let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string); let extra_files = [
"jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt", "docx",
]
.map(str::to_string);
match &args.command { match &args.command {
Commands::Import { files, copy } => { Commands::Import { files, copy } => {
@@ -73,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let tag = get_metadata(&path); let tag = get_metadata(&path);
if let Err(e) = if let Err(e) =
move_files(path.clone(), tag, extra_files.clone(), *copy, true) move_files(path.clone(), tag?, extra_files.clone(), *copy, true)
{ {
eprintln!("Failed to import {:?}: {}", path, e); eprintln!("Failed to import {:?}: {}", path, e);
} }
@@ -81,7 +67,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} else { } else {
println!("Importing file: {:?}", file); println!("Importing file: {:?}", file);
let tag = get_metadata(file); let tag = get_metadata(file);
move_files(file.clone(), tag.clone(), extra_files.clone(), *copy, false)?; move_files(
file.clone(),
tag?.clone(),
extra_files.clone(),
*copy,
false,
)?;
} }
} }
} }
@@ -91,7 +83,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Show file: {:?}", file); println!("Show file: {:?}", file);
let tag = get_metadata(file); let tag = get_metadata(file);
for item in tag.items() { for item in tag?.items() {
println!("{:?}", item) println!("{:?}", item)
} }
} }
@@ -105,16 +97,29 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
pub fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag { pub fn get_metadata<P: AsRef<OsStr>>(
let tagged_file = Probe::open(file_path.as_ref()) file_path: P,
.expect("ERROR: Bad path provided!") ) -> Result<lofty::tag::Tag, Box<dyn std::error::Error>> {
.read() let tagged_file = match Probe::open(file_path.as_ref()) {
.expect("ERROR: Failed to read file!"); Ok(file) => file,
Err(err) => {
eprintln!("ERROR: Bad path provided: {err}");
return Err(err.into());
}
};
let tagged_file = match tagged_file.read() {
Ok(file) => file,
Err(err) => {
eprintln!("ERROR: Failed to read file: {err}");
return Err(err.into());
}
};
let tag = match tagged_file.primary_tag() { let tag = match tagged_file.primary_tag() {
Some(primary_tag) => primary_tag, Some(primary_tag) => primary_tag,
None => tagged_file.first_tag().expect("ERROR: No tags found!"), None => tagged_file.first_tag().expect("ERROR: No tags found!"),
}; };
tag.clone() Ok(tag.clone())
} }
+9 -7
View File
@@ -12,7 +12,7 @@ use std::path::PathBuf;
pub fn move_files( pub fn move_files(
file_path: PathBuf, file_path: PathBuf,
tag: Tag, tag: Tag,
extra_files: [String; 8], extra_files: [String; 9],
should_copy: bool, should_copy: bool,
has_parent: bool, has_parent: bool,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
@@ -60,19 +60,21 @@ pub fn move_files(
} }
for ext in &extra_files { for ext in &extra_files {
let file = file_path.parent().unwrap().join(format!("*.{}", ext)); let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
for slice in glob(file.to_str().unwrap())? { let paths: Vec<_> = glob(pattern.to_str().unwrap())?.collect::<Result<_, _>>()?;
let path = slice?;
for path in paths {
let destination = target.join(path.file_name().unwrap());
let result = if should_copy { let result = if should_copy {
copy(&path, target.join(path.file_name().unwrap())).map(|_| ()) copy(&path, &destination).map(|_| ())
} else { } else {
rename(&path, target.join(path.file_name().unwrap())) rename(&path, &destination)
}; };
if let Err(e) = result { if let Err(e) = result {
println!("Copy failed: {e}"); println!("Copy failed for {:?}: {e}", path);
} }
} }
} }