jellyfin/MediaBrowser.Server.Implementations/Channels/ChannelItemImageProvider.cs

83 lines
2.6 KiB
C#
Raw Normal View History

2014-03-17 18:45:41 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Channels
{
2014-09-10 18:57:11 -07:00
public class ChannelItemImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
2014-03-17 18:45:41 -07:00
{
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
public ChannelItemImageProvider(IHttpClient httpClient, ILogger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
return new[] { ImageType.Primary };
}
public async Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
{
var channelItem = (IChannelItem)item;
var imageResponse = new DynamicImageResponse();
if (!string.IsNullOrEmpty(channelItem.OriginalImageUrl))
{
var options = new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = channelItem.OriginalImageUrl
};
var response = await _httpClient.GetResponse(options).ConfigureAwait(false);
if (response.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
imageResponse.HasImage = true;
imageResponse.Stream = response.Content;
imageResponse.SetFormatFromMimeType(response.ContentType);
}
else
{
_logger.Error("Provider did not return an image content type.");
}
}
return imageResponse;
}
public string Name
{
get { return "Channel Image Provider"; }
}
public bool Supports(IHasImages item)
{
return item is IChannelItem;
}
2014-09-10 18:57:11 -07:00
public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
2014-03-17 18:45:41 -07:00
{
var channelItem = item as IChannelItem;
if (channelItem != null)
{
return !channelItem.HasImage(ImageType.Primary) && !string.IsNullOrWhiteSpace(channelItem.OriginalImageUrl);
}
return false;
}
}
}