From b7ad9d539cc70d372143b64477878a47ef382496 Mon Sep 17 00:00:00 2001 From: jamesmoore Date: Fri, 21 Aug 2026 17:44:35 +0100 Subject: [PATCH] detect mime type --- SDMetaUI/Controllers/ImagesController.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/SDMetaUI/Controllers/ImagesController.cs b/SDMetaUI/Controllers/ImagesController.cs index 5e9a6c9..40d9c04 100644 --- a/SDMetaUI/Controllers/ImagesController.cs +++ b/SDMetaUI/Controllers/ImagesController.cs @@ -1,10 +1,13 @@ -using SDMetaUI.Services; +using Microsoft.AspNetCore.StaticFiles; +using SDMetaUI.Services; using System.IO.Abstractions; namespace SDMetaUI.Controllers { public class ImagesController() { + private static readonly FileExtensionContentTypeProvider ContentTypeProvider = new(); + public static IResult GetThumb( IFileSystem fileSystem, IThumbnailService thumbnailService, @@ -22,7 +25,7 @@ public static IResult GetThumb( var thumbPath = thumbnailService.GetOrGenerateThumbnail(fileInfo.FullName); httpResponse.Headers.LastModified = fileInfo.LastWriteTimeUtc.ToString("R"); - return Results.File(thumbPath, "image/jpg"); + return Results.File(thumbPath, GetContentType(thumbPath)); } else { @@ -49,7 +52,7 @@ public static IResult GetFull( { EnableCaching(httpResponse); httpResponse.Headers.LastModified = fileSystem.FileInfo.New(physicalPath).LastWriteTimeUtc.ToString("R"); - return Results.File(physicalPath, "image/png"); + return Results.File(physicalPath, GetContentType(physicalPath)); } else { @@ -69,6 +72,13 @@ private static string Base32Decode(string base32EncodedData) return System.Text.Encoding.UTF8.GetString(base32EncodedBytes); } + private static string GetContentType(string path) + { + return ContentTypeProvider.TryGetContentType(path, out var contentType) + ? contentType + : "application/octet-stream"; + } + private static void EnableCaching(HttpResponse httpResponse) { httpResponse.Headers.CacheControl = "public, max-age=31536000, immutable";