Skip to content

Commit fd2ad3c

Browse files
committed
💾 Feat(SyncCodes): Try to actually sync files
1 parent 17d9fd3 commit fd2ad3c

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

‎Utils/SyncCodes/FileItem.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class FileItem
88
{
99
public string Path { get; }
1010

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

1313
public FileItem(string path)
1414
{

‎Utils/SyncCodes/Program.cs

+54-15
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ await catalogResponse.Content.ReadAsStringAsync(),
137137

138138
var localCatalog = context.GetFiles();
139139

140+
// app.Logger.LogInformation("Remote catalog: {json}", JsonSerializer.Serialize(catalog, jsonSerializerOptions));
141+
// app.Logger.LogInformation("Local catalog: {json}", JsonSerializer.Serialize(localCatalog, jsonSerializerOptions));
142+
140143
var differences = catalog.Concat(localCatalog).GroupBy(
141144
f => f.Path.RelatedTo(workBase),
142145
f => f.Hash,
@@ -149,21 +152,57 @@ await catalogResponse.Content.ReadAsStringAsync(),
149152
}
150153
);
151154

152-
app.Logger.LogInformation(
153-
"Remote: {json1}\n Local: {json2}\n Differences: {json3}",
154-
JsonSerializer.Serialize(
155-
differences.Where(c => c.LocalHash is null && c.RemoteHash is not null),
156-
jsonSerializerOptions
157-
),
158-
JsonSerializer.Serialize(
159-
differences.Where(c => c.RemoteHash is null && c.LocalHash is not null),
160-
jsonSerializerOptions
161-
),
162-
JsonSerializer.Serialize(
163-
differences.Where(c => c.LocalHash is not null && c.RemoteHash is not null && !c.LocalHash.Equals(c.RemoteHash)),
164-
jsonSerializerOptions
165-
)
166-
);
155+
// var toDelete = differences.Where(c => c.LocalHash is not null && c.RemoteHash is null).ToList();
156+
// var toDownload = differences.Where(c => c.LocalHash is null && c.RemoteHash is not null).ToList();
157+
// var toUpdate = differences.Where(c => c.LocalHash is not null && c.RemoteHash is not null && !c.LocalHash.Equals(c.RemoteHash));
158+
//
159+
// app.Logger.LogInformation(
160+
// "Remote: {json1}\n Local: {json2}\n Differences: {json3}",
161+
// JsonSerializer.Serialize(toDelete, jsonSerializerOptions),
162+
// JsonSerializer.Serialize(toDownload, jsonSerializerOptions),
163+
// JsonSerializer.Serialize(toUpdate, jsonSerializerOptions)
164+
// );
165+
166+
foreach (var comparison in differences)
167+
{
168+
if (comparison.LocalHash is not null && comparison.RemoteHash is not null)
169+
{
170+
var filePath = Path.Combine(workBase, comparison.Path);
171+
if (!File.Exists(filePath))
172+
{
173+
app.Logger.LogWarning(
174+
"[{time}] Deleting file ({path}) but it doesn't exist.",
175+
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
176+
comparison.Path
177+
);
178+
continue;
179+
}
180+
181+
app.Logger.LogInformation(
182+
"[{time}] Deleting file: {path}",
183+
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
184+
comparison.Path
185+
);
186+
File.Delete(filePath);
187+
}
188+
else
189+
{
190+
var filePath = Path.Combine(workBase, comparison.Path);
191+
var fileContentResponse = await client.GetAsync(fileApi + Convert.ToBase64String(Encoding.UTF8.GetBytes(filePath)));
192+
if (!fileContentResponse.IsSuccessStatusCode)
193+
{
194+
app.Logger.LogError(
195+
"[{time}] Failed to fetch file ({path}) content: {statusCode}",
196+
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
197+
comparison.Path,
198+
fileContentResponse.StatusCode
199+
);
200+
}
201+
202+
var fileContent = await fileContentResponse.Content.ReadAsStringAsync();
203+
File.WriteAllText(filePath, fileContent);
204+
}
205+
}
167206
};
168207
timer.Start();
169208

0 commit comments

Comments
 (0)