Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
dcbf0a7ba2
|
+7
-14
@@ -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]) -> 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();
|
||||||
@@ -34,22 +34,15 @@ pub fn edit(file_path: Vec<PathBuf>, extra_files: &[String]) -> std::io::Result<
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tag = match crate::get_metadata(&path) {
|
let tag = crate::get_metadata(&path);
|
||||||
Ok(tag) => tag,
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Failed to get metadata for {:?}: {}", path, err);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
files.push(FileMetadata { path, tag });
|
files.push(FileMetadata { path, tag });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Ok(tag) = crate::get_metadata(file) {
|
let tag = &crate::get_metadata(file);
|
||||||
files.push(FileMetadata {
|
files.push(FileMetadata {
|
||||||
path: file.to_path_buf(),
|
path: file.to_path_buf(),
|
||||||
tag: tag.clone(),
|
tag: tag.clone(),
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+24
-25
@@ -1,10 +1,10 @@
|
|||||||
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;
|
||||||
use std::fs::read_dir;
|
use std::fs::read_dir;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
mod move_files;
|
mod move_files;
|
||||||
use move_files::move_files;
|
use move_files::move_files;
|
||||||
@@ -27,8 +27,6 @@ enum Commands {
|
|||||||
copy: bool,
|
copy: bool,
|
||||||
#[arg(short, long, default_value_t = false)]
|
#[arg(short, long, default_value_t = false)]
|
||||||
new: bool,
|
new: bool,
|
||||||
#[arg(short, long, default_value_t = false)]
|
|
||||||
update_mpd: bool,
|
|
||||||
},
|
},
|
||||||
Show {
|
Show {
|
||||||
files: Vec<PathBuf>,
|
files: Vec<PathBuf>,
|
||||||
@@ -39,19 +37,30 @@ 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 = [
|
let extra_files = [
|
||||||
"jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt", "docx", "gif",
|
"jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt", "docx",
|
||||||
]
|
]
|
||||||
.map(str::to_string);
|
.map(str::to_string);
|
||||||
|
|
||||||
match &args.command {
|
match &args.command {
|
||||||
Commands::Import {
|
Commands::Import { files, copy, new } => {
|
||||||
files,
|
|
||||||
copy,
|
|
||||||
new,
|
|
||||||
update_mpd,
|
|
||||||
} => {
|
|
||||||
for file in files {
|
for file in files {
|
||||||
if file.is_dir() {
|
if file.is_dir() {
|
||||||
println!("Importing folder: {:?}", file);
|
println!("Importing folder: {:?}", file);
|
||||||
@@ -68,15 +77,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
let tag = get_metadata(&path);
|
let tag = get_metadata(&path);
|
||||||
|
|
||||||
if let Err(e) = move_files(
|
if let Err(e) =
|
||||||
path.clone(),
|
move_files(path.clone(), tag?, extra_files.clone(), *copy, true, *new)
|
||||||
tag?,
|
{
|
||||||
&extra_files,
|
|
||||||
*copy,
|
|
||||||
file.to_path_buf(),
|
|
||||||
true,
|
|
||||||
*new,
|
|
||||||
) {
|
|
||||||
eprintln!("Failed to import {:?}: {}", path, e);
|
eprintln!("Failed to import {:?}: {}", path, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,17 +89,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
move_files(
|
move_files(
|
||||||
file.clone(),
|
file.clone(),
|
||||||
tag?.clone(),
|
tag?.clone(),
|
||||||
&extra_files,
|
extra_files.clone(),
|
||||||
*copy,
|
*copy,
|
||||||
file.to_path_buf(),
|
|
||||||
false,
|
false,
|
||||||
*new,
|
*new,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if *update_mpd {
|
|
||||||
Command::new("mpc").arg("update").spawn()?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Show { files } => {
|
Commands::Show { files } => {
|
||||||
@@ -111,7 +110,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Commands::Edit { files } => {
|
Commands::Edit { files } => {
|
||||||
edit(files.to_vec(), &extra_files)?;
|
edit(files.to_vec(), extra_files)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-24
@@ -5,16 +5,15 @@ use lofty::tag::Accessor;
|
|||||||
use lofty::tag::Tag;
|
use lofty::tag::Tag;
|
||||||
use std::env::var;
|
use std::env::var;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::fs::{File, copy, create_dir, create_dir_all, remove_dir, rename};
|
use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::PathBuf;
|
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],
|
extra_files: [String; 9],
|
||||||
should_copy: bool,
|
should_copy: bool,
|
||||||
folder_path: PathBuf,
|
|
||||||
has_parent: bool,
|
has_parent: bool,
|
||||||
add_new_playlist: bool,
|
add_new_playlist: bool,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@@ -61,7 +60,11 @@ pub fn move_files(
|
|||||||
rename(&file_path, target.join(&file_name))?;
|
rename(&file_path, target.join(&file_name))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ext in extra_files {
|
if add_new_playlist {
|
||||||
|
new_playlist(target.join(file_name))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ext in &extra_files {
|
||||||
let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
|
let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
|
||||||
|
|
||||||
let paths: Vec<_> = glob(pattern.to_str().unwrap())?.collect::<Result<_, _>>()?;
|
let paths: Vec<_> = glob(pattern.to_str().unwrap())?.collect::<Result<_, _>>()?;
|
||||||
@@ -86,17 +89,7 @@ pub fn move_files(
|
|||||||
remove_dir(file_path.parent().unwrap())?;
|
remove_dir(file_path.parent().unwrap())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if add_new_playlist {
|
write_log(file_path)?;
|
||||||
new_playlist(target.join(file_name))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set"));
|
|
||||||
let data_dir = home_dir.join(".local").join("share").join("music-sort");
|
|
||||||
let log_file_path = data_dir.join("verbose.log");
|
|
||||||
write_log(file_path.clone(), log_file_path, data_dir.clone())?;
|
|
||||||
|
|
||||||
let log_file_path = data_dir.join("music-sort.log");
|
|
||||||
write_log(folder_path, log_file_path, data_dir)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -116,22 +109,24 @@ fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
|
|||||||
.append(true)
|
.append(true)
|
||||||
.open(new_playlist_path)
|
.open(new_playlist_path)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
println!("{:?}", path);
|
||||||
writeln!(new_playlist, "{}", path.display())
|
writeln!(new_playlist, "{}", path.display())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_log(
|
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
file_path: PathBuf,
|
let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set"));
|
||||||
log_path: PathBuf,
|
let data_dir = home_dir.join(".local").join("share").join("music-sort");
|
||||||
data_dir: PathBuf,
|
let log_file_path = data_dir.join("music-sort.log");
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
if !data_dir.exists() {
|
if !data_dir.exists() {
|
||||||
create_dir(&data_dir)?;
|
create_dir(&data_dir)?;
|
||||||
}
|
}
|
||||||
if !log_path.exists() {
|
|
||||||
File::create(&log_path)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut log_file = OpenOptions::new().append(true).open(log_path).unwrap();
|
let mut log_file = OpenOptions::new()
|
||||||
|
.append(true)
|
||||||
|
.create(true)
|
||||||
|
.open(log_file_path)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
if let Err(e) = writeln!(
|
if let Err(e) = writeln!(
|
||||||
log_file,
|
log_file,
|
||||||
|
|||||||
Reference in New Issue
Block a user