mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
Fix AdminStaticFilesMiddleware to use simple file serving instead of internal ASP.NET classes
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
using Microsoft.AspNetCore.StaticFiles;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
namespace allstarr.Middleware;
|
namespace allstarr.Middleware;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -7,30 +10,15 @@ namespace allstarr.Middleware;
|
|||||||
public class AdminStaticFilesMiddleware
|
public class AdminStaticFilesMiddleware
|
||||||
{
|
{
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly StaticFileMiddleware _staticFileMiddleware;
|
private readonly IWebHostEnvironment _env;
|
||||||
private readonly DefaultFilesMiddleware _defaultFilesMiddleware;
|
|
||||||
private const int AdminPort = 5275;
|
private const int AdminPort = 5275;
|
||||||
|
|
||||||
public AdminStaticFilesMiddleware(
|
public AdminStaticFilesMiddleware(
|
||||||
RequestDelegate next,
|
RequestDelegate next,
|
||||||
IWebHostEnvironment env,
|
IWebHostEnvironment env)
|
||||||
ILoggerFactory loggerFactory)
|
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
|
_env = env;
|
||||||
var staticFileOptions = new StaticFileOptions();
|
|
||||||
var defaultFilesOptions = new DefaultFilesOptions();
|
|
||||||
|
|
||||||
_staticFileMiddleware = new StaticFileMiddleware(
|
|
||||||
_next,
|
|
||||||
env,
|
|
||||||
Microsoft.Extensions.Options.Options.Create(staticFileOptions),
|
|
||||||
loggerFactory);
|
|
||||||
|
|
||||||
_defaultFilesMiddleware = new DefaultFilesMiddleware(
|
|
||||||
(ctx) => _staticFileMiddleware.Invoke(ctx),
|
|
||||||
env,
|
|
||||||
Microsoft.Extensions.Options.Options.Create(defaultFilesOptions));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task InvokeAsync(HttpContext context)
|
public async Task InvokeAsync(HttpContext context)
|
||||||
@@ -39,13 +27,50 @@ public class AdminStaticFilesMiddleware
|
|||||||
|
|
||||||
if (port == AdminPort)
|
if (port == AdminPort)
|
||||||
{
|
{
|
||||||
// Serve static files on admin port
|
var path = context.Request.Path.Value ?? "/";
|
||||||
await _defaultFilesMiddleware.Invoke(context);
|
|
||||||
|
// Serve index.html for root path
|
||||||
|
if (path == "/" || path == "/index.html")
|
||||||
|
{
|
||||||
|
var indexPath = Path.Combine(_env.WebRootPath, "index.html");
|
||||||
|
if (File.Exists(indexPath))
|
||||||
|
{
|
||||||
|
context.Response.ContentType = "text/html";
|
||||||
|
await context.Response.SendFileAsync(indexPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to serve static file from wwwroot
|
||||||
|
var filePath = Path.Combine(_env.WebRootPath, path.TrimStart('/'));
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
var contentType = GetContentType(filePath);
|
||||||
|
context.Response.ContentType = contentType;
|
||||||
|
await context.Response.SendFileAsync(filePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// Not admin port or file not found - continue pipeline
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetContentType(string filePath)
|
||||||
|
{
|
||||||
|
var ext = Path.GetExtension(filePath).ToLowerInvariant();
|
||||||
|
return ext switch
|
||||||
{
|
{
|
||||||
// Skip static files on main port
|
".html" => "text/html",
|
||||||
await _next(context);
|
".css" => "text/css",
|
||||||
}
|
".js" => "application/javascript",
|
||||||
|
".json" => "application/json",
|
||||||
|
".png" => "image/png",
|
||||||
|
".jpg" or ".jpeg" => "image/jpeg",
|
||||||
|
".gif" => "image/gif",
|
||||||
|
".svg" => "image/svg+xml",
|
||||||
|
".ico" => "image/x-icon",
|
||||||
|
_ => "application/octet-stream"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user