fix: collect all extra files before renaming to fix failure of renaming
This commit is contained in:
+1
-1
@@ -10,7 +10,7 @@ use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
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 mut tmp_file = File::create(&tmp_file_path)?;
|
||||
let editor = var("EDITOR").unwrap();
|
||||
|
||||
+32
-27
@@ -1,5 +1,4 @@
|
||||
use clap::{Parser, Subcommand};
|
||||
//use ini::Ini;
|
||||
use lofty::file::TaggedFileExt;
|
||||
use lofty::probe::Probe;
|
||||
use std::ffi::OsStr;
|
||||
@@ -35,24 +34,11 @@ enum Commands {
|
||||
}
|
||||
|
||||
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 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 {
|
||||
Commands::Import { files, copy } => {
|
||||
@@ -73,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let tag = get_metadata(&path);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -81,7 +67,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
} else {
|
||||
println!("Importing file: {:?}", 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);
|
||||
let tag = get_metadata(file);
|
||||
|
||||
for item in tag.items() {
|
||||
for item in tag?.items() {
|
||||
println!("{:?}", item)
|
||||
}
|
||||
}
|
||||
@@ -105,16 +97,29 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
|
||||
let tagged_file = Probe::open(file_path.as_ref())
|
||||
.expect("ERROR: Bad path provided!")
|
||||
.read()
|
||||
.expect("ERROR: Failed to read file!");
|
||||
pub fn get_metadata<P: AsRef<OsStr>>(
|
||||
file_path: P,
|
||||
) -> Result<lofty::tag::Tag, Box<dyn std::error::Error>> {
|
||||
let tagged_file = match Probe::open(file_path.as_ref()) {
|
||||
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() {
|
||||
Some(primary_tag) => primary_tag,
|
||||
None => tagged_file.first_tag().expect("ERROR: No tags found!"),
|
||||
};
|
||||
|
||||
tag.clone()
|
||||
Ok(tag.clone())
|
||||
}
|
||||
|
||||
+9
-7
@@ -12,7 +12,7 @@ use std::path::PathBuf;
|
||||
pub fn move_files(
|
||||
file_path: PathBuf,
|
||||
tag: Tag,
|
||||
extra_files: [String; 8],
|
||||
extra_files: [String; 9],
|
||||
should_copy: bool,
|
||||
has_parent: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -60,19 +60,21 @@ pub fn move_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 path = slice?;
|
||||
let paths: Vec<_> = glob(pattern.to_str().unwrap())?.collect::<Result<_, _>>()?;
|
||||
|
||||
for path in paths {
|
||||
let destination = target.join(path.file_name().unwrap());
|
||||
|
||||
let result = if should_copy {
|
||||
copy(&path, target.join(path.file_name().unwrap())).map(|_| ())
|
||||
copy(&path, &destination).map(|_| ())
|
||||
} else {
|
||||
rename(&path, target.join(path.file_name().unwrap()))
|
||||
rename(&path, &destination)
|
||||
};
|
||||
|
||||
if let Err(e) = result {
|
||||
println!("Copy failed: {e}");
|
||||
println!("Copy failed for {:?}: {e}", path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user