Renames the image MIME resolver and organizes the MIME resolvers

Renames MimeTypes to ImageMimeTypes and moves it into Common/MimeTypes, updates the thumbnail endpoint and its tests, and aligns ThemeMimeTypes on the same frozen dictionary, initializer style and field naming.
This commit is contained in:
BinaryAlley committed 2026-10-13 22:16:57 +03:00
1 parent 1aa427b1e2
commit 4bcd37898c
5 files changed
+33 -32

No files matched your search

@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
#endregion
@@ -11,25 +12,25 @@ namespace Lumina.Infrastructure.Core.Themes;
/// </summary>
internal static class ThemeMimeTypes
{
private static readonly Dictionary<string, string> s_mimeByExtension = new(StringComparer.OrdinalIgnoreCase)
private static readonly FrozenDictionary<string, string> s_fileExtensionToMimeType = new Dictionary<string, string>
{
[".htm"] = "text/html",
[".html"] = "text/html",
[".css"] = "text/css",
[".js"] = "application/javascript",
[".json"] = "application/json",
[".png"] = "image/png",
[".jpg"] = "image/jpeg",
[".jpeg"] = "image/jpeg",
[".gif"] = "image/gif",
[".webp"] = "image/webp",
[".svg"] = "image/svg+xml",
[".ico"] = "image/x-icon",
[".woff"] = "font/woff",
[".woff2"] = "font/woff2",
[".ttf"] = "font/ttf",
[".txt"] = "text/plain"
};
{ ".htm", "text/html" },
{ ".html", "text/html" },
{ ".css", "text/css" },
{ ".js", "application/javascript" },
{ ".json", "application/json" },
{ ".png", "image/png" },
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".gif", "image/gif" },
{ ".webp", "image/webp" },
{ ".svg", "image/svg+xml" },
{ ".ico", "image/x-icon" },
{ ".woff", "font/woff" },
{ ".woff2", "font/woff2" },
{ ".ttf", "font/ttf" },
{ ".txt", "text/plain" }
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the MIME content type of a file based on its extension.
@@ -38,6 +39,6 @@ internal static class ThemeMimeTypes
/// <returns>The MIME content type, or <c>application/octet-stream</c> when the extension is unknown.</returns>
public static string GetMimeType(string filePath)
{
return s_mimeByExtension.TryGetValue(Path.GetExtension(filePath), out string? mimeType) ? mimeType : "application/octet-stream";
return s_fileExtensionToMimeType.TryGetValue(Path.GetExtension(filePath), out string? mimeType) ? mimeType : "application/octet-stream";
}
}
@@ -4,12 +4,12 @@ using System.Collections.Frozen;
using System.Collections.Generic;
#endregion
namespace Lumina.Presentation.Api.Common.Utilities;
namespace Lumina.Presentation.Api.Common.MimeTypes;
/// <summary>
/// Utility class for converting from domain image types to known image mime types.
/// Resolves the MIME content type of image types.
/// </summary>
public static class MimeTypes
public static class ImageMimeTypes
{
private static readonly FrozenDictionary<ImageType, string> s_imageTypeToMimeType = new Dictionary<ImageType, string>
{
@@ -7,8 +7,8 @@ using Lumina.Application.Core.FileSystemManagement.Thumbnails.Queries.GetThumbna
using Lumina.Contracts.Requests.FileSystemManagement.Thumbnails;
using Lumina.Contracts.Responses.FileSystemManagement.Thumbnails;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Common.MimeTypes;
using Lumina.Presentation.Api.Common.Routes.FileSystemManagement;
using Lumina.Presentation.Api.Common.Utilities;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Microsoft.AspNetCore.Http;
using System.Threading;
@@ -52,6 +52,6 @@ public class GetThumbnailEndpoint : BaseEndpoint<GetThumbnailRequest, IResult>
public override async Task<IResult> ExecuteAsync(GetThumbnailRequest request, CancellationToken cancellationToken)
{
Result<ThumbnailResponse> result = await _getThumbnailQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.File(success.Bytes, MimeTypes.GetMimeType(success.Type)), Problem);
return result.Match(success => TypedResults.File(success.Bytes, ImageMimeTypes.GetMimeType(success.Type)), Problem);
}
}
@@ -1,16 +1,16 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.SharedKernel.Common.Enums.PhotoLibrary;
using Lumina.Presentation.Api.Common.Utilities;
using Lumina.Presentation.Api.Common.MimeTypes;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Common.Utilities;
namespace Lumina.Presentation.Api.UnitTests.Common.MimeTypes;
/// <summary>
/// Contains unit tests for the <see cref="MimeTypes"/> class.
/// Contains unit tests for the <see cref="ImageMimeTypes"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class MimeTypesTests
public class ImageMimeTypesTests
{
[Theory]
[InlineData(ImageType.BMP, "image/bmp")] // bmp image type
@@ -21,7 +21,7 @@ public class MimeTypesTests
public void GetMimeType_WhenTypeIsKnown_ShouldReturnTheMappedMimeType(ImageType type, string expectedMimeType)
{
// Act
string result = MimeTypes.GetMimeType(type);
string result = ImageMimeTypes.GetMimeType(type);
// Assert
Assert.Equal(expectedMimeType, result);
@@ -33,7 +33,7 @@ public class MimeTypesTests
public void GetMimeType_WhenTypeIsUnknown_ShouldReturnOpaqueBinaryMimeType(ImageType type)
{
// Act
string result = MimeTypes.GetMimeType(type);
string result = ImageMimeTypes.GetMimeType(type);
// Assert
Assert.Equal("application/octet-stream", result);
@@ -7,7 +7,7 @@ using Lumina.Contracts.Fixtures.Core.Responses.FileSystemManagement.Thumbnails;
using Lumina.Contracts.Requests.FileSystemManagement.Thumbnails;
using Lumina.Contracts.Responses.FileSystemManagement.Thumbnails;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Common.Utilities;
using Lumina.Presentation.Api.Common.MimeTypes;
using Lumina.Presentation.Api.Core.Endpoints.FileSystemManagement.Thumbnails.GetThumbnail;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
@@ -56,7 +56,7 @@ public class GetThumbnailEndpointTests
// Assert
FileContentHttpResult fileResult = Assert.IsType<FileContentHttpResult>(result);
Assert.Equal(expectedResponse.Bytes, fileResult.FileContents.ToArray());
Assert.Equal(MimeTypes.GetMimeType(expectedResponse.Type), fileResult.ContentType);
Assert.Equal(ImageMimeTypes.GetMimeType(expectedResponse.Type), fileResult.ContentType);
}
[Fact]