feat: handle folder imports
This commit is contained in:
Generated
+7
@@ -141,6 +141,12 @@ dependencies = [
|
|||||||
"miniz_oxide",
|
"miniz_oxide",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "glob"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@@ -200,6 +206,7 @@ name = "music-sort"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"glob",
|
||||||
"lofty",
|
"lofty",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.6.6", features = ["derive"] }
|
clap = { version = "4.6.6", features = ["derive"] }
|
||||||
|
glob = "0.3.4"
|
||||||
lofty = "0.25.1"
|
lofty = "0.25.1"
|
||||||
|
|||||||
+27
-8
@@ -1,10 +1,12 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use glob::glob;
|
||||||
use lofty::file::TaggedFileExt;
|
use lofty::file::TaggedFileExt;
|
||||||
use lofty::prelude::ItemKey;
|
use lofty::prelude::ItemKey;
|
||||||
use lofty::probe::Probe;
|
use lofty::probe::Probe;
|
||||||
use lofty::tag::Accessor;
|
use lofty::tag::Accessor;
|
||||||
use lofty::tag::Tag;
|
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;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -22,16 +24,32 @@ enum Commands {
|
|||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string);
|
||||||
|
|
||||||
match &args.command {
|
match &args.command {
|
||||||
Commands::Import { file } => {
|
Commands::Import { file } => {
|
||||||
println!("Importing file: {:?}", file);
|
println!("Importing file: {:?}", file);
|
||||||
let tag = get_metadata(file.to_path_buf());
|
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());
|
let _ = move_files(file.clone(), tag.clone());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Commands::Show { file } => {
|
Commands::Show { file } => {
|
||||||
println!("Show file: {:?}", file);
|
println!("Show file: {:?}", file);
|
||||||
let tag = get_metadata(file.to_path_buf());
|
let tag = get_metadata(file);
|
||||||
|
|
||||||
for item in tag.items() {
|
for item in tag.items() {
|
||||||
println!("{:?}", item)
|
println!("{:?}", item)
|
||||||
@@ -42,8 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag {
|
fn get_metadata<P: AsRef<OsStr>>(file_path: P) -> lofty::tag::Tag {
|
||||||
let tagged_file = Probe::open(&file_path)
|
let tagged_file = Probe::open(file_path.as_ref())
|
||||||
.expect("ERROR: Bad path provided!")
|
.expect("ERROR: Bad path provided!")
|
||||||
.read()
|
.read()
|
||||||
.expect("ERROR: Failed to read file!");
|
.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>> {
|
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 artist = tag.artist();
|
||||||
let album = tag.album();
|
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
|
let target = target_dir
|
||||||
.join(artist_dir)
|
.join(artist_dir)
|
||||||
.join(album_dir.as_ref())
|
.join(album_dir.as_ref())
|
||||||
.join(&file_path);
|
.join(file_path.file_name().unwrap());
|
||||||
|
|
||||||
println!("{:?}", target);
|
println!("{:?}", target);
|
||||||
|
copy(file_path, &target)?;
|
||||||
//rename(file_path, target)?;
|
//rename(file_path, target)?;
|
||||||
copy(file_path, target)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user