Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Why use 300+ middleware when a single is fine.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Johnson committed Nov 28, 2021
1 parent eb46be4 commit f370c08
Showing 1 changed file with 9 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,61 +1,24 @@
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Umbraco.Docs.Preview.App.MiscellaneousOurStuff;
using Umbraco.Docs.Preview.App.Models;
using Microsoft.Extensions.Options;
using Umbraco.Docs.Preview.App.Options;

namespace Umbraco.Docs.Preview.App.Extensions
{
public static class UmbracoDocsApplicationBuilderExtensions
{
public static IApplicationBuilder UseUmbracoDocsImageFileProviders(this IApplicationBuilder app)
{
var serviceProvider = app.ApplicationServices;
var settings = app.ApplicationServices.GetRequiredService<IOptions<UmbracoDocsOptions>>();

var tree = serviceProvider.GetRequiredService<DocumentationUpdater>().BuildSitemap();

var log = serviceProvider
.GetRequiredService<ILoggerFactory>()
.CreateLogger(typeof(UmbracoDocsApplicationBuilderExtensions));

var total = AddImageFileProviders(tree, app, log);
log.LogInformation("Found {count} image directories for UmbracoDocs", total);

log.LogWarning("Images from new documentation sub directories will not be served without a restart");

return app;
}

private static int AddImageFileProviders(UmbracoDocsTreeNode node, IApplicationBuilder app, ILogger log)
{
var counter = 0;
var path = Path.Combine(node.PhysicalPath, "images");

var requestPath = $"/documentation/{node.Path}/images"
.Replace("//", "/"); // Hack for root UmbracoDocs folder

if (Directory.Exists(path))
app.UseStaticFiles(new StaticFileOptions
{
log.LogDebug("Adding file provider for {path}", path);

app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(path),
RequestPath = requestPath
});

++counter;
}
FileProvider = new PhysicalFileProvider(settings.Value.UmbracoDocsRootFolder),
RequestPath = "/documentation"
});

// Could add these to a composite?
foreach (var child in node.Directories)
{
counter += AddImageFileProviders(child, app, log);
}

return counter;
return app;
}
}
}

0 comments on commit f370c08

Please sign in to comment.