commit
9b1965b48a
@ -0,0 +1,27 @@
|
||||
using System.ComponentModel;
|
||||
using System.Net.Mime;
|
||||
|
||||
namespace MediaBrowser.Model.Drawing;
|
||||
|
||||
/// <summary>
|
||||
/// Extension class for the <see cref="ImageFormat" /> enum.
|
||||
/// </summary>
|
||||
public static class ImageFormatExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the correct mime type for this <see cref="ImageFormat" />.
|
||||
/// </summary>
|
||||
/// <param name="format">This <see cref="ImageFormat" />.</param>
|
||||
/// <exception cref="InvalidEnumArgumentException">The <paramref name="format"/> is an invalid enumeration value.</exception>
|
||||
/// <returns>The correct mime type for this <see cref="ImageFormat" />.</returns>
|
||||
public static string GetMimeType(this ImageFormat format)
|
||||
=> format switch
|
||||
{
|
||||
ImageFormat.Bmp => "image/bmp",
|
||||
ImageFormat.Gif => MediaTypeNames.Image.Gif,
|
||||
ImageFormat.Jpg => MediaTypeNames.Image.Jpeg,
|
||||
ImageFormat.Png => "image/png",
|
||||
ImageFormat.Webp => "image/webp",
|
||||
_ => throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ImageFormat))
|
||||
};
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using MediaBrowser.Model.Drawing;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Model.Drawing;
|
||||
|
||||
public static class ImageFormatExtensionsTests
|
||||
{
|
||||
private static TheoryData<ImageFormat> GetAllImageFormats()
|
||||
{
|
||||
var theoryTypes = new TheoryData<ImageFormat>();
|
||||
foreach (var x in Enum.GetValues<ImageFormat>())
|
||||
{
|
||||
theoryTypes.Add(x);
|
||||
}
|
||||
|
||||
return theoryTypes;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetAllImageFormats))]
|
||||
public static void GetMimeType_Valid_Valid(ImageFormat format)
|
||||
=> Assert.Null(Record.Exception(() => format.GetMimeType()));
|
||||
|
||||
[Theory]
|
||||
[InlineData((ImageFormat)int.MinValue)]
|
||||
[InlineData((ImageFormat)int.MaxValue)]
|
||||
[InlineData((ImageFormat)(-1))]
|
||||
[InlineData((ImageFormat)5)]
|
||||
public static void GetMimeType_Valid_ThrowsInvalidEnumArgumentException(ImageFormat format)
|
||||
=> Assert.Throws<InvalidEnumArgumentException>(() => format.GetMimeType());
|
||||
}
|
Loading…
Reference in new issue