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