1
- <<<<<<< HEAD
2
1
use std:: collections:: { BTreeSet , HashMap , HashSet } ;
3
- use std:: fs:: OpenOptions ;
4
- =======
5
- use std:: collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ;
6
- >>>>>>> 42 e62c3 ( Simplify cache code)
7
2
use std:: fs:: { File , Metadata } ;
8
3
use std:: io:: Write ;
9
4
use std:: io:: * ;
@@ -133,6 +128,7 @@ pub struct SimilarImages {
133
128
exclude_images_with_same_size : bool ,
134
129
use_reference_folders : bool ,
135
130
fast_comparing : bool ,
131
+ save_also_as_json : bool ,
136
132
}
137
133
138
134
/// Info struck with helpful information's about results
@@ -176,6 +172,7 @@ impl SimilarImages {
176
172
exclude_images_with_same_size : false ,
177
173
use_reference_folders : false ,
178
174
fast_comparing : false ,
175
+ save_also_as_json : false ,
179
176
}
180
177
}
181
178
@@ -207,6 +204,9 @@ impl SimilarImages {
207
204
pub fn set_fast_comparing ( & mut self , fast_comparing : bool ) {
208
205
self . fast_comparing = fast_comparing;
209
206
}
207
+ pub fn set_save_also_as_json ( & mut self , save_also_as_json : bool ) {
208
+ self . save_also_as_json = save_also_as_json;
209
+ }
210
210
211
211
pub fn get_stopped_search ( & self ) -> bool {
212
212
self . stopped_search
@@ -647,7 +647,14 @@ impl SimilarImages {
647
647
for ( file_entry, _hash) in vec_file_entry {
648
648
all_results. insert ( file_entry. path . to_string_lossy ( ) . to_string ( ) , file_entry) ;
649
649
}
650
- save_hashes_to_file ( & all_results, & mut self . text_messages , self . hash_size , self . hash_alg , self . image_filter ) ;
650
+ save_hashes_to_file (
651
+ & all_results,
652
+ & mut self . text_messages ,
653
+ self . save_also_as_json ,
654
+ self . hash_size ,
655
+ self . hash_alg ,
656
+ self . image_filter ,
657
+ ) ;
651
658
}
652
659
653
660
Common :: print_time ( hash_map_modification, SystemTime :: now ( ) , "sort_images - saving data to files" . to_string ( ) ) ;
@@ -1036,49 +1043,36 @@ impl PrintResults for SimilarImages {
1036
1043
}
1037
1044
}
1038
1045
1039
- <<<<<<< HEAD
1040
- pub fn save_hashes_to_file( hashmap : & HashMap < String , FileEntry > , text_messages : & mut Messages , hash_size : u8 , hash_alg : HashAlg , image_filter : FilterType ) {
1041
- if let Some ( proj_dirs ) = ProjectDirs :: from ( "pl" , "Qarmin" , "Czkawka" ) {
1042
- let cache_dir = PathBuf :: from ( proj_dirs. cache_dir ( ) ) ;
1043
- if cache_dir. exists( ) {
1044
- if !cache_dir. is_dir( ) {
1045
- text_messages. messages. push( format ! ( "Config dir {} is a file!" , cache_dir. display( ) ) ) ;
1046
- return ;
1047
- }
1048
- } else if let Err ( e) = fs:: create_dir_all ( & cache_dir ) {
1049
- text_messages. messages. push( format ! ( "Cannot create config dir {}, reason {}" , cache_dir. display( ) , e) ) ;
1050
- return ;
1051
- }
1052
-
1053
- let cache_file = cache_dir. join( cache_dir. join( get_cache_file( & hash_size, & hash_alg, & image_filter) ) ) ;
1054
- let file_handler = match OpenOptions :: new ( ) . truncate( true ) . write( true ) . create( true ) . open( & cache_file) {
1055
- Ok ( t) => t ,
1056
- Err ( e) => {
1046
+ pub fn save_hashes_to_file (
1047
+ hashmap : & HashMap < String , FileEntry > ,
1048
+ text_messages : & mut Messages ,
1049
+ save_also_as_json : bool ,
1050
+ hash_size : u8 ,
1051
+ hash_alg : HashAlg ,
1052
+ image_filter : FilterType ,
1053
+ ) {
1054
+ if let Some ( ( ( file_handler, cache_file) , ( file_handler_json, cache_file_json) ) ) =
1055
+ open_cache_folder ( & get_cache_file ( & hash_size, & hash_alg, & image_filter) , true , save_also_as_json, & mut text_messages. warnings )
1056
+ {
1057
+ {
1058
+ let writer = BufWriter :: new ( file_handler. unwrap ( ) ) ; // Unwrap because cannot fail here
1059
+ if let Err ( e) = bincode:: serialize_into ( writer, hashmap) {
1057
1060
text_messages
1058
- . messages
1059
- . push( format ! ( "Cannot create or open cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1061
+ . warnings
1062
+ . push ( format ! ( "Cannot write data to cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1060
1063
return ;
1061
1064
}
1062
- } ;
1063
-
1064
- =======
1065
- pub fn save_hashes_to_file ( hashmap : & BTreeMap < String , FileEntry > , text_messages : & mut Messages , hash_size : u8 , hash_alg : HashAlg , image_filter : FilterType ) {
1066
- if let Some ( ( file_handler, cache_file) ) = open_cache_folder( & get_cache_file( & hash_size, & hash_alg, & image_filter) , true , & mut text_messages. warnings) {
1067
- >>>>>>> 42e62 c3 ( Simplify cache code)
1068
- let writer = BufWriter :: new ( file_handler ) ;
1069
- #[ cfg( not( debug_assertions) ) ]
1070
- if let Err ( e) = bincode:: serialize_into ( writer , hashmap ) {
1071
- text_messages
1072
- . warnings
1073
- . push( format ! ( "Cannot write data to cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1074
- return ;
1075
1065
}
1076
- #[ cfg( debug_assertions) ]
1077
- if let Err ( e) = serde_json:: to_writer ( writer , hashmap ) {
1078
- text_messages
1079
- . warnings
1080
- . push( format ! ( "Cannot write data to cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1081
- return ;
1066
+ if save_also_as_json {
1067
+ if let Some ( file_handler_json) = file_handler_json {
1068
+ let writer = BufWriter :: new ( file_handler_json) ;
1069
+ if let Err ( e) = serde_json:: to_writer ( writer, hashmap) {
1070
+ text_messages
1071
+ . warnings
1072
+ . push ( format ! ( "Cannot write data to cache file {}, reason {}" , cache_file_json. display( ) , e) ) ;
1073
+ return ;
1074
+ }
1075
+ }
1082
1076
}
1083
1077
1084
1078
text_messages. messages . push ( format ! ( "Properly saved to file {} cache entries." , hashmap. len( ) ) ) ;
@@ -1091,44 +1085,34 @@ pub fn load_hashes_from_file(
1091
1085
hash_size : u8 ,
1092
1086
hash_alg : HashAlg ,
1093
1087
image_filter : FilterType ,
1094
- <<<<<<< HEAD
1095
1088
) -> Option < HashMap < String , FileEntry > > {
1096
- if let Some ( proj_dirs ) = ProjectDirs :: from ( "pl" , "Qarmin" , "Czkawka" ) {
1097
- let cache_dir = PathBuf :: from ( proj_dirs. cache_dir ( ) ) ;
1098
- let cache_file = cache_dir. join( get_cache_file( & hash_size, & hash_alg, & image_filter) ) ;
1099
- let file_handler = match OpenOptions :: new ( ) . read( true ) . open( & cache_file) {
1100
- Ok ( t) => t ,
1101
- Err ( _inspected) => {
1102
- // text_messages.messages.push(format!("Cannot find or open cache file {}", cache_file.display())); // No error warning
1103
- return None ;
1104
- }
1105
- } ;
1106
-
1107
- =======
1108
- ) -> Option < BTreeMap < String , FileEntry > > {
1109
- if let Some ( ( file_handler , cache_file ) ) = open_cache_folder ( & get_cache_file ( & hash_size , & hash_alg , & image_filter ) , false , & mut text_messages . warnings) {
1110
- >>>>>>> 42 e62c3 ( Simplify cache code)
1111
- let reader = BufReader :: new ( file_handler ) ;
1112
- #[ cfg( debug_assertions) ]
1113
- let mut hashmap_loaded_entries: HashMap <String , FileEntry > = match serde_json:: from_reader ( reader ) {
1114
- Ok ( t) => t,
1115
- Err ( e) => {
1116
- text_messages
1117
- . warnings
1118
- . push ( format ! ( "Failed to load data from cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1119
- return None ;
1120
- }
1121
- } ;
1122
- #[ cfg ( not ( debug_assertions ) ) ]
1123
- let mut hashmap_loaded_entries : HashMap < String , FileEntry > = match bincode:: deserialize_from ( reader ) {
1124
- Ok ( t ) => t,
1125
- Err ( e) => {
1126
- text_messages
1127
- . warnings
1128
- . push ( format ! ( "Failed to load data from cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1129
- return None ;
1130
- }
1131
- } ;
1089
+ if let Some ( ( ( file_handler, cache_file) , ( file_handler_json, cache_file_json) ) ) =
1090
+ open_cache_folder ( & get_cache_file ( & hash_size, & hash_alg, & image_filter) , false , true , & mut text_messages. warnings )
1091
+ {
1092
+ let mut hashmap_loaded_entries: HashMap < String , FileEntry > ;
1093
+ if let Some ( file_handler) = file_handler {
1094
+ let reader = BufReader :: new ( file_handler) ;
1095
+ hashmap_loaded_entries = match bincode:: deserialize_from ( reader) {
1096
+ Ok ( t) => t,
1097
+ Err ( e) => {
1098
+ text_messages
1099
+ . warnings
1100
+ . push ( format ! ( "Failed to load data from cache file {}, reason {}" , cache_file. display( ) , e) ) ;
1101
+ return None ;
1102
+ }
1103
+ } ;
1104
+ } else {
1105
+ let reader = BufReader :: new ( file_handler_json. unwrap ( ) ) ; // Unwrap cannot fail, because at least one file must be valid
1106
+ hashmap_loaded_entries = match serde_json:: from_reader ( reader) {
1107
+ Ok ( t) => t,
1108
+ Err ( e) => {
1109
+ text_messages
1110
+ . warnings
1111
+ . push ( format ! ( "Failed to load data from cache file {}, reason {}" , cache_file_json. display( ) , e) ) ;
1112
+ return None ;
1113
+ }
1114
+ } ;
1115
+ }
1132
1116
1133
1117
// Don't load cache data if destination file not exists
1134
1118
if delete_outdated_cache {
@@ -1143,22 +1127,11 @@ pub fn load_hashes_from_file(
1143
1127
}
1144
1128
1145
1129
fn get_cache_file ( hash_size : & u8 , hash_alg : & HashAlg , image_filter : & FilterType ) -> String {
1146
- let extension;
1147
- #[ cfg( debug_assertions) ]
1148
- {
1149
- extension = "json" ;
1150
- }
1151
- #[ cfg( not( debug_assertions) ) ]
1152
- {
1153
- extension = "bin" ;
1154
- }
1155
-
1156
1130
format ! (
1157
- "cache_similar_images_{}_{}_{}.{} " ,
1131
+ "cache_similar_images_{}_{}_{}.bin " ,
1158
1132
hash_size,
1159
1133
convert_algorithm_to_string( hash_alg) ,
1160
1134
convert_filters_to_string( image_filter) ,
1161
- extension
1162
1135
)
1163
1136
}
1164
1137
0 commit comments