This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Why use 300+ middleware when a single is fine.
- Loading branch information
Paul Johnson
committed
Nov 28, 2021
1 parent
eb46be4
commit f370c08
Showing
1 changed file
with
9 additions
and
46 deletions.
There are no files selected for viewing
55 changes: 9 additions & 46 deletions
55
src/Umbraco.Docs.Preview.App/Extensions/UmbracoDocsApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |