jellyfin/MediaBrowser.Server.Implementations/LiveTv/ChannelImageProvider.cs

121 lines
4.1 KiB
C#
Raw Normal View History

2015-07-20 11:32:55 -07:00
using MediaBrowser.Common;
using MediaBrowser.Common.Net;
2013-11-24 13:51:45 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
2013-11-24 13:51:45 -07:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.LiveTv
{
2014-09-10 18:57:11 -07:00
public class ChannelImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
2013-11-24 13:51:45 -07:00
{
private readonly ILiveTvManager _liveTvManager;
2013-12-19 14:51:32 -07:00
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
2015-07-20 11:32:55 -07:00
private readonly IApplicationHost _appHost;
2013-11-24 13:51:45 -07:00
2015-07-20 11:32:55 -07:00
public ChannelImageProvider(ILiveTvManager liveTvManager, IHttpClient httpClient, ILogger logger, IApplicationHost appHost)
2013-11-24 13:51:45 -07:00
{
_liveTvManager = liveTvManager;
2013-12-19 14:51:32 -07:00
_httpClient = httpClient;
_logger = logger;
2015-07-20 11:32:55 -07:00
_appHost = appHost;
2013-11-24 13:51:45 -07:00
}
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
2013-11-24 13:51:45 -07:00
{
return new[] { ImageType.Primary };
2013-11-24 13:51:45 -07:00
}
public async Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
2013-11-24 13:51:45 -07:00
{
var liveTvItem = (LiveTvChannel)item;
2013-11-24 13:51:45 -07:00
var imageResponse = new DynamicImageResponse();
2013-11-24 13:51:45 -07:00
2015-10-04 11:10:50 -07:00
if (!string.IsNullOrEmpty(liveTvItem.ExternalImagePath))
2013-11-24 13:51:45 -07:00
{
2015-10-04 11:10:50 -07:00
if (liveTvItem.ExternalImagePath.StartsWith("http", StringComparison.OrdinalIgnoreCase))
2013-12-19 14:51:32 -07:00
{
2015-10-04 11:10:50 -07:00
var options = new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = liveTvItem.ExternalImagePath,
2013-12-19 14:51:32 -07:00
2015-10-04 11:10:50 -07:00
// Some image hosts require a user agent to be specified.
UserAgent = "Emby Server/" + _appHost.ApplicationVersion
};
2013-11-24 13:51:45 -07:00
2015-10-04 11:10:50 -07:00
var response = await _httpClient.GetResponse(options).ConfigureAwait(false);
2015-03-29 20:04:55 -07:00
2015-10-04 11:10:50 -07:00
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.");
}
}
else
{
2015-10-04 11:10:50 -07:00
imageResponse.Path = liveTvItem.ExternalImagePath;
imageResponse.HasImage = true;
2013-12-19 14:51:32 -07:00
}
}
2015-10-04 11:10:50 -07:00
else
2013-12-19 14:51:32 -07:00
{
var service = _liveTvManager.Services.FirstOrDefault(i => string.Equals(i.Name, liveTvItem.ServiceName, StringComparison.OrdinalIgnoreCase));
2013-12-19 14:51:32 -07:00
if (service != null)
{
2013-12-22 10:16:24 -07:00
try
{
var response = await service.GetChannelImageAsync(liveTvItem.ExternalId, cancellationToken).ConfigureAwait(false);
2013-12-22 10:16:24 -07:00
if (response != null)
{
imageResponse.HasImage = true;
imageResponse.Stream = response.Stream;
imageResponse.Format = response.Format;
2013-12-22 10:16:24 -07:00
}
}
catch (NotImplementedException)
{
}
2013-12-19 14:51:32 -07:00
}
}
return imageResponse;
}
2013-11-24 13:51:45 -07:00
public string Name
{
get { return "Live TV Service Provider"; }
}
2013-12-22 10:16:24 -07:00
public bool Supports(IHasImages item)
{
return item is LiveTvChannel;
2013-11-24 13:51:45 -07:00
}
public int Order
2013-11-24 13:51:45 -07:00
{
get { return 0; }
2013-11-24 13:51:45 -07:00
}
2014-09-10 18:57:11 -07:00
public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
2013-11-24 13:51:45 -07:00
{
2014-10-08 16:31:44 -07:00
return GetSupportedImages(item).Any(i => !item.HasImage(i));
2013-11-24 13:51:45 -07:00
}
}
}