Skip to content

Commit

Permalink
Refactor StyleSheetMiddleware for readability and modularity
Browse files Browse the repository at this point in the history
Refactored the `Invoke` method in `StyleSheetMiddleware` to improve readability and maintainability. Introduced two new private methods: `HandleDefaultPath` and `HandleContentCss`, which handle specific request paths. Updated path comparisons to use `string.Equals` with `StringComparison.OrdinalIgnoreCase` for case-insensitive comparison. Stored `requestPath` in a variable to avoid repeated calls. Encapsulated inline logic for custom and content CSS handling within new methods, enhancing modularity and testability.
  • Loading branch information
EdiWang committed Oct 31, 2024
1 parent 985577b commit b7ffd4c
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions src/Moonglade.Web/Middleware/StyleSheetMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,61 @@ public class StyleSheetMiddleware(RequestDelegate next)

public async Task Invoke(HttpContext context, IBlogConfig blogConfig, IMediator mediator)
{
if (!context.Request.Path.ToString().ToLower().EndsWith(".css"))
var requestPath = context.Request.Path.ToString().ToLowerInvariant();

if (!requestPath.EndsWith(".css"))
{
await next(context);
return;
}

if (context.Request.Path == Options.DefaultPath)
if (string.Equals(requestPath, Options.DefaultPath, StringComparison.OrdinalIgnoreCase))
{
if (!blogConfig.CustomStyleSheetSettings.EnableCustomCss)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}

var cssCode = blogConfig.CustomStyleSheetSettings.CssCode;
await WriteStyleSheet(context, cssCode);
await HandleDefaultPath(context, blogConfig);
}
else if (context.Request.Path == "/content.css" && context.Request.QueryString.HasValue)
else if (string.Equals(requestPath, "/content.css", StringComparison.OrdinalIgnoreCase) && context.Request.QueryString.HasValue)
{
// Get query string value
var qs = HttpUtility.ParseQueryString(context.Request.QueryString.Value!);
var id = qs["id"];

if (!string.IsNullOrWhiteSpace(id))
{
if (!Guid.TryParse(id, out var guid))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
}
else
{
var css = await mediator.Send(new GetStyleSheetQuery(guid));
if (css == null)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}

// TODO: May need a server side cache
await WriteStyleSheet(context, css.CssContent);
}
}
else
{
await next(context);
}
await HandleContentCss(context, mediator);
}
else
{
await next(context);
}
}

private async Task HandleDefaultPath(HttpContext context, IBlogConfig blogConfig)
{
if (!blogConfig.CustomStyleSheetSettings.EnableCustomCss)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}

var cssCode = blogConfig.CustomStyleSheetSettings.CssCode;
await WriteStyleSheet(context, cssCode);
}

private async Task HandleContentCss(HttpContext context, IMediator mediator)
{
var qs = HttpUtility.ParseQueryString(context.Request.QueryString.Value!);
var id = qs["id"];

if (string.IsNullOrWhiteSpace(id) || !Guid.TryParse(id, out var guid))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}

var css = await mediator.Send(new GetStyleSheetQuery(guid));
if (css == null)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}

await WriteStyleSheet(context, css.CssContent);
}

private static async Task WriteStyleSheet(HttpContext context, string cssCode)
{
if (cssCode.Length > Options.MaxContentLength)
Expand Down

0 comments on commit b7ffd4c

Please sign in to comment.