mirror of https://github.com/qarmin/czkawka
Big code refactoring
parent
3cff32c031
commit
8161a1c59b
@ -0,0 +1,252 @@
|
||||
use crate::common::Common;
|
||||
use crate::common_messages::Messages;
|
||||
use std::path::Path;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub struct Directories {
|
||||
pub excluded_directories: Vec<String>,
|
||||
pub included_directories: Vec<String>,
|
||||
}
|
||||
impl Directories {
|
||||
pub fn new() -> Directories {
|
||||
Directories {
|
||||
excluded_directories: vec![],
|
||||
included_directories: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Setting include directories, at least one must be provided
|
||||
pub fn set_included_directory(&mut self, mut included_directory: String, text_messages: &mut Messages) -> bool {
|
||||
let start_time: SystemTime = SystemTime::now();
|
||||
|
||||
if included_directory.is_empty() {
|
||||
text_messages.errors.push("At least one directory must be provided".to_string());
|
||||
return false;
|
||||
}
|
||||
|
||||
included_directory = included_directory.replace("\"", "");
|
||||
let directories: Vec<String> = included_directory.split(',').map(String::from).collect();
|
||||
let mut checked_directories: Vec<String> = Vec::new();
|
||||
|
||||
for directory in directories {
|
||||
let directory: String = directory.trim().to_string();
|
||||
|
||||
if directory == "" {
|
||||
continue;
|
||||
}
|
||||
if directory.contains('*') {
|
||||
text_messages.warnings.push("Include Directory Warning: Wildcards in path are not supported, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
if !directory.starts_with('/') {
|
||||
text_messages.warnings.push("Include Directory Warning: Relative path are not supported, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
if !Path::new(&directory).exists() {
|
||||
text_messages.warnings.push("Include Directory Warning: Provided folder path must exits, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
if !Path::new(&directory).is_dir() {
|
||||
text_messages.warnings.push("Include Directory Warning: Provided path must point at the directory, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// directory must end with /, due to possibility of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho
|
||||
if !directory.ends_with('/') {
|
||||
checked_directories.push(directory + "/");
|
||||
} else {
|
||||
checked_directories.push(directory);
|
||||
}
|
||||
}
|
||||
|
||||
if checked_directories.is_empty() {
|
||||
text_messages.errors.push("Include Directory ERROR: Not found even one correct path to include which is required.".to_string());
|
||||
return false;
|
||||
}
|
||||
|
||||
self.included_directories = checked_directories;
|
||||
|
||||
Common::print_time(start_time, SystemTime::now(), "set_included_directory".to_string());
|
||||
true
|
||||
}
|
||||
|
||||
/// Setting absolute path to exclude
|
||||
pub fn set_excluded_directory(&mut self, mut excluded_directory: String, text_messages: &mut Messages) {
|
||||
let start_time: SystemTime = SystemTime::now();
|
||||
if excluded_directory.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
excluded_directory = excluded_directory.replace("\"", "");
|
||||
let directories: Vec<String> = excluded_directory.split(',').map(String::from).collect();
|
||||
let mut checked_directories: Vec<String> = Vec::new();
|
||||
|
||||
for directory in directories {
|
||||
let directory: String = directory.trim().to_string();
|
||||
|
||||
if directory == "" {
|
||||
continue;
|
||||
}
|
||||
if directory == "/" {
|
||||
text_messages.errors.push("Exclude Directory ERROR: Excluding / is pointless, because it means that no files will be scanned.".to_string());
|
||||
break;
|
||||
}
|
||||
if directory.contains('*') {
|
||||
text_messages.warnings.push("Exclude Directory Warning: Wildcards in path are not supported, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
if !directory.starts_with('/') {
|
||||
text_messages.warnings.push("Exclude Directory Warning: Relative path are not supported, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
if !Path::new(&directory).exists() {
|
||||
text_messages.warnings.push("Exclude Directory Warning: Provided folder path must exits, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
if !Path::new(&directory).is_dir() {
|
||||
text_messages.warnings.push("Exclude Directory Warning: Provided path must point at the directory, ignoring ".to_string() + directory.as_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// directory must end with /, due to possibility of incorrect assumption, that e.g. /home/rafal is top folder to /home/rafalinho
|
||||
if !directory.ends_with('/') {
|
||||
checked_directories.push(directory.trim().to_string() + "/");
|
||||
} else {
|
||||
checked_directories.push(directory.trim().to_string());
|
||||
}
|
||||
}
|
||||
self.excluded_directories = checked_directories;
|
||||
|
||||
Common::print_time(start_time, SystemTime::now(), "set_excluded_directory".to_string());
|
||||
}
|
||||
|
||||
/// Remove unused entries when included or excluded overlaps with each other or are duplicated etc.
|
||||
pub fn optimize_directories(&mut self, recursive_search: bool, text_messages: &mut Messages) -> bool {
|
||||
let start_time: SystemTime = SystemTime::now();
|
||||
|
||||
let mut optimized_included: Vec<String> = Vec::<String>::new();
|
||||
let mut optimized_excluded: Vec<String> = Vec::<String>::new();
|
||||
|
||||
// Remove duplicated entries like: "/", "/"
|
||||
|
||||
self.excluded_directories.sort();
|
||||
self.included_directories.sort();
|
||||
|
||||
self.excluded_directories.dedup();
|
||||
self.included_directories.dedup();
|
||||
|
||||
// Optimize for duplicated included directories - "/", "/home". "/home/Pulpit" to "/"
|
||||
if recursive_search {
|
||||
// This is only point which can't be done when recursive search is disabled.
|
||||
let mut is_inside: bool;
|
||||
for ed_checked in &self.excluded_directories {
|
||||
is_inside = false;
|
||||
for ed_help in &self.excluded_directories {
|
||||
if ed_checked == ed_help {
|
||||
// We checking same element
|
||||
continue;
|
||||
}
|
||||
if ed_checked.starts_with(ed_help) {
|
||||
is_inside = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !is_inside {
|
||||
optimized_excluded.push(ed_checked.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
for id_checked in &self.included_directories {
|
||||
is_inside = false;
|
||||
for id_help in &self.included_directories {
|
||||
if id_checked == id_help {
|
||||
// We checking same element
|
||||
continue;
|
||||
}
|
||||
if id_checked.starts_with(id_help) {
|
||||
is_inside = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !is_inside {
|
||||
optimized_included.push(id_checked.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
self.included_directories = optimized_included;
|
||||
optimized_included = Vec::<String>::new();
|
||||
self.excluded_directories = optimized_excluded;
|
||||
optimized_excluded = Vec::<String>::new();
|
||||
}
|
||||
|
||||
// Remove include directories which are inside any exclude directory
|
||||
for id in &self.included_directories {
|
||||
let mut is_inside: bool = false;
|
||||
for ed in &self.excluded_directories {
|
||||
if id.starts_with(ed) {
|
||||
is_inside = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !is_inside {
|
||||
optimized_included.push(id.to_string());
|
||||
}
|
||||
}
|
||||
self.included_directories = optimized_included;
|
||||
optimized_included = Vec::<String>::new();
|
||||
|
||||
// Remove non existed directories
|
||||
for id in &self.included_directories {
|
||||
let path = Path::new(id);
|
||||
if path.exists() {
|
||||
optimized_included.push(id.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
for ed in &self.excluded_directories {
|
||||
let path = Path::new(ed);
|
||||
if path.exists() {
|
||||
optimized_excluded.push(ed.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
self.included_directories = optimized_included;
|
||||
// optimized_included = Vec::<String>::new();
|
||||
self.excluded_directories = optimized_excluded;
|
||||
optimized_excluded = Vec::<String>::new();
|
||||
|
||||
// Excluded paths must are inside include path, because
|
||||
for ed in &self.excluded_directories {
|
||||
let mut is_inside: bool = false;
|
||||
for id in &self.included_directories {
|
||||
if ed.starts_with(id) {
|
||||
is_inside = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if is_inside {
|
||||
optimized_excluded.push(ed.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
self.excluded_directories = optimized_excluded;
|
||||
// optimized_excluded = Vec::<String>::new();
|
||||
|
||||
if self.included_directories.is_empty() {
|
||||
text_messages.errors.push("Optimize Directories ERROR: Excluded directories overlaps all included directories.".to_string());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Not needed, but better is to have sorted everything
|
||||
self.excluded_directories.sort();
|
||||
self.included_directories.sort();
|
||||
Common::print_time(start_time, SystemTime::now(), "optimize_directories".to_string());
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Directories {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
use crate::common::Common;
|
||||
use crate::common_messages::Messages;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub struct Extensions {
|
||||
pub file_extensions: Vec<String>,
|
||||
}
|
||||
|
||||
impl Extensions {
|
||||
pub fn new() -> Extensions {
|
||||
Extensions { file_extensions: vec![] }
|
||||
}
|
||||
/// List of allowed extensions, only files with this extensions will be checking if are duplicates
|
||||
pub fn set_allowed_extensions(&mut self, mut allowed_extensions: String, text_messages: &mut Messages) {
|
||||
let start_time: SystemTime = SystemTime::now();
|
||||
if allowed_extensions.is_empty() {
|
||||
return;
|
||||
}
|
||||
allowed_extensions = allowed_extensions.replace("IMAGE", "jpg,kra,gif,png,bmp,tiff,webp,hdr,svg");
|
||||
allowed_extensions = allowed_extensions.replace("VIDEO", "mp4,flv,mkv,webm,vob,ogv,gifv,avi,mov,wmv,mpg,m4v,m4p,mpeg,3gp");
|
||||
allowed_extensions = allowed_extensions.replace("MUSIC", "mp3,flac,ogg,tta,wma,webm");
|
||||
allowed_extensions = allowed_extensions.replace("TEXT", "txt,doc,docx,odt,rtf");
|
||||
|
||||
let extensions: Vec<String> = allowed_extensions.split(',').map(String::from).collect();
|
||||
for mut extension in extensions {
|
||||
if extension == "" || extension.replace('.', "").trim() == "" {
|
||||
continue;
|
||||
}
|
||||
|
||||
if extension.starts_with('.') {
|
||||
extension = extension[1..].to_string();
|
||||
}
|
||||
|
||||
if extension[1..].contains('.') {
|
||||
text_messages.warnings.push(".".to_string() + extension.as_str() + " is not valid extension(valid extension doesn't have dot inside)");
|
||||
continue;
|
||||
}
|
||||
|
||||
if !self.file_extensions.contains(&extension.trim().to_string()) {
|
||||
self.file_extensions.push(extension.trim().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if self.file_extensions.is_empty() {
|
||||
text_messages.messages.push("No valid extensions were provided, so allowing all extensions by default.".to_string());
|
||||
}
|
||||
Common::print_time(start_time, SystemTime::now(), "set_allowed_extensions".to_string());
|
||||
}
|
||||
}
|
||||
impl Default for Extensions {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
use crate::common::Common;
|
||||
use crate::common_messages::Messages;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub struct ExcludedItems {
|
||||
pub items: Vec<String>,
|
||||
}
|
||||
|
||||
impl ExcludedItems {
|
||||
pub fn new() -> ExcludedItems {
|
||||
ExcludedItems { items: vec![] }
|
||||
}
|
||||
/// Setting excluded items which needs to contains * wildcrard
|
||||
/// Are a lot of slower than absolute path, so it should be used to heavy
|
||||
pub fn set_excluded_items(&mut self, mut excluded_items: String, text_messages: &mut Messages) {
|
||||
let start_time: SystemTime = SystemTime::now();
|
||||
|
||||
if excluded_items.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
excluded_items = excluded_items.replace("\"", "");
|
||||
let expressions: Vec<String> = excluded_items.split(',').map(String::from).collect();
|
||||
let mut checked_expressions: Vec<String> = Vec::new();
|
||||
|
||||
for expression in expressions {
|
||||
let expression: String = expression.trim().to_string();
|
||||
|
||||
if expression == "" {
|
||||
continue;
|
||||
}
|
||||
if expression == "DEFAULT" {
|
||||
// TODO add more files by default
|
||||
checked_expressions.push("*/.git/*".to_string());
|
||||
continue;
|
||||
}
|
||||
if !expression.contains('*') {
|
||||
text_messages.warnings.push("Excluded Items Warning: Wildcard * is required in expression, ignoring ".to_string() + expression.as_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
checked_expressions.push(expression);
|
||||
}
|
||||
self.items = checked_expressions;
|
||||
Common::print_time(start_time, SystemTime::now(), "set_excluded_items".to_string());
|
||||
}
|
||||
}
|
||||
impl Default for ExcludedItems {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
pub struct Messages {
|
||||
pub messages: Vec<String>,
|
||||
pub warnings: Vec<String>,
|
||||
pub errors: Vec<String>,
|
||||
}
|
||||
|
||||
impl Messages {
|
||||
pub fn new() -> Messages {
|
||||
Messages {
|
||||
messages: vec![],
|
||||
warnings: vec![],
|
||||
errors: vec![],
|
||||
}
|
||||
}
|
||||
pub fn print_messages(&self) {
|
||||
if !self.messages.is_empty() {
|
||||
println!("-------------------------------MESSAGES--------------------------------");
|
||||
for i in &self.messages {
|
||||
println!("{}", i);
|
||||
}
|
||||
println!("---------------------------END OF MESSAGES-----------------------------");
|
||||
}
|
||||
|
||||
if !self.warnings.is_empty() {
|
||||
println!("-------------------------------WARNINGS--------------------------------");
|
||||
|
||||
for i in &self.warnings {
|
||||
println!("{}", i);
|
||||
}
|
||||
println!("---------------------------END OF WARNINGS-----------------------------");
|
||||
}
|
||||
|
||||
if !self.errors.is_empty() {
|
||||
println!("--------------------------------ERRORS---------------------------------");
|
||||
|
||||
for i in &self.errors {
|
||||
println!("{}", i);
|
||||
}
|
||||
println!("----------------------------END OF ERRORS------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Default for Messages {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
pub trait DebugPrint {
|
||||
fn debug_print(&self);
|
||||
}
|
||||
pub trait SaveResults {
|
||||
fn save_results_to_file(&mut self, file_name: &str) -> bool;
|
||||
}
|
||||
pub trait PrintResults {
|
||||
fn print_results(&self);
|
||||
}
|
Loading…
Reference in New Issue