Add logging and fast return

This commit is contained in:
Joe Rogers 2021-11-15 15:30:43 +01:00
parent 370b7f8e12
commit f059be8e4d
No known key found for this signature in database
GPG Key ID: 0074AD57B8FDBBB4
2 changed files with 16 additions and 5 deletions

View File

@ -15,6 +15,7 @@ using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.MediaInfo namespace MediaBrowser.Providers.MediaInfo
{ {
@ -45,14 +46,17 @@ namespace MediaBrowser.Providers.MediaInfo
}; };
private readonly IMediaEncoder _mediaEncoder; private readonly IMediaEncoder _mediaEncoder;
private readonly ILogger<EmbeddedImageProvider> _logger;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class. /// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class.
/// </summary> /// </summary>
/// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param> /// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param>
public EmbeddedImageProvider(IMediaEncoder mediaEncoder) /// <param name="logger">The logger.</param>
public EmbeddedImageProvider(IMediaEncoder mediaEncoder, ILogger<EmbeddedImageProvider> logger)
{ {
_mediaEncoder = mediaEncoder; _mediaEncoder = mediaEncoder;
_logger = logger;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -117,6 +121,12 @@ namespace MediaBrowser.Providers.MediaInfo
_ => Array.Empty<string>() _ => Array.Empty<string>()
}; };
if (imageFileNames.Length == 0)
{
_logger.LogWarning("Attempted to load unexpected image type: {Type}", type);
return new DynamicImageResponse { HasImage = false };
}
// Try attachments first // Try attachments first
var attachmentStream = item.GetMediaSources(false) var attachmentStream = item.GetMediaSources(false)
.SelectMany(source => source.MediaAttachments) .SelectMany(source => source.MediaAttachments)

View File

@ -11,6 +11,7 @@ using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Providers.MediaInfo; using MediaBrowser.Providers.MediaInfo;
using Microsoft.Extensions.Logging.Abstractions;
using Moq; using Moq;
using Xunit; using Xunit;
@ -28,7 +29,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
public void GetSupportedImages_AnyBaseItem_ReturnsExpected(Type type, params ImageType[] expected) public void GetSupportedImages_AnyBaseItem_ReturnsExpected(Type type, params ImageType[] expected)
{ {
BaseItem item = (BaseItem)Activator.CreateInstance(type)!; BaseItem item = (BaseItem)Activator.CreateInstance(type)!;
var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of<IMediaEncoder>()); var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of<IMediaEncoder>(), new NullLogger<EmbeddedImageProvider>());
var actual = embeddedImageProvider.GetSupportedImages(item); var actual = embeddedImageProvider.GetSupportedImages(item);
Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString())); Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString()));
} }
@ -36,7 +37,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
[Fact] [Fact]
public async void GetImage_NoStreams_ReturnsNoImage() public async void GetImage_NoStreams_ReturnsNoImage()
{ {
var embeddedImageProvider = new EmbeddedImageProvider(null); var embeddedImageProvider = new EmbeddedImageProvider(null, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie(new List<MediaAttachment>(), new List<MediaStream>()); var input = GetMovie(new List<MediaAttachment>(), new List<MediaStream>());
@ -69,7 +70,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict); var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>())) mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
.Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext)); .Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext));
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object); var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie(attachments, new List<MediaStream>()); var input = GetMovie(attachments, new List<MediaStream>());
@ -119,7 +120,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
Assert.Equal(streams[index - 1], stream); Assert.Equal(streams[index - 1], stream);
return Task.FromResult(pathPrefix + index + "." + ext); return Task.FromResult(pathPrefix + index + "." + ext);
}); });
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object); var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie(new List<MediaAttachment>(), streams); var input = GetMovie(new List<MediaAttachment>(), streams);