From 15d2fdb25e8f2966cf1f9d9161adce64c08c049a Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Mon, 25 Mar 2024 10:15:28 +0800 Subject: [PATCH] update localFiles if modified or new files added to R2 --- MyApp.ServiceInterface/Data/QuestionsProvider.cs | 5 +++++ MyApp/Components/Pages/Questions/Question.razor | 1 + MyApp/Configure.Renderer.cs | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/MyApp.ServiceInterface/Data/QuestionsProvider.cs b/MyApp.ServiceInterface/Data/QuestionsProvider.cs index bb15102..95b4a54 100644 --- a/MyApp.ServiceInterface/Data/QuestionsProvider.cs +++ b/MyApp.ServiceInterface/Data/QuestionsProvider.cs @@ -134,4 +134,9 @@ public async Task SaveAnswerAsync(int postId, string model, string json) { await SaveFileAsync(GetModelAnswerPath(postId, model), json); } + + public async Task SaveLocalFileAsync(string virtualPath, string contents) + { + await fs.WriteFileAsync(virtualPath, contents); + } } diff --git a/MyApp/Components/Pages/Questions/Question.razor b/MyApp/Components/Pages/Questions/Question.razor index bc1a71b..7a8e4c1 100644 --- a/MyApp/Components/Pages/Questions/Question.razor +++ b/MyApp/Components/Pages/Questions/Question.razor @@ -128,6 +128,7 @@ else { MessageProducer.Publish(new RenderComponent { + IfQuestionModified = Id, Question = question, }); } diff --git a/MyApp/Configure.Renderer.cs b/MyApp/Configure.Renderer.cs index 048f30e..fc17674 100644 --- a/MyApp/Configure.Renderer.cs +++ b/MyApp/Configure.Renderer.cs @@ -127,6 +127,7 @@ public async Task SetHomeTabHtmlAsync(string? tab, string html) } public class RenderServices( + ILogger log, QuestionsProvider questions, BlazorRenderer renderer, RendererCache cache, @@ -153,8 +154,21 @@ public async Task Any(RenderComponent request) await ShouldRegenerateMeta(id, localFiles, remoteFiles, dbStatTotals, allPostVotes); if (regenerateMeta) { + log.LogInformation("Regenerating Meta for Post {Id}...", id); await RegenerateMeta(dbAnalytics, id, remoteFiles, dbStatTotals, allPostVotes); } + + // Update Local Files with new or modified remote files + foreach (var remoteFile in remoteFiles.Files) + { + var localFile = localFiles.Files.FirstOrDefault(x => x.Name == remoteFile.Name); + if (localFile == null || localFile.LastModified < remoteFile.LastModified) + { + log.LogInformation("Saving local file for {State} {Path}", localFile == null ? "new" : "modified", remoteFile.VirtualPath); + var remoteContents = await remoteFile.ReadAllTextAsync(); + await questions.SaveLocalFileAsync(remoteFile.VirtualPath, remoteContents); + } + } var rerenderPostHtml = regenerateMeta; var htmlPostPath = cache.GetCachedQuestionPostPath(id); @@ -173,6 +187,7 @@ public async Task Any(RenderComponent request) if (request.Question != null) { + log.LogInformation("Rendering Question Post HTML {Id}...", request.Question.Id); var html = await renderer.RenderComponent(new() { ["Question"] = request.Question }); await cache.SetQuestionPostHtmlAsync(request.Question.Id, html); }