Merge pull request #4259 from ConfusedPolarBear/imageFormat

Accept ImageFormat as API parameter
This commit is contained in:
Anthony Lavado 2020-10-09 20:12:11 -04:00 committed by GitHub
commit ea5c78ee21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -333,7 +333,7 @@ namespace Jellyfin.Api.Controllers
/// <param name="quality">Optional. Quality setting, from 0-100. Defaults to 90 and should suffice in most cases.</param> /// <param name="quality">Optional. Quality setting, from 0-100. Defaults to 90 and should suffice in most cases.</param>
/// <param name="tag">Optional. Supply the cache tag from the item object to receive strong caching headers.</param> /// <param name="tag">Optional. Supply the cache tag from the item object to receive strong caching headers.</param>
/// <param name="cropWhitespace">Optional. Specify if whitespace should be cropped out of the image. True/False. If unspecified, whitespace will be cropped from logos and clear art.</param> /// <param name="cropWhitespace">Optional. Specify if whitespace should be cropped out of the image. True/False. If unspecified, whitespace will be cropped from logos and clear art.</param>
/// <param name="format">Determines the output format of the image - original,gif,jpg,png.</param> /// <param name="format">Optional. The <see cref="ImageFormat"/> of the returned image.</param>
/// <param name="addPlayedIndicator">Optional. Add a played indicator.</param> /// <param name="addPlayedIndicator">Optional. Add a played indicator.</param>
/// <param name="percentPlayed">Optional. Percent to render for the percent played overlay.</param> /// <param name="percentPlayed">Optional. Percent to render for the percent played overlay.</param>
/// <param name="unplayedCount">Optional. Unplayed count overlay to render.</param> /// <param name="unplayedCount">Optional. Unplayed count overlay to render.</param>
@ -364,7 +364,7 @@ namespace Jellyfin.Api.Controllers
[FromQuery] int? quality, [FromQuery] int? quality,
[FromQuery] string? tag, [FromQuery] string? tag,
[FromQuery] bool? cropWhitespace, [FromQuery] bool? cropWhitespace,
[FromQuery] string? format, [FromQuery] ImageFormat? format,
[FromQuery] bool? addPlayedIndicator, [FromQuery] bool? addPlayedIndicator,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
[FromQuery] int? unplayedCount, [FromQuery] int? unplayedCount,
@ -443,7 +443,7 @@ namespace Jellyfin.Api.Controllers
[FromQuery] int? quality, [FromQuery] int? quality,
[FromRoute, Required] string tag, [FromRoute, Required] string tag,
[FromQuery] bool? cropWhitespace, [FromQuery] bool? cropWhitespace,
[FromRoute, Required] string format, [FromRoute, Required] ImageFormat format,
[FromQuery] bool? addPlayedIndicator, [FromQuery] bool? addPlayedIndicator,
[FromRoute, Required] double percentPlayed, [FromRoute, Required] double percentPlayed,
[FromRoute, Required] int unplayedCount, [FromRoute, Required] int unplayedCount,
@ -516,7 +516,7 @@ namespace Jellyfin.Api.Controllers
[FromRoute, Required] string name, [FromRoute, Required] string name,
[FromRoute, Required] ImageType imageType, [FromRoute, Required] ImageType imageType,
[FromQuery] string tag, [FromQuery] string tag,
[FromQuery] string format, [FromQuery] ImageFormat? format,
[FromQuery] int? maxWidth, [FromQuery] int? maxWidth,
[FromQuery] int? maxHeight, [FromQuery] int? maxHeight,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
@ -595,7 +595,7 @@ namespace Jellyfin.Api.Controllers
[FromRoute, Required] string name, [FromRoute, Required] string name,
[FromRoute, Required] ImageType imageType, [FromRoute, Required] ImageType imageType,
[FromQuery] string tag, [FromQuery] string tag,
[FromQuery] string format, [FromQuery] ImageFormat? format,
[FromQuery] int? maxWidth, [FromQuery] int? maxWidth,
[FromQuery] int? maxHeight, [FromQuery] int? maxHeight,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
@ -674,7 +674,7 @@ namespace Jellyfin.Api.Controllers
[FromRoute, Required] string name, [FromRoute, Required] string name,
[FromRoute, Required] ImageType imageType, [FromRoute, Required] ImageType imageType,
[FromQuery] string tag, [FromQuery] string tag,
[FromQuery] string format, [FromQuery] ImageFormat? format,
[FromQuery] int? maxWidth, [FromQuery] int? maxWidth,
[FromQuery] int? maxHeight, [FromQuery] int? maxHeight,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
@ -753,7 +753,7 @@ namespace Jellyfin.Api.Controllers
[FromRoute, Required] string name, [FromRoute, Required] string name,
[FromRoute, Required] ImageType imageType, [FromRoute, Required] ImageType imageType,
[FromQuery] string tag, [FromQuery] string tag,
[FromQuery] string format, [FromQuery] ImageFormat? format,
[FromQuery] int? maxWidth, [FromQuery] int? maxWidth,
[FromQuery] int? maxHeight, [FromQuery] int? maxHeight,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
@ -832,7 +832,7 @@ namespace Jellyfin.Api.Controllers
[FromRoute, Required] string name, [FromRoute, Required] string name,
[FromRoute, Required] ImageType imageType, [FromRoute, Required] ImageType imageType,
[FromRoute, Required] string tag, [FromRoute, Required] string tag,
[FromRoute, Required] string format, [FromRoute, Required] ImageFormat format,
[FromQuery] int? maxWidth, [FromQuery] int? maxWidth,
[FromQuery] int? maxHeight, [FromQuery] int? maxHeight,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
@ -911,7 +911,7 @@ namespace Jellyfin.Api.Controllers
[FromRoute, Required] Guid userId, [FromRoute, Required] Guid userId,
[FromRoute, Required] ImageType imageType, [FromRoute, Required] ImageType imageType,
[FromQuery] string? tag, [FromQuery] string? tag,
[FromQuery] string? format, [FromQuery] ImageFormat? format,
[FromQuery] int? maxWidth, [FromQuery] int? maxWidth,
[FromQuery] int? maxHeight, [FromQuery] int? maxHeight,
[FromQuery] double? percentPlayed, [FromQuery] double? percentPlayed,
@ -1038,7 +1038,7 @@ namespace Jellyfin.Api.Controllers
ImageType imageType, ImageType imageType,
int? imageIndex, int? imageIndex,
string? tag, string? tag,
string? format, ImageFormat? format,
int? maxWidth, int? maxWidth,
int? maxHeight, int? maxHeight,
double? percentPlayed, double? percentPlayed,
@ -1128,12 +1128,11 @@ namespace Jellyfin.Api.Controllers
isHeadRequest).ConfigureAwait(false); isHeadRequest).ConfigureAwait(false);
} }
private ImageFormat[] GetOutputFormats(string? format) private ImageFormat[] GetOutputFormats(ImageFormat? format)
{ {
if (!string.IsNullOrWhiteSpace(format) if (format.HasValue)
&& Enum.TryParse(format, true, out ImageFormat parsedFormat))
{ {
return new[] { parsedFormat }; return new[] { format.Value };
} }
return GetClientSupportedFormats(); return GetClientSupportedFormats();
@ -1157,7 +1156,7 @@ namespace Jellyfin.Api.Controllers
var acceptParam = Request.Query[HeaderNames.Accept]; var acceptParam = Request.Query[HeaderNames.Accept];
var supportsWebP = SupportsFormat(supportedFormats, acceptParam, "webp", false); var supportsWebP = SupportsFormat(supportedFormats, acceptParam, ImageFormat.Webp, false);
if (!supportsWebP) if (!supportsWebP)
{ {
@ -1179,7 +1178,7 @@ namespace Jellyfin.Api.Controllers
formats.Add(ImageFormat.Jpg); formats.Add(ImageFormat.Jpg);
formats.Add(ImageFormat.Png); formats.Add(ImageFormat.Png);
if (SupportsFormat(supportedFormats, acceptParam, "gif", true)) if (SupportsFormat(supportedFormats, acceptParam, ImageFormat.Gif, true))
{ {
formats.Add(ImageFormat.Gif); formats.Add(ImageFormat.Gif);
} }
@ -1187,9 +1186,10 @@ namespace Jellyfin.Api.Controllers
return formats.ToArray(); return formats.ToArray();
} }
private bool SupportsFormat(IReadOnlyCollection<string> requestAcceptTypes, string acceptParam, string format, bool acceptAll) private bool SupportsFormat(IReadOnlyCollection<string> requestAcceptTypes, string acceptParam, ImageFormat format, bool acceptAll)
{ {
var mimeType = "image/" + format; var normalized = format.ToString().ToLowerInvariant();
var mimeType = "image/" + normalized;
if (requestAcceptTypes.Contains(mimeType)) if (requestAcceptTypes.Contains(mimeType))
{ {
@ -1201,7 +1201,7 @@ namespace Jellyfin.Api.Controllers
return true; return true;
} }
return string.Equals(acceptParam, format, StringComparison.OrdinalIgnoreCase); return string.Equals(acceptParam, normalized, StringComparison.OrdinalIgnoreCase);
} }
private async Task<ActionResult> GetImageResult( private async Task<ActionResult> GetImageResult(