Skip to content

Commit

Permalink
Add support for finding similar images to CLI (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin authored Oct 14, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent acfecd7 commit 3dd203f
Showing 8 changed files with 578 additions and 10 deletions.
96 changes: 94 additions & 2 deletions Cargo.lock

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

23 changes: 19 additions & 4 deletions czkawka_cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ pub enum Commands {
#[structopt(flatten)]
excluded_items: ExcludedItems,
#[structopt(short, long, parse(try_from_str = parse_minimal_file_size), default_value = "1024", help = "Minimum size in bytes", long_help = "Minimum size of checked files in bytes, assigning bigger value may speed up searching")]
min_size: u64,
minimal_file_size: u64,
#[structopt(flatten)]
allowed_extensions: AllowedExtensions,
#[structopt(short, long, default_value = "HASH", parse(try_from_str = parse_checking_method), help = "Search method (SIZE, HASH, HASHMB)", long_help = "Methods to search files.\nSIZE - The fastest method, checking by the file's size,\nHASHMB - More accurate but slower, checking by the hash of the file's first mibibyte or\nHASH - The slowest method, checking by the hash of the entire file")]
@@ -83,6 +83,21 @@ pub enum Commands {
#[structopt(flatten)]
not_recursive: NotRecursive,
},
#[structopt(name = "ima", about = "Finds similar images", help_message = HELP_MESSAGE, after_help = "EXAMPLE:\n czkawka ima -d /home/rafal/ -E */.git */tmp* *Pulpit -f results.txt")]
SimilarImages {
#[structopt(flatten)]
directories: Directories,
#[structopt(flatten)]
excluded_directories: ExcludedDirectories,
#[structopt(short, long, parse(try_from_str = parse_minimal_file_size), default_value = "16384", help = "Minimum size in bytes", long_help = "Minimum size of checked files in bytes, assigning bigger value may speed up searching")]
minimal_file_size: u64,
#[structopt(flatten)]
excluded_items: ExcludedItems,
#[structopt(flatten)]
file_to_save: FileToSave,
#[structopt(flatten)]
not_recursive: NotRecursive,
},
}

#[derive(Debug, StructOpt)]
@@ -158,9 +173,9 @@ fn parse_delete_method(src: &str) -> Result<DeleteMethod, &'static str> {

fn parse_minimal_file_size(src: &str) -> Result<u64, String> {
match src.parse::<u64>() {
Ok(min_size) => {
if min_size > 0 {
Ok(min_size)
Ok(minimal_file_size) => {
if minimal_file_size > 0 {
Ok(minimal_file_size)
} else {
Err("Minimum file size must be at least 1 byte".to_string())
}
34 changes: 32 additions & 2 deletions czkawka_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ use czkawka_core::{
duplicate::DuplicateFinder,
empty_files::{self, EmptyFiles},
empty_folder::EmptyFolder,
similar_files::SimilarImages,
temporary::{self, Temporary},
};
use std::{path::PathBuf, process};
@@ -31,7 +32,7 @@ fn main() {
directories,
excluded_directories,
excluded_items,
min_size,
minimal_file_size,
allowed_extensions,
search_method,
delete_method,
@@ -43,7 +44,7 @@ fn main() {
df.set_included_directory(path_list_to_str(directories.directories));
df.set_excluded_directory(path_list_to_str(excluded_directories.excluded_directories));
df.set_excluded_items(path_list_to_str(excluded_items.excluded_items));
df.set_minimal_file_size(min_size);
df.set_minimal_file_size(minimal_file_size);
df.set_allowed_extensions(allowed_extensions.allowed_extensions.join(","));
df.set_check_method(search_method);
df.set_delete_method(delete_method);
@@ -178,5 +179,34 @@ fn main() {
tf.print_results();
tf.get_text_messages().print_messages();
}
Commands::SimilarImages {
directories,
excluded_directories,
excluded_items,
file_to_save,
minimal_file_size,
not_recursive,
} => {
let mut sf = SimilarImages::new();

sf.set_included_directory(path_list_to_str(directories.directories));
sf.set_excluded_directory(path_list_to_str(excluded_directories.excluded_directories));
sf.set_excluded_items(path_list_to_str(excluded_items.excluded_items));
sf.set_minimal_file_size(minimal_file_size);
sf.set_recursive_search(!not_recursive.not_recursive);

sf.find_similar_images(None);

if let Some(file_name) = file_to_save.file_name() {
if !sf.save_results_to_file(file_name) {
sf.get_text_messages().print_messages();
process::exit(1);
}
}

#[cfg(not(debug_assertions))] // This will show too much probably unnecessary data to debug, comment line only if needed
sf.print_results();
sf.get_text_messages().print_messages();
}
}
}
8 changes: 7 additions & 1 deletion czkawka_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -13,4 +13,10 @@ repository = "https://github.com/qarmin/czkawka"
humansize = "1"
blake3 = "0.3"
#rayon = "1"
crossbeam-channel = "0.4.4"
crossbeam-channel = "0.4.4"


# Needed by similar images
img_hash = "3.1"
bk-tree = "0.3"
image = "0.23"
2 changes: 1 addition & 1 deletion czkawka_core/src/big_file.rs
Original file line number Diff line number Diff line change
@@ -325,7 +325,7 @@ impl SaveResults for BigFile {
}
}
} else {
write!(file, "Not found any empty folders.").unwrap();
write!(file, "Not found any files.").unwrap();
}
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
true
1 change: 1 addition & 0 deletions czkawka_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -10,5 +10,6 @@ pub mod common_extensions;
pub mod common_items;
pub mod common_messages;
pub mod common_traits;
pub mod similar_files;

pub const CZKAWKA_VERSION: &str = env!("CARGO_PKG_VERSION");
Loading

0 comments on commit 3dd203f

Please sign in to comment.