Skip to content

Commit

Permalink
Added option to sort single file.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgremlich committed Aug 13, 2021
1 parent 180919d commit 0d17eed
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ photos/
.DS_Store
test-photos/
test-lib/
photo_organizer
*_organizer
media/
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "media_organizer"
version = "0.1.1"
version = "0.2.0"
authors = ["Andrew Gremlich <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion cp_organizer.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cp target/release/photo_organizer .
cp target/release/media_organizer .
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Need an organizer for your media content on the computer? Run this!

- Organize photos and videos in a folder structure based off creation dates.

- Organize a single media file, or a folder containing unorganized media files.

- Photos organized based off of EXIF creation dates.

- Whitelisted photo file types. ("jpeg", "jpg", "JPEG", "JPG", "HEIC", "heic", "PNG", "png")
Expand Down Expand Up @@ -43,9 +45,7 @@ cargo run -- --target test-photos

## Future development

- Make a multi-media organizer binary.
- Organize single file entry through cli.
- through CLI organize specific media files.
- Organize specific file types (i.e. sort with glob?).
- Organize audio files? https://github.com/pdeljanov/Symphonia
- Export web assembly binary to use in JS/Node/Electron.

Expand Down
21 changes: 13 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
extern crate clap;

mod env;
mod organize;
mod sorter;

use clap::{App, Arg, ArgMatches};
use env::set_env;
use organize::sorter;
use sorter::{sort_dir, sort_file};
use std::path::Path;

fn main() {
// TODO: Change target argument name? It's confusing me.
let matches: ArgMatches = App::new("Photo Organizer")
.version("0.4.0")
.version("0.2.0")
.author("Andrew Gremlich")
.about("Organize photos in one folder into date-centric folder structure.")
.arg(
Arg::with_name("target")
.short("t")
.long("target")
.value_name("TARGET_FOLDER")
.help("Target folder where all the unorganized photos are.")
.value_name("TARGET_MEDIA")
.help("Target media. Can be a folder with unorganized media or a single file.")
.takes_value(true)
.required(true),
)
Expand All @@ -36,10 +37,14 @@ fn main() {
set_env(&matches);

if let Some(targ) = matches.value_of("target") {
let dir_path = Path::new(targ);
let path = Path::new(targ);
let path_exists = path.exists();
let path_is_dir = path.is_dir();

if dir_path.is_dir() {
sorter(targ);
if path_exists && path_is_dir {
sort_dir(targ);
} else {
sort_file(targ);
}
}
}
48 changes: 0 additions & 48 deletions src/organize/mod.rs

This file was deleted.

File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions src/sorter/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
extern crate glob;
extern crate mkdirp;

mod media_info;
mod utils;

use self::glob::glob;
use media_info::date_data::{read_photo_creation_date, read_video_creation_date};
use mkdirp::mkdirp;
use utils::{get_white_list_file_types, is_photo, is_video, make_dir_string, move_image};

fn make_photo_dir_str(dir_str: &str) -> String {
let date_of_photo: String = read_photo_creation_date(dir_str);
let photo_dir_str: String = make_dir_string(date_of_photo.split_whitespace().next());
return photo_dir_str;
}

fn make_video_dir_str(dir_str: &str) -> String {
let date_of_video: String = read_video_creation_date(dir_str);
let video_dir_str: String = make_dir_string(date_of_video.split("T").next());
return video_dir_str;
}

fn handle_path(path: &str) {
let path_str: &str = path;
let mut date_data: String = String::new();

if is_photo(path_str) {
let photo_dir_str: String = make_photo_dir_str(path_str);
date_data.push_str(&photo_dir_str);
}
if is_video(path_str) {
let video_dir_str: String = make_video_dir_str(path_str);
date_data.push_str(&video_dir_str);
}

mkdirp(&date_data).unwrap();
move_image(path_str, &date_data);
}

pub fn sort_file(file_path: &str) {
handle_path(file_path);
}

pub fn sort_dir(dir_str: &str) {
let white_list_file_types: Vec<&str> = get_white_list_file_types();

for file_type in &white_list_file_types {
let mut glob_path: String = String::new();

glob_path.push_str(dir_str);
glob_path.push_str("/*.");
glob_path.push_str(file_type);

for entry in glob(&glob_path).expect("Failed to read glob pattern") {
match entry {
Ok(path) => {
handle_path(path.to_str().unwrap());
}
Err(e) => println!("{:?}", e),
}
}
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit 0d17eed

Please sign in to comment.