From 03e4e243dd6e0ea546d033c55d3077aa19a45839 Mon Sep 17 00:00:00 2001 From: Ivar Nesje Date: Fri, 13 Jan 2023 12:31:22 +0100 Subject: [PATCH] Localtest: Sort files in directoryBrowser by LastModified (#9572) Sorting by guid does not make any sense. Co-authored-by: Ivar --- src/Helpers/SortedHtmlDirectoryFormatter.cs | 16 ++++++++++++++++ src/Startup.cs | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/Helpers/SortedHtmlDirectoryFormatter.cs diff --git a/src/Helpers/SortedHtmlDirectoryFormatter.cs b/src/Helpers/SortedHtmlDirectoryFormatter.cs new file mode 100644 index 00000000..ea6fe671 --- /dev/null +++ b/src/Helpers/SortedHtmlDirectoryFormatter.cs @@ -0,0 +1,16 @@ +using System.Text.Encodings.Web; +using Microsoft.AspNetCore.StaticFiles; +using Microsoft.Extensions.FileProviders; + +namespace LocalTest.Helpers; + +public class SortedHtmlDirectoryFormatter : HtmlDirectoryFormatter +{ + public SortedHtmlDirectoryFormatter() : base(HtmlEncoder.Default) { } + + public override Task GenerateContentAsync(HttpContext context, IEnumerable contents) + { + var sorted = contents.OrderByDescending(f => f.LastModified); + return base.GenerateContentAsync(context, sorted); + } +} diff --git a/src/Startup.cs b/src/Startup.cs index d56f2c37..ef5c1203 100644 --- a/src/Startup.cs +++ b/src/Startup.cs @@ -23,6 +23,7 @@ using AltinnCore.Authentication.JwtCookie; using LocalTest.Clients.CdnAltinnOrgs; using LocalTest.Configuration; +using LocalTest.Helpers; using LocalTest.Services.Authentication.Implementation; using LocalTest.Services.Authentication.Interface; using LocalTest.Services.Authorization.Implementation; @@ -208,7 +209,8 @@ public void Configure( app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = new PhysicalFileProvider(localPlatformSettings.Value.LocalTestingStorageBasePath), - RequestPath = "/LocalPlatformStorage" + RequestPath = "/LocalPlatformStorage", + Formatter = new SortedHtmlDirectoryFormatter(), }); app.UseStaticFiles();