Skip to content

Commit 17d9fd3

Browse files
committed
💾 Feat(SyncCodes): Better FileItem constructor
1 parent 66abcd2 commit 17d9fd3

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

‎Utils/SyncCodes/Context.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ public void RefreshFiles()
7575
.Where(f => Filter.ShouldIgnore(f.FullName) == false)
7676
.Select(
7777
f => new FileItem(
78-
Path.GetRelativePath(WorkBase, f.FullName),
79-
_logger
80-
)
78+
Path.GetRelativePath(WorkBase, f.FullName)
79+
).Read(_logger)
8180
)
82-
.Where(f => f.FileLoaded)
81+
.Where(f => f.Hash is not null)
8382
);
8483

8584
var foldersToSearch = new Queue<DirectoryInfo>(folder.GetDirectories());
@@ -93,11 +92,10 @@ public void RefreshFiles()
9392
.Where(f => Filter.ShouldIgnore(f.FullName) == false)
9493
.Select(
9594
f => new FileItem(
96-
Path.GetRelativePath(WorkBase, f.FullName),
97-
_logger
98-
)
95+
Path.GetRelativePath(WorkBase, f.FullName)
96+
).Read(_logger)
9997
)
100-
.Where(f => f.FileLoaded)
98+
.Where(f => f.Hash is not null)
10199
);
102100
}
103101
}

‎Utils/SyncCodes/FileItem.cs

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Security.Cryptography;
22
using System.Text;
3-
using System.Text.Json.Serialization;
43
using Polly;
54

65
namespace SyncCodes;
@@ -9,48 +8,50 @@ public class FileItem
98
{
109
public string Path { get; }
1110

12-
public string Hash { get; set; }
11+
public string? Hash { get; private set; }
1312

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)
1914
{
2015
Path = path;
16+
}
2117

22-
_logger = logger;
23-
18+
public FileItem Read(ILogger logger)
19+
{
2420
// Avoid file not exists exception, sometimes IDE creates temporary files
25-
Policy.Handle<Exception>().Retry(3, (exception, retryCount) =>
21+
try
2622
{
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 _)
3438
{
35-
var content = File.ReadAllText(Path);
36-
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(content));
37-
Hash = Convert.ToBase64String(hash);
39+
}
3840

39-
FileLoaded = true;
40-
});
41+
return this;
4142
}
4243
}
4344

4445
public class FileItemExistenceComparer : IEqualityComparer<FileItem>
4546
{
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;
4748

4849
public int GetHashCode(FileItem obj) => obj.GetHashCode();
4950
}
5051

5152
public class FileItemComparer : IEqualityComparer<FileItem>
5253
{
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);
5455

5556
public int GetHashCode(FileItem obj) => obj.GetHashCode();
5657
}

‎Utils/SyncCodes/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void RunServer(Context context)
8383

8484
app.MapGet(
8585
"/catalog",
86-
() => context.GetFiles().Where(f => f.FileLoaded)
86+
() => context.GetFiles().Where(f => f.Hash is not null)
8787
);
8888

8989
app.MapGet(
@@ -179,7 +179,7 @@ await catalogResponse.Content.ReadAsStringAsync(),
179179

180180
app.MapGet(
181181
"/catalog",
182-
() => context.GetFiles().Where(f => f.FileLoaded)
182+
() => context.GetFiles().Where(f => f.Hash is not null)
183183
);
184184

185185
app.Run();

0 commit comments

Comments
 (0)