Skip to content

Commit

Permalink
Image files can now relocate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Jacob Gremlich committed Aug 20, 2019
1 parent b75bbe4 commit c5440db
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/photo_library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use self::glob::glob;
use std::fs::{create_dir_all, rename, File};
use std::path::{Display, Path};

fn make_dirs(date_time: &str) {
fn make_dir(date_time: &str) -> String {
let mut split_date_time_spaces = date_time.split_whitespace();

match split_date_time_spaces.next() {
Expand All @@ -14,16 +14,19 @@ fn make_dirs(date_time: &str) {
let dir_to_create = "./photos/".to_owned() + &replace_date_hyphens;

match create_dir_all(&dir_to_create) {
Ok(_e) => println!("{:?} Created!", &dir_to_create),
Err(_) => println!("{:?} could not be created!", &dir_to_create)
Ok(_e) => (),
Err(_) => println!("{:?} could not be created!", &dir_to_create),
}

return dir_to_create;
}
None => println!("{:?}", "No dates exist."),
};

return String::from("Somehow no directories were made!");
}

fn read_exif_date_data(image_path: &Display) -> String {
let image_path_str: &str = &image_path.to_string();
fn read_exif_date_data(image_path_str: &str) -> String {
let path = Path::new(image_path_str);

let file = File::open(path).unwrap();
Expand All @@ -41,14 +44,37 @@ pub fn make_photo_library(photos_dir_str: &str) {
let white_list_file_types: Vec<&str> = vec!["jpeg", "jpg", "JPEG", "JPG"];

for file_type in &white_list_file_types {
let glob_path = photos_dir_str.to_owned() + "**/*." + file_type.to_owned();
let glob_path = photos_dir_str.to_owned() + "/*." + file_type.to_owned();

for entry in glob(&glob_path).expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
let image_path: Display = path.display();
let date_data: String = read_exif_date_data(&image_path);
let image_path_str: &str = &image_path.to_string();

let date_data: String = read_exif_date_data(image_path_str);
let made_dir: String = make_dir(&date_data);

match path.file_name() {
Some(data) => {
match data.to_str() {
Some(file_name) => {
let new_file_name = made_dir + "/" + file_name;

println!("{:?}", new_file_name);
println!("{:?}", image_path_str);

make_dirs(&date_data);
match rename(image_path_str, new_file_name) {
Ok(_e) => println!("File relocated!"),
Err(_) => println!("File not relocated"),
};
},
_ => ()
}
},
_ => (),
};

}
Err(e) => println!("{:?}", e),
}
Expand Down

0 comments on commit c5440db

Please sign in to comment.