@@ -115,64 +115,46 @@ private async void btnIndex_Click(object sender, EventArgs e)
115
115
return ;
116
116
}
117
117
118
- if ( string . IsNullOrEmpty ( txtDirectory . Text ) )
118
+ IndexRunning = true ;
119
+ btnIndex . Text = "停止索引" ;
120
+ var dirs = string . IsNullOrWhiteSpace ( txtDirectory . Text ) ? PathPrefixFinder . FindLongestCommonPathPrefixes ( _index . Keys . Union ( _frameIndex . Keys ) , 3 ) . Where ( Directory . Exists ) . ToArray ( ) : [ txtDirectory . Text ] ;
121
+ if ( dirs . Length == 0 )
119
122
{
120
123
MessageBox . Show ( this , "请先选择文件夹" , "警告" , MessageBoxButtons . OK , MessageBoxIcon . Warning , MessageBoxDefaultButton . Button1 ) ;
124
+ IndexRunning = false ;
125
+ btnIndex . Text = "更新索引" ;
121
126
return ;
122
127
}
123
128
129
+ cbRemoveInvalidIndex . Hide ( ) ;
130
+ var imageHasher = new ImageHasher ( new ImageSharpTransformer ( ) ) ;
131
+ lblProcess . Text = "正在扫描文件..." ;
132
+ var files = File . Exists ( "Everything64.dll" ) && Process . GetProcessesByName ( "Everything" ) . Length > 0 ? dirs . SelectMany ( s =>
133
+ {
134
+ var array = EverythingHelper . EnumerateFiles ( s ) . ToArray ( ) ;
135
+ return array . Length == 0 ? Directory . GetFiles ( s , "*" , SearchOption . AllDirectories ) : array ;
136
+ } ) . ToArray ( ) : dirs . SelectMany ( s => Directory . GetFiles ( s , "*" , SearchOption . AllDirectories ) ) . ToArray ( ) ;
124
137
if ( cbRemoveInvalidIndex . Checked )
125
138
{
126
139
_ = Task . Run ( ( ) =>
127
140
{
128
- foreach ( var key in _index . Keys . Except ( _index . Keys . GroupBy ( x => string . Join ( '\\ ' , x . Split ( '\\ ' ) [ ..2 ] ) ) . SelectMany ( g =>
129
- {
130
- if ( Directory . Exists ( g . Key ) )
131
- {
132
- if ( File . Exists ( "Everything64.dll" ) && Process . GetProcessesByName ( "Everything" ) . Length > 0 )
133
- {
134
- var files = EverythingHelper . EnumerateFiles ( FindLCP ( g . ToArray ( ) ) , "*.jpg|*.jpeg|*.bmp|*.png|*.webp" ) . ToList ( ) ;
135
- if ( files . Count == 0 )
136
- {
137
- throw new Exception ( "所选目录未包含在everything索引范围,或文件数为0,请检查" ) ;
138
- }
139
-
140
- return files ;
141
- }
142
-
143
- return Directory . EnumerateFiles ( FindLCP ( g . ToArray ( ) ) , "*" , new EnumerationOptions ( )
144
- {
145
- IgnoreInaccessible = true ,
146
- AttributesToSkip = FileAttributes . System | FileAttributes . Temporary ,
147
- RecurseSubdirectories = true
148
- } ) . Where ( s => picRegex . IsMatch ( s ) ) ;
149
- }
150
-
151
- return [ ] ;
152
- } ) ) . AsParallel ( ) . WithDegreeOfParallelism ( Environment . ProcessorCount * 4 ) . Where ( s => ! File . Exists ( s ) ) )
141
+ var allfiles = files ;
142
+ if ( ! string . IsNullOrWhiteSpace ( txtDirectory . Text ) )
143
+ {
144
+ var alldirs = PathPrefixFinder . FindLongestCommonPathPrefixes ( _index . Keys . Union ( _frameIndex . Keys ) , 3 ) . Where ( Directory . Exists ) ;
145
+ allfiles = File . Exists ( "Everything64.dll" ) && Process . GetProcessesByName ( "Everything" ) . Length > 0 ? alldirs . SelectMany ( s =>
146
+ {
147
+ var array = EverythingHelper . EnumerateFiles ( s ) . ToArray ( ) ;
148
+ return array . Length == 0 ? Directory . GetFiles ( s , "*" , SearchOption . AllDirectories ) : array ;
149
+ } ) . ToArray ( ) : alldirs . SelectMany ( s => Directory . GetFiles ( s , "*" , SearchOption . AllDirectories ) ) . ToArray ( ) ;
150
+ }
151
+
152
+ foreach ( var key in _index . Keys . Except ( allfiles ) . AsParallel ( ) . WithDegreeOfParallelism ( Environment . ProcessorCount * 4 ) . Where ( s => ! File . Exists ( s ) ) )
153
153
{
154
154
_index . TryRemove ( key , out _ ) ;
155
155
}
156
156
157
- foreach ( var key in _frameIndex . Keys . Except ( _frameIndex . Keys . GroupBy ( x => string . Join ( '\\ ' , x . Split ( '\\ ' ) [ ..2 ] ) ) . SelectMany ( g =>
158
- {
159
- if ( Directory . Exists ( g . Key ) )
160
- {
161
- if ( File . Exists ( "Everything64.dll" ) && Process . GetProcessesByName ( "Everything" ) . Length > 0 )
162
- {
163
- return EverythingHelper . EnumerateFiles ( FindLCP ( g . ToArray ( ) ) , "*.gif" ) ;
164
- }
165
-
166
- return Directory . EnumerateFiles ( FindLCP ( g . ToArray ( ) ) , "*.gif" , new EnumerationOptions ( )
167
- {
168
- IgnoreInaccessible = true ,
169
- AttributesToSkip = FileAttributes . System | FileAttributes . Temporary ,
170
- RecurseSubdirectories = true
171
- } ) ;
172
- }
173
-
174
- return [ ] ;
175
- } ) ) . AsParallel ( ) . WithDegreeOfParallelism ( Environment . ProcessorCount * 4 ) . Where ( s => ! File . Exists ( s ) ) )
157
+ foreach ( var key in _frameIndex . Keys . Except ( allfiles ) . AsParallel ( ) . WithDegreeOfParallelism ( Environment . ProcessorCount * 4 ) . Where ( s => ! File . Exists ( s ) ) )
176
158
{
177
159
_frameIndex . TryRemove ( key , out _ ) ;
178
160
}
@@ -187,12 +169,6 @@ private async void btnIndex_Click(object sender, EventArgs e)
187
169
} ) . ConfigureAwait ( false ) ;
188
170
}
189
171
190
- IndexRunning = true ;
191
- btnIndex . Text = "停止索引" ;
192
- cbRemoveInvalidIndex . Hide ( ) ;
193
- var imageHasher = new ImageHasher ( new ImageSharpTransformer ( ) ) ;
194
- lblProcess . Text = "正在扫描文件..." ;
195
- var files = File . Exists ( "Everything64.dll" ) && Process . GetProcessesByName ( "Everything" ) . Length > 0 ? EverythingHelper . EnumerateFiles ( txtDirectory . Text ) . ToArray ( ) : Directory . GetFiles ( txtDirectory . Text , "*" , SearchOption . AllDirectories ) ;
196
172
int ? filesCount = files . Except ( _index . Keys ) . Except ( _frameIndex . Keys ) . Count ( s => Regex . IsMatch ( s , "(gif|jpg|jpeg|png|bmp|webp)$" , RegexOptions . IgnoreCase ) ) ;
197
173
var local = new ThreadLocal < int > ( true ) ;
198
174
var errors = new List < string > ( ) ;
@@ -285,34 +261,6 @@ await Task.Run(() =>
285
261
btnIndex . Text = "更新索引" ;
286
262
}
287
263
288
- private static string FindLCP ( string [ ] strs )
289
- {
290
- if ( strs == null || strs . Length == 0 ) return "" ;
291
-
292
- // 找到最短的字符串,因为LCP不会超过最短字符串的长度
293
- string shortest = strs [ 0 ] ;
294
- for ( int i = 1 ; i < strs . Length ; i ++ )
295
- {
296
- if ( strs [ i ] . Length < shortest . Length )
297
- shortest = strs [ i ] ;
298
- }
299
-
300
- // 逐字符比较,直到找到不匹配的字符
301
- for ( int i = 0 ; i < shortest . Length ; i ++ )
302
- {
303
- char c = shortest [ i ] ;
304
- for ( int j = 1 ; j < strs . Length ; j ++ )
305
- {
306
- // 如果当前索引越界或字符不匹配,则返回当前LCP
307
- if ( i >= strs [ j ] . Length || strs [ j ] [ i ] != c )
308
- return shortest . Substring ( 0 , i ) ;
309
- }
310
- }
311
-
312
- // 如果所有字符串共享整个最短字符串,则返回它
313
- return shortest ;
314
- }
315
-
316
264
private void btnSearch_Click ( object sender , EventArgs e )
317
265
{
318
266
var filename = txtPic . Text ;
0 commit comments