refactor some stuff

This commit is contained in:
Max Bradbury 2021-11-25 23:21:26 +00:00
parent f15d0e0f76
commit 0a4dbbba81
1 changed files with 55 additions and 41 deletions

View File

@ -1,3 +1,4 @@
use std::fs::DirEntry;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
@ -40,52 +41,65 @@ fn move_file(file: PathBuf, mut destination: PathBuf) {
.expect("Couldn't move file"); .expect("Couldn't move file");
} }
fn main() { fn yes() -> bool {
let downloads = std::fs::read_dir(downloads_dir()).unwrap(); let mut answer = String::new();
// todo break this out into a "handle_dir" function, and recursively call it for child dirs io::stdin().read_line(&mut answer)
for file in downloads { .expect("Failed to read input");
let file = file.unwrap();
match file.path().extension().unwrap_or("none".as_ref()).to_str() { answer.to_lowercase().starts_with("y")
Some("gif") => { /*println!("Here's where we would move things to ~/Pictures");*/ } }
Some("jpg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => {
let meta = mp3_metadata::read_from_file(file.path()).unwrap();
for tag in meta.tag { fn handle_mp3(file: DirEntry) {
println!("----------------------"); let meta = mp3_metadata::read_from_file(file.path()).unwrap();
println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim());
println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim()); for tag in meta.tag {
println!("----------------------");
println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim());
let mut answer = String::new(); println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim());
io::stdin().read_line(&mut answer) if yes() {
.expect("Failed to read input"); move_file(
file.path(),
if answer.to_lowercase().starts_with("y") { create_dir_if_not_exists(
move_file( vec![
file.path(), "Music",
create_dir_if_not_exists( strip_null_bytes(&tag.artist).trim(),
vec![ strip_null_bytes(&tag.album).trim()
"Music", ]
strip_null_bytes(&tag.artist).trim(), )
strip_null_bytes(&tag.album).trim() );
] } else {
) println!("skipping...");
); }
} else {
println!("skipping...");
}
}
}
// todo m4a, flac, etc?
_ => { /*println!("Here's where we would do nothing.");*/ }
};
} }
} }
fn handle_dir(path: PathBuf) {
let dir = std::fs::read_dir(path).expect("Couldn't read dir");
for inode in dir {
let inode = inode.unwrap();
if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
match inode.path().extension().unwrap_or("none".as_ref()).to_str() {
Some("gif") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => { handle_mp3(inode) }
// todo m4a, flac, etc?
_ => { /*println!("Here's where we would do nothing.");*/ }
}
}
}
fn main() {
handle_dir(downloads_dir());
}