| | | 1 | | // File: Middleware/FaviconMiddlewareExtensions.cs |
| | | 2 | | using Microsoft.AspNetCore.StaticFiles; |
| | | 3 | | using Serilog; |
| | | 4 | | |
| | | 5 | | namespace Kestrun.Middleware; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for serving a favicon in ASP.NET Core applications. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class FaviconMiddlewareExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds middleware to serve a favicon for the application. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="app">The application builder.</param> |
| | | 16 | | /// <param name="iconPath">Optional path to a custom favicon file. If not provided, uses the embedded favicon.</para |
| | | 17 | | /// <returns>The application builder.</returns> |
| | | 18 | | public static IApplicationBuilder UseFavicon(this IApplicationBuilder app, string? iconPath = null) |
| | | 19 | | { |
| | 4 | 20 | | if (Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 21 | | { |
| | 4 | 22 | | Log.Debug("Using favicon middleware, iconPath={IconPath}", iconPath); |
| | | 23 | | } |
| | | 24 | | |
| | 4 | 25 | | ArgumentNullException.ThrowIfNull(app); |
| | | 26 | | |
| | | 27 | | // MIME-type detection |
| | 4 | 28 | | var contentTypeProvider = new FileExtensionContentTypeProvider(); |
| | | 29 | | string? contentType; |
| | | 30 | | byte[] iconBytes; |
| | | 31 | | |
| | | 32 | | // Check if user provided a custom icon path |
| | 4 | 33 | | if (!string.IsNullOrWhiteSpace(iconPath) && File.Exists(iconPath)) |
| | | 34 | | { |
| | 2 | 35 | | if (Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 36 | | { |
| | 2 | 37 | | Log.Debug("Using user-provided favicon at {IconPath}", iconPath); |
| | | 38 | | } |
| | | 39 | | // Serve user-provided file |
| | 2 | 40 | | iconBytes = File.ReadAllBytes(iconPath); |
| | | 41 | | |
| | 2 | 42 | | if (!contentTypeProvider.TryGetContentType(iconPath, out contentType) || contentType is null) |
| | | 43 | | { |
| | 1 | 44 | | contentType = "application/octet-stream"; // fallback |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | else |
| | | 48 | | { |
| | 2 | 49 | | if (Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 50 | | { |
| | 2 | 51 | | Log.Debug("Using embedded favicon, no custom path provided"); |
| | | 52 | | } |
| | | 53 | | // Fallback to embedded .ico |
| | 2 | 54 | | var asm = typeof(FaviconMiddlewareExtensions).Assembly; |
| | | 55 | | const string embedded = "Kestrun.Assets.favicon.ico"; |
| | 2 | 56 | | using var stream = asm.GetManifestResourceStream(embedded) |
| | 2 | 57 | | ?? throw new InvalidOperationException($"Embedded favicon not found: {embedded}"); |
| | 2 | 58 | | using var ms = new MemoryStream(); |
| | 2 | 59 | | stream.CopyTo(ms); |
| | 2 | 60 | | iconBytes = ms.ToArray(); |
| | 2 | 61 | | contentType = "image/x-icon"; |
| | | 62 | | } |
| | | 63 | | |
| | 4 | 64 | | var headers = new HeaderDictionary |
| | 4 | 65 | | { |
| | 4 | 66 | | ["Content-Type"] = contentType, |
| | 4 | 67 | | ["Cache-Control"] = "public,max-age=31536000" |
| | 4 | 68 | | }; |
| | 4 | 69 | | if (Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 70 | | { |
| | 4 | 71 | | Log.Debug("Favicon content type: {ContentType}, size={Size} bytes", contentType, iconBytes.Length); |
| | | 72 | | } |
| | | 73 | | |
| | 4 | 74 | | return app.Map("/favicon.ico", branch => |
| | 4 | 75 | | { |
| | 4 | 76 | | branch.Run(async ctx => |
| | 4 | 77 | | { |
| | 4 | 78 | | if (Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | 4 | 79 | | { |
| | 4 | 80 | | Log.Debug("Serving favicon.ico, size={Size} bytes", iconBytes.Length); |
| | 4 | 81 | | } |
| | 4 | 82 | | |
| | 4 | 83 | | ctx.Response.StatusCode = 200; |
| | 24 | 84 | | foreach (var kv in headers) |
| | 4 | 85 | | { |
| | 8 | 86 | | ctx.Response.Headers[kv.Key] = kv.Value; |
| | 4 | 87 | | } |
| | 4 | 88 | | |
| | 4 | 89 | | await ctx.Response.Body.WriteAsync(iconBytes); |
| | 8 | 90 | | }); |
| | 8 | 91 | | }); |
| | | 92 | | } |
| | | 93 | | } |