From b0b6081bea5d58bc01dd368db2bc09a4c245626b Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Tue, 5 Jul 2022 15:24:53 +0200 Subject: [PATCH] Read BootFailed.html file from WebRootFileProvider (#12634) --- .../Middleware/BootFailedMiddleware.cs | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs b/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs index ea2c0e02e4..11abf725c2 100644 --- a/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs +++ b/src/Umbraco.Web.Common/Middleware/BootFailedMiddleware.cs @@ -2,31 +2,25 @@ using System.Text; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Exceptions; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Web.Common.DependencyInjection; -using Umbraco.Extensions; using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; namespace Umbraco.Cms.Web.Common.Middleware; /// -/// Executes when Umbraco booting fails in order to show the problem +/// Executes when Umbraco booting fails in order to show the problem. /// +/// public class BootFailedMiddleware : IMiddleware { private readonly IHostingEnvironment _hostingEnvironment; private readonly IWebHostEnvironment _webHostEnvironment; private readonly IRuntimeState _runtimeState; - public BootFailedMiddleware(IRuntimeState runtimeState, IHostingEnvironment hostingEnvironment) - : this(runtimeState, hostingEnvironment, StaticServiceProvider.Instance.GetRequiredService()) - { - _runtimeState = runtimeState; - _hostingEnvironment = hostingEnvironment; - } - public BootFailedMiddleware(IRuntimeState runtimeState, IHostingEnvironment hostingEnvironment, IWebHostEnvironment webHostEnvironment) { _runtimeState = runtimeState; @@ -34,6 +28,11 @@ public class BootFailedMiddleware : IMiddleware _webHostEnvironment = webHostEnvironment; } + [Obsolete("Use ctor with all params. This will be removed in Umbraco 12")] + public BootFailedMiddleware(IRuntimeState runtimeState, IHostingEnvironment hostingEnvironment) + : this(runtimeState, hostingEnvironment, StaticServiceProvider.Instance.GetRequiredService()) + { } + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { // TODO: It would be possible to redirect to the installer here in debug mode while @@ -53,10 +52,12 @@ public class BootFailedMiddleware : IMiddleware context.Response.Clear(); context.Response.StatusCode = 500; - var file = GetBootErrorFileName(); - - var viewContent = await File.ReadAllTextAsync(file); - await context.Response.WriteAsync(viewContent, Encoding.UTF8); + IFileInfo? fileInfo = GetBootErrorFileInfo(); + if (fileInfo is not null) + { + using var sr = new StreamReader(fileInfo.CreateReadStream(), Encoding.UTF8); + await context.Response.WriteAsync(await sr.ReadToEndAsync(), Encoding.UTF8); + } } } else @@ -65,14 +66,20 @@ public class BootFailedMiddleware : IMiddleware } } - private string GetBootErrorFileName() + private IFileInfo? GetBootErrorFileInfo() { - var fileName = _webHostEnvironment.MapPathWebRoot("~/config/errors/BootFailed.html"); - if (File.Exists(fileName)) + IFileInfo? fileInfo = _webHostEnvironment.WebRootFileProvider.GetFileInfo("config/errors/BootFailed.html"); + if (fileInfo.Exists) { - return fileName; + return fileInfo; } - return _webHostEnvironment.MapPathWebRoot("~/umbraco/views/errors/BootFailed.html"); + fileInfo = _webHostEnvironment.WebRootFileProvider.GetFileInfo("umbraco/views/errors/BootFailed.html"); + if (fileInfo.Exists) + { + return fileInfo; + } + + return null; } }