feat: handle folder imports

This commit is contained in:
2026-08-23 03:08:03 +02:00
parent 8327fd7cab
commit 6a91d20a76
3 changed files with 36 additions and 9 deletions
Generated
+7
View File
@@ -141,6 +141,12 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "glob"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b"
[[package]]
name = "heck"
version = "0.5.0"
@@ -200,6 +206,7 @@ name = "music-sort"
version = "0.1.0"
dependencies = [
"clap",
"glob",
"lofty",
]
+1
View File
@@ -5,4 +5,5 @@ edition = "2024"
[dependencies]
clap = { version = "4.6.6", features = ["derive"] }
glob = "0.3.4"
lofty = "0.25.1"
+28 -9
View File
@@ -1,10 +1,12 @@
use clap::{Parser, Subcommand};
use glob::glob;
use lofty::file::TaggedFileExt;
use lofty::prelude::ItemKey;
use lofty::probe::Probe;
use lofty::tag::Accessor;
use lofty::tag::Tag;
use std::fs::{copy, create_dir_all, rename};
use std::ffi::OsStr;
use std::fs::{copy, create_dir_all, read_dir, rename};
use std::path::PathBuf;
#[derive(Parser)]
@@ -22,16 +24,32 @@ 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"].map(str::to_string);
match &args.command {
Commands::Import { file } => {
println!("Importing file: {:?}", file);
let tag = get_metadata(file.to_path_buf());
let _ = move_files(file.clone(), tag.clone());
if file.is_dir() {
for music_file in read_dir(file)? {
let path = music_file?.path();
if path
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| extra_files.iter().any(|x| x == ext))
{
continue;
}
let tag = get_metadata(&path);
let _ = move_files(path, tag.clone());
}
} else {
let tag = get_metadata(file);
let _ = move_files(file.clone(), tag.clone());
}
}
Commands::Show { file } => {
println!("Show file: {:?}", file);
let tag = get_metadata(file.to_path_buf());
let tag = get_metadata(file);
for item in tag.items() {
println!("{:?}", item)
@@ -42,8 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag {
let tagged_file = Probe::open(&file_path)
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!");
@@ -56,7 +74,8 @@ fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag {
}
fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("./");
println!("{:?}", file_path);
let target_dir = PathBuf::from("/home/user/Desktop/coding/keks-sort");
let artist = tag.artist();
let album = tag.album();
@@ -80,10 +99,10 @@ fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Er
let target = target_dir
.join(artist_dir)
.join(album_dir.as_ref())
.join(&file_path);
.join(file_path.file_name().unwrap());
println!("{:?}", target);
copy(file_path, &target)?;
//rename(file_path, target)?;
copy(file_path, target)?;
Ok(())
}