Read BootFailed.html file from WebRootFileProvider (#12634)

This commit is contained in:
Ronald Barendse
2022-07-05 15:24:53 +02:00
committed by GitHub
parent be38fb41ad
commit b0b6081bea

View File

@@ -2,31 +2,25 @@ using System.Text;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Umbraco.Cms.Core; using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Exceptions; using Umbraco.Cms.Core.Exceptions;
using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.DependencyInjection; using Umbraco.Cms.Web.Common.DependencyInjection;
using Umbraco.Extensions;
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
namespace Umbraco.Cms.Web.Common.Middleware; namespace Umbraco.Cms.Web.Common.Middleware;
/// <summary> /// <summary>
/// Executes when Umbraco booting fails in order to show the problem /// Executes when Umbraco booting fails in order to show the problem.
/// </summary> /// </summary>
/// <seealso cref="Microsoft.AspNetCore.Http.IMiddleware" />
public class BootFailedMiddleware : IMiddleware public class BootFailedMiddleware : IMiddleware
{ {
private readonly IHostingEnvironment _hostingEnvironment; private readonly IHostingEnvironment _hostingEnvironment;
private readonly IWebHostEnvironment _webHostEnvironment; private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IRuntimeState _runtimeState; private readonly IRuntimeState _runtimeState;
public BootFailedMiddleware(IRuntimeState runtimeState, IHostingEnvironment hostingEnvironment)
: this(runtimeState, hostingEnvironment, StaticServiceProvider.Instance.GetRequiredService<IWebHostEnvironment>())
{
_runtimeState = runtimeState;
_hostingEnvironment = hostingEnvironment;
}
public BootFailedMiddleware(IRuntimeState runtimeState, IHostingEnvironment hostingEnvironment, IWebHostEnvironment webHostEnvironment) public BootFailedMiddleware(IRuntimeState runtimeState, IHostingEnvironment hostingEnvironment, IWebHostEnvironment webHostEnvironment)
{ {
_runtimeState = runtimeState; _runtimeState = runtimeState;
@@ -34,6 +28,11 @@ public class BootFailedMiddleware : IMiddleware
_webHostEnvironment = webHostEnvironment; _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<IWebHostEnvironment>())
{ }
public async Task InvokeAsync(HttpContext context, RequestDelegate next) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
// TODO: It would be possible to redirect to the installer here in debug mode while // 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.Clear();
context.Response.StatusCode = 500; context.Response.StatusCode = 500;
var file = GetBootErrorFileName(); IFileInfo? fileInfo = GetBootErrorFileInfo();
if (fileInfo is not null)
var viewContent = await File.ReadAllTextAsync(file); {
await context.Response.WriteAsync(viewContent, Encoding.UTF8); using var sr = new StreamReader(fileInfo.CreateReadStream(), Encoding.UTF8);
await context.Response.WriteAsync(await sr.ReadToEndAsync(), Encoding.UTF8);
}
} }
} }
else else
@@ -65,14 +66,20 @@ public class BootFailedMiddleware : IMiddleware
} }
} }
private string GetBootErrorFileName() private IFileInfo? GetBootErrorFileInfo()
{ {
var fileName = _webHostEnvironment.MapPathWebRoot("~/config/errors/BootFailed.html"); IFileInfo? fileInfo = _webHostEnvironment.WebRootFileProvider.GetFileInfo("config/errors/BootFailed.html");
if (File.Exists(fileName)) 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;
} }
} }