Compare commits
2 commits
95fe74e395
...
bffc4a6ab8
Author | SHA1 | Date | |
---|---|---|---|
bffc4a6ab8 | |||
3f49891dc8 |
2 changed files with 48 additions and 14 deletions
26
src/lib.rs
26
src/lib.rs
|
@ -8,15 +8,15 @@ use std::{
|
|||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
pub pictures_dir: Option<PathBuf>,
|
||||
pub videos_dir: Option<PathBuf>,
|
||||
pub video_dir: Option<PathBuf>,
|
||||
pub music_dir: Option<PathBuf>,
|
||||
pub books_dir: Option<PathBuf>,
|
||||
pub sources: HashMap<String, PathBuf>,
|
||||
}
|
||||
|
||||
pub const PICTURE_EXTENTIONS: [&str; 17] = [
|
||||
pub const PICTURE_EXTENTIONS: [&str; 18] = [
|
||||
"jpg", "jpeg", "png", "gif", "webp", "jfif", "bmp", "apng", "avif", "tif", "tga", "psd", "eps",
|
||||
"ai", "indd", "raw", "ico",
|
||||
"ai", "indd", "raw", "ico", "svg",
|
||||
];
|
||||
pub const SOUND_EXTENTIONS: [&str; 10] = [
|
||||
"mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "au", "opus",
|
||||
|
@ -33,7 +33,7 @@ impl Config {
|
|||
let mut sources: HashMap<String, PathBuf> = HashMap::new();
|
||||
|
||||
let mut pictures_dir = None;
|
||||
let mut videos_dir = None;
|
||||
let mut video_dir = None;
|
||||
let mut music_dir = None;
|
||||
let mut books_dir = None;
|
||||
|
||||
|
@ -61,10 +61,10 @@ impl Config {
|
|||
sources.insert(key_trimmed.into(), value_trimmed.into());
|
||||
}
|
||||
"destinations" => match key_trimmed {
|
||||
"music_dir" => music_dir = Some(PathBuf::from(value_trimmed)),
|
||||
"video_dir" => videos_dir = Some(PathBuf::from(value_trimmed)),
|
||||
"books_dir" => books_dir = Some(PathBuf::from(value_trimmed)),
|
||||
"pictures_dir" => pictures_dir = Some(PathBuf::from(value_trimmed)),
|
||||
"music_dir" => set_directory(&mut music_dir, value_trimmed),
|
||||
"video_dir" => set_directory(&mut video_dir, value_trimmed),
|
||||
"books_dir" => set_directory(&mut books_dir, value_trimmed),
|
||||
"pictures_dir" => set_directory(&mut pictures_dir, value_trimmed),
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
|
@ -74,7 +74,7 @@ impl Config {
|
|||
|
||||
Ok(Self {
|
||||
pictures_dir,
|
||||
videos_dir,
|
||||
video_dir,
|
||||
music_dir,
|
||||
books_dir,
|
||||
sources,
|
||||
|
@ -85,7 +85,7 @@ impl Config {
|
|||
let config_str = r#"
|
||||
[destinations]
|
||||
pictures_dir = ""
|
||||
videos_dir = ""
|
||||
video_dir = ""
|
||||
music_dir = ""
|
||||
books_dir = ""
|
||||
|
||||
|
@ -98,3 +98,9 @@ books_dir = ""
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn set_directory(dir: &mut Option<PathBuf>, value: &str) {
|
||||
if !value.is_empty() {
|
||||
*dir = Some(PathBuf::from(value));
|
||||
}
|
||||
}
|
||||
|
|
36
src/main.rs
36
src/main.rs
|
@ -49,16 +49,20 @@ fn main() {
|
|||
filesorters::Config::create(&config_path).unwrap();
|
||||
}
|
||||
|
||||
match filesorters::Config::parse(config_path) {
|
||||
match filesorters::Config::parse(config_path.clone()) {
|
||||
Ok(parsed_config) => {
|
||||
CONFIG.set(parsed_config).unwrap();
|
||||
println!("Config file loaded successfully");
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {}", err);
|
||||
eprintln!("Error reading config: {}", err);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
println!("config:{:#?}", CONFIG.get().unwrap())
|
||||
}
|
||||
|
||||
{
|
||||
let mut counter = 1;
|
||||
for alias in CONFIG.get().unwrap().sources.keys() {
|
||||
|
@ -67,6 +71,14 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
if CONFIG.get().unwrap().sources.is_empty() {
|
||||
println!(
|
||||
"No sources found, please specify them in {}",
|
||||
config_path.display()
|
||||
);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let mut actual_selection: Vec<String> = Vec::new();
|
||||
|
||||
loop {
|
||||
|
@ -145,6 +157,10 @@ fn main() {
|
|||
let total_files_sorted: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
|
||||
|
||||
for selection in actual_selection {
|
||||
if cfg!(debug_assertions) {
|
||||
println!("spawned a thread with selection: {}", selection);
|
||||
}
|
||||
|
||||
let tfs = Arc::clone(&total_files_sorted);
|
||||
let thread = thread::spawn(move || match sort_files(selection, recursive) {
|
||||
Ok(files_sorted) => {
|
||||
|
@ -189,7 +205,7 @@ fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::
|
|||
(
|
||||
"Video",
|
||||
filesorters::VIDEO_EXTENTIONS.to_vec(),
|
||||
CONFIG.get().unwrap().videos_dir.clone(),
|
||||
CONFIG.get().unwrap().video_dir.clone(),
|
||||
),
|
||||
(
|
||||
"Music",
|
||||
|
@ -213,6 +229,9 @@ fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::
|
|||
|
||||
for (_label, valid_extensions, target_dir_option) in file_types {
|
||||
if let Some(target_dir) = target_dir_option {
|
||||
if cfg!(debug_assertions) {
|
||||
println!("{} - {}", file_path.display(), &extension);
|
||||
}
|
||||
if valid_extensions.contains(&extension) {
|
||||
move_file_to_directory(&file_path, target_dir);
|
||||
files_sorted += 1;
|
||||
|
@ -252,6 +271,15 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
|
|||
|
||||
let destination = dir.join(path.file_name().unwrap_or_default());
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
println!(
|
||||
"{} - {} - {}",
|
||||
path.display(),
|
||||
dir.display(),
|
||||
destination.display()
|
||||
);
|
||||
}
|
||||
|
||||
match fs::rename(path, &destination) {
|
||||
Ok(()) => {}
|
||||
Err(err) => eprintln!("Error moving file: {}", err),
|
||||
|
|
Loading…
Reference in a new issue