Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions SDMetaUI/Controllers/ImagesController.cs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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";
Expand Down
Loading