1
1
using System . Security . Cryptography ;
2
2
using System . Text ;
3
- using System . Text . Json . Serialization ;
4
3
using Polly ;
5
4
6
5
namespace SyncCodes ;
@@ -9,48 +8,50 @@ public class FileItem
9
8
{
10
9
public string Path { get ; }
11
10
12
- public string Hash { get ; set ; }
11
+ public string ? Hash { get ; private set ; }
13
12
14
- [ JsonIgnore ] public bool FileLoaded { get ; set ; }
15
-
16
- private readonly ILogger _logger ;
17
-
18
- public FileItem ( string path , ILogger logger )
13
+ public FileItem ( string path )
19
14
{
20
15
Path = path ;
16
+ }
21
17
22
- _logger = logger ;
23
-
18
+ public FileItem Read ( ILogger logger )
19
+ {
24
20
// Avoid file not exists exception, sometimes IDE creates temporary files
25
- Policy . Handle < Exception > ( ) . Retry ( 3 , ( exception , retryCount ) =>
21
+ try
26
22
{
27
- _logger . LogError (
28
- "Error loading sync ignore file: {message}, try times: {retryCount}" ,
29
- exception . Message ,
30
- retryCount
31
- ) ;
32
-
33
- } ) . Execute ( ( ) =>
23
+ Policy . Handle < Exception > ( ) . Retry ( 3 , ( exception , retryCount ) =>
24
+ {
25
+ logger . LogError (
26
+ "Error loading sync ignore file: {message}, try times: {retryCount}" ,
27
+ exception . Message ,
28
+ retryCount
29
+ ) ;
30
+ } ) . Execute ( ( ) =>
31
+ {
32
+ var content = File . ReadAllText ( Path ) ;
33
+ var hash = SHA256 . HashData ( Encoding . UTF8 . GetBytes ( content ) ) ;
34
+ Hash = Convert . ToBase64String ( hash ) ;
35
+ } ) ;
36
+ }
37
+ catch ( Exception _ )
34
38
{
35
- var content = File . ReadAllText ( Path ) ;
36
- var hash = SHA256 . HashData ( Encoding . UTF8 . GetBytes ( content ) ) ;
37
- Hash = Convert . ToBase64String ( hash ) ;
39
+ }
38
40
39
- FileLoaded = true ;
40
- } ) ;
41
+ return this ;
41
42
}
42
43
}
43
44
44
45
public class FileItemExistenceComparer : IEqualityComparer < FileItem >
45
46
{
46
- public bool Equals ( FileItem ? x , FileItem ? y ) => x ? . Path ? . Equals ( y ? . Path ) ?? false ;
47
+ public bool Equals ( FileItem ? x , FileItem ? y ) => x ? . Path . Equals ( y ? . Path ) ?? false ;
47
48
48
49
public int GetHashCode ( FileItem obj ) => obj . GetHashCode ( ) ;
49
50
}
50
51
51
52
public class FileItemComparer : IEqualityComparer < FileItem >
52
53
{
53
- public bool Equals ( FileItem ? x , FileItem ? y ) => x is not null && y is not null && x . FileLoaded && y . FileLoaded && x . Path . Equals ( y . Path ) && x . Hash . Equals ( y . Hash ) ;
54
+ public bool Equals ( FileItem ? x , FileItem ? y ) => x is not null && y is not null && x . Path . Equals ( y . Path ) && ( x . Hash ? . Equals ( y . Hash ) ?? false ) ;
54
55
55
56
public int GetHashCode ( FileItem obj ) => obj . GetHashCode ( ) ;
56
57
}
0 commit comments