using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Progress;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
using Priority_Queue;
using Book = MediaBrowser.Controller.Entities.Book;
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
using Movie = MediaBrowser.Controller.Entities.Movies.Movie;
using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum;
using Season = MediaBrowser.Controller.Entities.TV.Season;
using Series = MediaBrowser.Controller.Entities.TV.Series;
namespace MediaBrowser.Providers.Manager
{
///
/// Class ProviderManager.
///
public class ProviderManager : IProviderManager, IDisposable
{
private readonly object _refreshQueueLock = new object();
private readonly ILogger _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILibraryMonitor _libraryMonitor;
private readonly IFileSystem _fileSystem;
private readonly IServerApplicationPaths _appPaths;
private readonly ILibraryManager _libraryManager;
private readonly ISubtitleManager _subtitleManager;
private readonly IServerConfigurationManager _configurationManager;
private readonly ConcurrentDictionary _activeRefreshes = new ConcurrentDictionary();
private readonly CancellationTokenSource _disposeCancellationTokenSource = new CancellationTokenSource();
private readonly SimplePriorityQueue> _refreshQueue =
new SimplePriorityQueue>();
private IMetadataService[] _metadataServices = Array.Empty();
private IMetadataProvider[] _metadataProviders = Array.Empty();
private IEnumerable _savers;
private IExternalId[] _externalIds;
private bool _isProcessingRefreshQueue;
private bool _disposed;
///
/// Initializes a new instance of the class.
///
/// The Http client factory.
/// The subtitle manager.
/// The configuration manager.
/// The library monitor.
/// The logger.
/// The filesystem.
/// The server application paths.
/// The library manager.
public ProviderManager(
IHttpClientFactory httpClientFactory,
ISubtitleManager subtitleManager,
IServerConfigurationManager configurationManager,
ILibraryMonitor libraryMonitor,
ILogger logger,
IFileSystem fileSystem,
IServerApplicationPaths appPaths,
ILibraryManager libraryManager)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
_configurationManager = configurationManager;
_libraryMonitor = libraryMonitor;
_fileSystem = fileSystem;
_appPaths = appPaths;
_libraryManager = libraryManager;
_subtitleManager = subtitleManager;
}
///
public event EventHandler> RefreshStarted;
///
public event EventHandler> RefreshCompleted;
///
public event EventHandler>> RefreshProgress;
private IImageProvider[] ImageProviders { get; set; }
///
public void AddParts(
IEnumerable imageProviders,
IEnumerable metadataServices,
IEnumerable metadataProviders,
IEnumerable metadataSavers,
IEnumerable externalIds)
{
ImageProviders = imageProviders.ToArray();
_metadataServices = metadataServices.OrderBy(i => i.Order).ToArray();
_metadataProviders = metadataProviders.ToArray();
_externalIds = externalIds.OrderBy(i => i.ProviderName).ToArray();
_savers = metadataSavers
.Where(i => !(i is IConfigurableProvider configurable) || configurable.IsEnabled)
.ToArray();
}
///
public Task RefreshSingleItem(BaseItem item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
var type = item.GetType();
var service = _metadataServices.FirstOrDefault(current => current.CanRefreshPrimary(type));
if (service == null)
{
foreach (var current in _metadataServices)
{
if (current.CanRefresh(item))
{
service = current;
break;
}
}
}
if (service != null)
{
return service.RefreshMetadata(item, options, cancellationToken);
}
_logger.LogError("Unable to find a metadata service for item of type {TypeName}", item.GetType().Name);
return Task.FromResult(ItemUpdateType.None);
}
///
public async Task SaveImage(BaseItem item, string url, ImageType type, int? imageIndex, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient();
using var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
var contentType = response.Content.Headers.ContentType.MediaType;
// Workaround for tvheadend channel icons
// TODO: Isolate this hack into the tvh plugin
if (string.IsNullOrEmpty(contentType))
{
if (url.IndexOf("/imagecache/", StringComparison.OrdinalIgnoreCase) != -1)
{
contentType = "image/png";
}
}
// thetvdb will sometimes serve a rubbish 404 html page with a 200 OK code, because reasons...
if (contentType.Equals(MediaTypeNames.Text.Html, StringComparison.OrdinalIgnoreCase))
{
throw new HttpException("Invalid image received.")
{
StatusCode = HttpStatusCode.NotFound
};
}
await SaveImage(
item,
await response.Content.ReadAsStreamAsync().ConfigureAwait(false),
contentType,
type,
imageIndex,
cancellationToken).ConfigureAwait(false);
}
///
public Task SaveImage(BaseItem item, Stream source, string mimeType, ImageType type, int? imageIndex, CancellationToken cancellationToken)
{
return new ImageSaver(_configurationManager, _libraryMonitor, _fileSystem, _logger).SaveImage(item, source, mimeType, type, imageIndex, cancellationToken);
}
///
public Task SaveImage(BaseItem item, string source, string mimeType, ImageType type, int? imageIndex, bool? saveLocallyWithMedia, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(source))
{
throw new ArgumentNullException(nameof(source));
}
var fileStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, true);
return new ImageSaver(_configurationManager, _libraryMonitor, _fileSystem, _logger).SaveImage(item, fileStream, mimeType, type, imageIndex, saveLocallyWithMedia, cancellationToken);
}
///
public Task SaveImage(User user, Stream source, string mimeType, string path)
{
return new ImageSaver(_configurationManager, _libraryMonitor, _fileSystem, _logger)
.SaveImage(user, source, path);
}
///
public async Task> GetAvailableRemoteImages(BaseItem item, RemoteImageQuery query, CancellationToken cancellationToken)
{
var providers = GetRemoteImageProviders(item, query.IncludeDisabledProviders);
if (!string.IsNullOrEmpty(query.ProviderName))
{
var providerName = query.ProviderName;
providers = providers.Where(i => string.Equals(i.Name, providerName, StringComparison.OrdinalIgnoreCase));
}
var preferredLanguage = item.GetPreferredMetadataLanguage();
var languages = new List();
if (!query.IncludeAllLanguages && !string.IsNullOrWhiteSpace(preferredLanguage))
{
languages.Add(preferredLanguage);
}
var tasks = providers.Select(i => GetImages(item, i, languages, cancellationToken, query.ImageType));
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
return results.SelectMany(i => i.ToList());
}
///
/// Gets the images.
///
/// The item.
/// The provider.
/// The preferred languages.
/// The cancellation token.
/// The type.
/// Task{IEnumerable{RemoteImageInfo}}.
private async Task> GetImages(
BaseItem item,
IRemoteImageProvider provider,
IReadOnlyCollection preferredLanguages,
CancellationToken cancellationToken,
ImageType? type = null)
{
try
{
var result = await provider.GetImages(item, cancellationToken).ConfigureAwait(false);
if (type.HasValue)
{
result = result.Where(i => i.Type == type.Value);
}
if (preferredLanguages.Count > 0)
{
result = result.Where(i => string.IsNullOrEmpty(i.Language) ||
preferredLanguages.Contains(i.Language, StringComparer.OrdinalIgnoreCase) ||
string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase));
}
return result;
}
catch (OperationCanceledException)
{
return new List();
}
catch (Exception ex)
{
_logger.LogError(ex, "{ProviderName} failed in GetImageInfos for type {ItemType} at {ItemPath}", provider.GetType().Name, item.GetType().Name, item.Path);
return new List();
}
}
///
public IEnumerable GetRemoteImageProviderInfo(BaseItem item)
{
return GetRemoteImageProviders(item, true).Select(i => new ImageProviderInfo(i.Name, i.GetSupportedImages(item).ToArray()));
}
///
/// Gets the image providers for the provided item.
///
/// The item.
/// The image refresh options.
/// The image providers for the item.
public IEnumerable GetImageProviders(BaseItem item, ImageRefreshOptions refreshOptions)
{
return GetImageProviders(item, _libraryManager.GetLibraryOptions(item), GetMetadataOptions(item), refreshOptions, false);
}
private IEnumerable GetImageProviders(BaseItem item, LibraryOptions libraryOptions, MetadataOptions options, ImageRefreshOptions refreshOptions, bool includeDisabled)
{
// Avoid implicitly captured closure
var currentOptions = options;
var typeOptions = libraryOptions.GetTypeOptions(item.GetType().Name);
var typeFetcherOrder = typeOptions?.ImageFetcherOrder;
return ImageProviders.Where(i => CanRefresh(i, item, libraryOptions, refreshOptions, includeDisabled))
.OrderBy(i =>
{
// See if there's a user-defined order
if (!(i is ILocalImageProvider))
{
var fetcherOrder = typeFetcherOrder ?? currentOptions.ImageFetcherOrder;
var index = Array.IndexOf(fetcherOrder, i.Name);
if (index != -1)
{
return index;
}
}
// Not configured. Just return some high number to put it at the end.
return 100;
})
.ThenBy(GetOrder);
}
///
/// Gets the metadata providers for the provided item.
///
/// The item.
/// The library options.
/// The type of metadata provider.
/// The metadata providers.
public IEnumerable> GetMetadataProviders(BaseItem item, LibraryOptions libraryOptions)
where T : BaseItem
{
var globalMetadataOptions = GetMetadataOptions(item);
return GetMetadataProvidersInternal(item, libraryOptions, globalMetadataOptions, false, false);
}
private IEnumerable> GetMetadataProvidersInternal(BaseItem item, LibraryOptions libraryOptions, MetadataOptions globalMetadataOptions, bool includeDisabled, bool forceEnableInternetMetadata)
where T : BaseItem
{
// Avoid implicitly captured closure
var currentOptions = globalMetadataOptions;
return _metadataProviders.OfType>()
.Where(i => CanRefresh(i, item, libraryOptions, includeDisabled, forceEnableInternetMetadata))
.OrderBy(i => GetConfiguredOrder(item, i, libraryOptions, globalMetadataOptions))
.ThenBy(GetDefaultOrder);
}
private IEnumerable GetRemoteImageProviders(BaseItem item, bool includeDisabled)
{
var options = GetMetadataOptions(item);
var libraryOptions = _libraryManager.GetLibraryOptions(item);
return GetImageProviders(
item,
libraryOptions,
options,
new ImageRefreshOptions(new DirectoryService(_fileSystem)),
includeDisabled).OfType();
}
private bool CanRefresh(
IMetadataProvider provider,
BaseItem item,
LibraryOptions libraryOptions,
bool includeDisabled,
bool forceEnableInternetMetadata)
{
if (!includeDisabled)
{
// If locked only allow local providers
if (item.IsLocked && !(provider is ILocalMetadataProvider) && !(provider is IForcedProvider))
{
return false;
}
if (provider is IRemoteMetadataProvider)
{
if (!forceEnableInternetMetadata && !item.IsMetadataFetcherEnabled(libraryOptions, provider.Name))
{
return false;
}
}
}
if (!item.SupportsLocalMetadata && provider is ILocalMetadataProvider)
{
return false;
}
// If this restriction is ever lifted, movie xml providers will have to be updated to prevent owned items like trailers from reading those files
if (!item.OwnerId.Equals(Guid.Empty))
{
if (provider is ILocalMetadataProvider || provider is IRemoteMetadataProvider)
{
return false;
}
}
return true;
}
private bool CanRefresh(
IImageProvider provider,
BaseItem item,
LibraryOptions libraryOptions,
ImageRefreshOptions refreshOptions,
bool includeDisabled)
{
if (!includeDisabled)
{
// If locked only allow local providers
if (item.IsLocked && !(provider is ILocalImageProvider))
{
if (refreshOptions.ImageRefreshMode != MetadataRefreshMode.FullRefresh)
{
return false;
}
}
if (provider is IRemoteImageProvider || provider is IDynamicImageProvider)
{
if (!item.IsImageFetcherEnabled(libraryOptions, provider.Name))
{
return false;
}
}
}
try
{
return provider.Supports(item);
}
catch (Exception ex)
{
_logger.LogError(ex, "{ProviderName} failed in Supports for type {ItemType} at {ItemPath}", provider.GetType().Name, item.GetType().Name, item.Path);
return false;
}
}
///
/// Gets the order.
///
/// The provider.
/// System.Int32.
private int GetOrder(IImageProvider provider)
{
if (!(provider is IHasOrder hasOrder))
{
return 0;
}
return hasOrder.Order;
}
private int GetConfiguredOrder(BaseItem item, IMetadataProvider provider, LibraryOptions libraryOptions, MetadataOptions globalMetadataOptions)
{
// See if there's a user-defined order
if (provider is ILocalMetadataProvider)
{
var configuredOrder = libraryOptions.LocalMetadataReaderOrder ?? globalMetadataOptions.LocalMetadataReaderOrder;
var index = Array.IndexOf(configuredOrder, provider.Name);
if (index != -1)
{
return index;
}
}
// See if there's a user-defined order
if (provider is IRemoteMetadataProvider)
{
var typeOptions = libraryOptions.GetTypeOptions(item.GetType().Name);
var typeFetcherOrder = typeOptions?.MetadataFetcherOrder;
var fetcherOrder = typeFetcherOrder ?? globalMetadataOptions.MetadataFetcherOrder;
var index = Array.IndexOf(fetcherOrder, provider.Name);
if (index != -1)
{
return index;
}
}
// Not configured. Just return some high number to put it at the end.
return 100;
}
private int GetDefaultOrder(IMetadataProvider provider)
{
if (provider is IHasOrder hasOrder)
{
return hasOrder.Order;
}
return 0;
}
///
public MetadataPluginSummary[] GetAllMetadataPlugins()
{
return new[]
{
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary(),
GetPluginSummary