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

157 lines
5.3 KiB
C#
Raw Normal View History

2013-12-15 01:17:57 +00:00
using MediaBrowser.Common.IO;
2013-12-19 21:51:32 +00:00
using MediaBrowser.Common.Net;
2013-12-15 01:17:57 +00:00
using MediaBrowser.Controller.Configuration;
2013-11-24 20:51:45 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using System;
2013-12-15 01:17:57 +00:00
using System.IO;
2013-11-24 20:51:45 +00:00
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.LiveTv
{
public class ChannelImageProvider : BaseMetadataProvider
{
private readonly ILiveTvManager _liveTvManager;
private readonly IProviderManager _providerManager;
2013-12-15 01:17:57 +00:00
private readonly IFileSystem _fileSystem;
2013-12-19 21:51:32 +00:00
private readonly IHttpClient _httpClient;
2013-11-24 20:51:45 +00:00
2013-12-19 21:51:32 +00:00
public ChannelImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, ILiveTvManager liveTvManager, IProviderManager providerManager, IFileSystem fileSystem, IHttpClient httpClient)
2013-11-24 20:51:45 +00:00
: base(logManager, configurationManager)
{
_liveTvManager = liveTvManager;
_providerManager = providerManager;
2013-12-15 01:17:57 +00:00
_fileSystem = fileSystem;
2013-12-19 21:51:32 +00:00
_httpClient = httpClient;
2013-11-24 20:51:45 +00:00
}
public override bool Supports(BaseItem item)
{
2013-12-19 21:51:32 +00:00
return item is LiveTvChannel;
2013-11-24 20:51:45 +00:00
}
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
2013-11-24 21:30:38 +00:00
return !item.HasImage(ImageType.Primary);
2013-11-24 20:51:45 +00:00
}
2013-12-06 20:07:34 +00:00
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
2013-11-24 20:51:45 +00:00
{
if (item.HasImage(ImageType.Primary))
{
2013-12-06 20:07:34 +00:00
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
2013-11-24 20:51:45 +00:00
return true;
}
2013-12-22 17:16:24 +00:00
var changed = true;
2013-12-19 21:51:32 +00:00
try
2013-11-24 20:51:45 +00:00
{
2013-12-22 17:16:24 +00:00
changed = await DownloadImage((LiveTvChannel)item, cancellationToken).ConfigureAwait(false);
2013-12-19 21:51:32 +00:00
}
catch (HttpException ex)
{
// Don't fail the provider on a 404
if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound)
2013-12-13 15:48:48 +00:00
{
2013-12-19 21:51:32 +00:00
throw;
2013-11-24 20:51:45 +00:00
}
}
2013-12-22 17:16:24 +00:00
if (changed)
{
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
}
return changed;
2013-11-24 20:51:45 +00:00
}
2013-12-22 17:16:24 +00:00
private async Task<bool> DownloadImage(LiveTvChannel item, CancellationToken cancellationToken)
2013-11-24 20:51:45 +00:00
{
2013-12-19 21:51:32 +00:00
var channelInfo = item.ChannelInfo;
2013-11-24 20:51:45 +00:00
2013-12-19 21:51:32 +00:00
Stream imageStream = null;
string contentType = null;
2013-11-24 20:51:45 +00:00
2013-12-19 21:51:32 +00:00
if (!string.IsNullOrEmpty(channelInfo.ImagePath))
2013-11-24 20:51:45 +00:00
{
2013-12-19 21:51:32 +00:00
contentType = "image/" + Path.GetExtension(channelInfo.ImagePath).ToLower();
imageStream = _fileSystem.GetFileStream(channelInfo.ImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true);
}
else if (!string.IsNullOrEmpty(channelInfo.ImageUrl))
{
var options = new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = channelInfo.ImageUrl
};
var response = await _httpClient.GetResponse(options).ConfigureAwait(false);
2013-11-24 20:51:45 +00:00
2013-12-19 21:51:32 +00:00
if (!response.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
2013-12-22 17:16:24 +00:00
Logger.Error("Provider did not return an image content type.");
return false;
2013-12-19 21:51:32 +00:00
}
imageStream = response.Content;
contentType = response.ContentType;
}
2013-12-22 17:16:24 +00:00
else if (channelInfo.HasImage ?? true)
2013-12-19 21:51:32 +00:00
{
var service = _liveTvManager.Services.FirstOrDefault(i => string.Equals(i.Name, item.ServiceName, StringComparison.OrdinalIgnoreCase));
if (service != null)
{
2013-12-22 17:16:24 +00:00
try
{
var response = await service.GetChannelImageAsync(channelInfo.Id, cancellationToken).ConfigureAwait(false);
if (response != null)
{
imageStream = response.Stream;
contentType = response.MimeType;
}
}
catch (NotImplementedException)
{
return false;
}
2013-12-19 21:51:32 +00:00
}
}
if (imageStream != null)
{
2013-11-24 20:51:45 +00:00
// Dummy up the original url
2013-12-19 21:51:32 +00:00
var url = item.ServiceName + channelInfo.Id;
2013-11-24 20:51:45 +00:00
2013-12-19 21:51:32 +00:00
await _providerManager.SaveImage(item, imageStream, contentType, ImageType.Primary, null, url, cancellationToken).ConfigureAwait(false);
2013-12-22 17:16:24 +00:00
return true;
2013-11-24 20:51:45 +00:00
}
2013-12-22 17:16:24 +00:00
return false;
2013-11-24 20:51:45 +00:00
}
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Second; }
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
}
}