Skip to content

Commit

Permalink
update localFiles if modified or new files added to R2
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 25, 2024
1 parent d281b2c commit 15d2fdb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions MyApp.ServiceInterface/Data/QuestionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions MyApp/Components/Pages/Questions/Question.razor
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
else
{
MessageProducer.Publish(new RenderComponent {
IfQuestionModified = Id,
Question = question,
});
}
Expand Down
15 changes: 15 additions & 0 deletions MyApp/Configure.Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public async Task SetHomeTabHtmlAsync(string? tab, string html)
}

public class RenderServices(
ILogger<RenderServices> log,
QuestionsProvider questions,
BlazorRenderer renderer,
RendererCache cache,
Expand All @@ -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);
Expand All @@ -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<QuestionPost>(new() { ["Question"] = request.Question });
await cache.SetQuestionPostHtmlAsync(request.Question.Id, html);
}
Expand Down

0 comments on commit 15d2fdb

Please sign in to comment.