Skip to content

Commit 4708834

Browse files
committed
🔧 Fix(SyncCodes): Some errors in the code
1 parent fd2ad3c commit 4708834

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

‎Utils/SyncCodes/Context.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public void RefreshFiles()
7272
Files.Clear();
7373
Files.AddRange(
7474
folder.GetFiles()
75-
.Where(f => Filter.ShouldIgnore(f.FullName) == false)
75+
.Where(f => Filter.Include(f.FullName))
7676
.Select(
7777
f => new FileItem(
7878
Path.GetRelativePath(WorkBase, f.FullName)
79-
).Read(_logger)
79+
).Read(WorkBase, _logger)
8080
)
8181
.Where(f => f.Hash is not null)
8282
);
@@ -89,11 +89,11 @@ public void RefreshFiles()
8989
foldersToSearch.Enqueue(dir);
9090
Files.AddRange(
9191
subFolder.GetFiles()
92-
.Where(f => Filter.ShouldIgnore(f.FullName) == false)
92+
.Where(f => Filter.Include(f.FullName))
9393
.Select(
9494
f => new FileItem(
9595
Path.GetRelativePath(WorkBase, f.FullName)
96-
).Read(_logger)
96+
).Read(WorkBase, _logger)
9797
)
9898
.Where(f => f.Hash is not null)
9999
);

‎Utils/SyncCodes/FileItem.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ public FileItem(string path)
1515
Path = path;
1616
}
1717

18-
public FileItem Read(ILogger logger)
18+
public FileItem Read(string workBase, ILogger logger)
1919
{
2020
// Avoid file not exists exception, sometimes IDE creates temporary files
2121
try
2222
{
2323
Policy.Handle<Exception>().Retry(3, (exception, retryCount) =>
2424
{
2525
logger.LogError(
26-
"Error loading sync ignore file: {message}, try times: {retryCount}",
26+
"Error loading file: {message}, try times: {retryCount}",
2727
exception.Message,
2828
retryCount
2929
);
3030
}).Execute(() =>
3131
{
32-
var content = File.ReadAllText(Path);
32+
var content = File.ReadAllText(System.IO.Path.Combine(workBase, Path));
3333
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(content));
3434
Hash = Convert.ToBase64String(hash);
3535
});

‎Utils/SyncCodes/FilesFilter.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class FilesFilter
88

99
public readonly List<string> IgnoredPaths = [];
1010

11-
public string WorkBase { get; init; }
11+
public string WorkBase { get; }
1212

1313
private bool _configFileExists = true;
1414

@@ -20,7 +20,7 @@ public FilesFilter(string workBase, ILogger logger)
2020

2121
WorkBase = workBase;
2222

23-
LoadIgnoreConfig();
23+
_ = LoadIgnoreConfig();
2424
}
2525

2626
public FilesFilter LoadIgnoreConfig()
@@ -30,7 +30,7 @@ public FilesFilter LoadIgnoreConfig()
3030

3131
var path = Path.Combine(WorkBase, ".sync-ignore");
3232

33-
if (!File.Exists(path))
33+
if (File.Exists(path) == false)
3434
{
3535
_configFileExists = false;
3636
return this;
@@ -71,7 +71,7 @@ public FilesFilter LoadIgnoreConfig()
7171
return this;
7272
}
7373

74-
public bool ShouldIgnore(string path)
74+
private bool ShouldIgnore(string path)
7575
{
7676
if (_configFileExists == false) return false;
7777

@@ -87,4 +87,6 @@ public bool ShouldIgnore(string path)
8787

8888
return false;
8989
}
90+
91+
public bool Include(string path) => !ShouldIgnore(path);
9092
}

‎Utils/SyncCodes/Program.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
context.InitializeFileSystemWatcher(
3636
(e) =>
3737
{
38-
if (context.Filter.ShouldIgnore(e.FullPath) == false)
38+
if (context.Filter.Include(e.FullPath))
3939
app.Logger.LogInformation(
4040
"[{time}] FileSystem Modified: {name}, {changeType} | {path}",
4141
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
@@ -88,10 +88,14 @@ void RunServer(Context context)
8888

8989
app.MapGet(
9090
"/file/{path}",
91-
(string path) => context.GetFile(
92-
Encoding.UTF8.GetString(
93-
Convert.FromBase64String(path)
94-
)
91+
(string path) => Results.Content(
92+
context.GetFile(
93+
Encoding.UTF8.GetString(
94+
Convert.FromBase64String(path)
95+
)
96+
),
97+
"text/plain",
98+
Encoding.UTF8
9599
)
96100
);
97101

0 commit comments

Comments
 (0)