You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../LiveTv/RecordingImageProvider.cs

157 lines
5.4 KiB

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
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;
namespace MediaBrowser.Server.Implementations.LiveTv
{
public class RecordingImageProvider : BaseMetadataProvider
{
private readonly ILiveTvManager _liveTvManager;
private readonly IProviderManager _providerManager;
private readonly IFileSystem _fileSystem;
private readonly IHttpClient _httpClient;
public RecordingImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, ILiveTvManager liveTvManager, IProviderManager providerManager, IFileSystem fileSystem, IHttpClient httpClient)
: base(logManager, configurationManager)
{
_liveTvManager = liveTvManager;
_providerManager = providerManager;
_fileSystem = fileSystem;
_httpClient = httpClient;
}
public override bool Supports(BaseItem item)
{
return item is ILiveTvRecording;
}
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
return !item.HasImage(ImageType.Primary);
}
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
{
if (item.HasImage(ImageType.Primary))
{
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
return true;
}
11 years ago
var changed = true;
try
{
changed = await DownloadImage((ILiveTvRecording)item, cancellationToken).ConfigureAwait(false);
}
catch (HttpException ex)
{
// Don't fail the provider on a 404
if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound)
{
throw;
}
}
11 years ago
if (changed)
{
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
}
return changed;
}
private async Task<bool> DownloadImage(ILiveTvRecording item, CancellationToken cancellationToken)
{
var recordingInfo = item.RecordingInfo;
Stream imageStream = null;
string contentType = null;
if (!string.IsNullOrEmpty(recordingInfo.ImagePath))
{
contentType = "image/" + Path.GetExtension(recordingInfo.ImagePath).ToLower();
imageStream = _fileSystem.GetFileStream(recordingInfo.ImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true);
}
else if (!string.IsNullOrEmpty(recordingInfo.ImageUrl))
{
var options = new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = recordingInfo.ImageUrl
};
var response = await _httpClient.GetResponse(options).ConfigureAwait(false);
if (!response.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
11 years ago
Logger.Error("Provider did not return an image content type.");
return false;
}
imageStream = response.Content;
contentType = response.ContentType;
}
11 years ago
else if (recordingInfo.HasImage ?? true)
{
var service = _liveTvManager.Services.FirstOrDefault(i => string.Equals(i.Name, item.ServiceName, StringComparison.OrdinalIgnoreCase));
if (service != null)
{
11 years ago
try
{
var response = await service.GetRecordingImageAsync(recordingInfo.Id, cancellationToken).ConfigureAwait(false);
if (response != null)
{
imageStream = response.Stream;
contentType = "image/" + response.Format.ToString().ToLower();
11 years ago
}
}
catch (NotImplementedException)
{
return false;
}
}
}
if (imageStream != null)
{
// Dummy up the original url
var url = item.ServiceName + recordingInfo.Id;
await _providerManager.SaveImage((BaseItem)item, imageStream, contentType, ImageType.Primary, null, url, cancellationToken).ConfigureAwait(false);
11 years ago
return true;
}
11 years ago
return false;
}
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Second; }
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
}
}