Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
deeddb525a
|
|||
|
11e56e15f0
|
|||
|
777e7055f1
|
|||
|
d0014711c8
|
+2
-29
@@ -5,10 +5,9 @@ use lofty::tag::{ItemKey, ItemValue};
|
||||
use lofty::tag::{Tag, TagExt};
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::env::var;
|
||||
use std::io::Write;
|
||||
use std::path:read_dir, fs::File
|
||||
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]) -> std::io::Result<()> {
|
||||
@@ -30,7 +29,7 @@ pub fn edit(file_path: Vec<PathBuf>, extra_files: &[String]) -> std::io::Result<
|
||||
if path
|
||||
.extension()
|
||||
.and_then(|ext| ext.to_str())
|
||||
.is_some_and(|ext| extra_files.iter().any(|x| *x == ext))
|
||||
.is_some_and(|ext| extra_files.iter().any(|x| x == ext))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -42,7 +41,6 @@ pub fn edit(file_path: Vec<PathBuf>, extra_files: &[String]) -> std::io::Result<
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
files.push(FileMetadata { path, tag });
|
||||
}
|
||||
} else {
|
||||
@@ -55,31 +53,6 @@ pub fn edit(file_path: Vec<PathBuf>, extra_files: &[String]) -> std::io::Result<
|
||||
}
|
||||
}
|
||||
|
||||
let mut item_count = HashSet::new();
|
||||
|
||||
writeln!(tmp_file.try_clone()?, "------------ all ------------",)?;
|
||||
|
||||
for file in &files {
|
||||
let mut items: Vec<_> = file.tag.items().collect();
|
||||
items.sort_by_key(|item| item.key().map_key(VorbisComments).unwrap());
|
||||
|
||||
for item in items {
|
||||
let key = item.key().map_key(VorbisComments).unwrap();
|
||||
let value = item.value().text().unwrap();
|
||||
|
||||
if item_count.insert((key, value)) {
|
||||
writeln!(tmp_file, "{} = {}", key, value)?;
|
||||
}
|
||||
//writeln!(
|
||||
// tmp_file,
|
||||
// "{}",
|
||||
// item.key().map_key(VorbisComments).unwrap(),
|
||||
// //item.value().text().unwrap()
|
||||
//)?;
|
||||
}
|
||||
}
|
||||
writeln!(tmp_file.try_clone()?)?; // writes empty line
|
||||
|
||||
for file in &files {
|
||||
let mut items: Vec<_> = file.tag.items().collect();
|
||||
items.sort_by_key(|item| item.key().map_key(VorbisComments).unwrap());
|
||||
|
||||
+25
-7
@@ -4,6 +4,7 @@ use lofty::probe::Probe;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::read_dir;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
mod move_files;
|
||||
use move_files::move_files;
|
||||
@@ -26,6 +27,8 @@ enum Commands {
|
||||
copy: bool,
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
new: bool,
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
update_mpd: bool,
|
||||
},
|
||||
Show {
|
||||
files: Vec<PathBuf>,
|
||||
@@ -38,12 +41,17 @@ enum Commands {
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Args::parse();
|
||||
let extra_files = [
|
||||
"jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt", "docx",
|
||||
"jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt", "docx", "gif",
|
||||
]
|
||||
.map(str::to_string);
|
||||
|
||||
match &args.command {
|
||||
Commands::Import { files, copy, new } => {
|
||||
Commands::Import {
|
||||
files,
|
||||
copy,
|
||||
new,
|
||||
update_mpd,
|
||||
} => {
|
||||
for file in files {
|
||||
if file.is_dir() {
|
||||
println!("Importing folder: {:?}", file);
|
||||
@@ -60,9 +68,15 @@ 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, *new)
|
||||
{
|
||||
if let Err(e) = move_files(
|
||||
path.clone(),
|
||||
tag?,
|
||||
&extra_files,
|
||||
*copy,
|
||||
file.to_path_buf(),
|
||||
true,
|
||||
*new,
|
||||
) {
|
||||
eprintln!("Failed to import {:?}: {}", path, e);
|
||||
}
|
||||
}
|
||||
@@ -72,13 +86,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
move_files(
|
||||
file.clone(),
|
||||
tag?.clone(),
|
||||
extra_files.clone(),
|
||||
&extra_files,
|
||||
*copy,
|
||||
file.to_path_buf(),
|
||||
false,
|
||||
*new,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
if *update_mpd {
|
||||
Command::new("mpc").arg("update").spawn()?;
|
||||
}
|
||||
}
|
||||
|
||||
Commands::Show { files } => {
|
||||
@@ -93,7 +111,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
Commands::Edit { files } => {
|
||||
edit(files.to_vec(), extra_files)?;
|
||||
edit(files.to_vec(), &extra_files)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-19
@@ -5,15 +5,16 @@ use lofty::tag::Accessor;
|
||||
use lofty::tag::Tag;
|
||||
use std::env::var;
|
||||
use std::fs::OpenOptions;
|
||||
use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename};
|
||||
use std::fs::{File, copy, create_dir, create_dir_all, remove_dir, rename};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn move_files(
|
||||
file_path: PathBuf,
|
||||
tag: Tag,
|
||||
extra_files: [String; 9],
|
||||
extra_files: &[String],
|
||||
should_copy: bool,
|
||||
folder_path: PathBuf,
|
||||
has_parent: bool,
|
||||
add_new_playlist: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -60,11 +61,7 @@ pub fn move_files(
|
||||
rename(&file_path, target.join(&file_name))?;
|
||||
}
|
||||
|
||||
if add_new_playlist {
|
||||
new_playlist(target.join(file_name))?;
|
||||
}
|
||||
|
||||
for ext in &extra_files {
|
||||
for ext in extra_files {
|
||||
let pattern = file_path.parent().unwrap().join(format!("*.{}", ext));
|
||||
|
||||
let paths: Vec<_> = glob(pattern.to_str().unwrap())?.collect::<Result<_, _>>()?;
|
||||
@@ -89,7 +86,17 @@ pub fn move_files(
|
||||
remove_dir(file_path.parent().unwrap())?;
|
||||
}
|
||||
|
||||
write_log(file_path)?;
|
||||
if add_new_playlist {
|
||||
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(())
|
||||
}
|
||||
@@ -109,24 +116,22 @@ fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
|
||||
.append(true)
|
||||
.open(new_playlist_path)
|
||||
.unwrap();
|
||||
println!("{:?}", path);
|
||||
writeln!(new_playlist, "{}", path.display())
|
||||
}
|
||||
|
||||
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
||||
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("music-sort.log");
|
||||
|
||||
fn write_log(
|
||||
file_path: PathBuf,
|
||||
log_path: PathBuf,
|
||||
data_dir: PathBuf,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if !data_dir.exists() {
|
||||
create_dir(&data_dir)?;
|
||||
}
|
||||
if !log_path.exists() {
|
||||
File::create(&log_path)?;
|
||||
}
|
||||
|
||||
let mut log_file = OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(log_file_path)
|
||||
.unwrap();
|
||||
let mut log_file = OpenOptions::new().append(true).open(log_path).unwrap();
|
||||
|
||||
if let Err(e) = writeln!(
|
||||
log_file,
|
||||
|
||||
Reference in New Issue
Block a user