2020-12-03 02:51:53 +00:00
|
|
|
using System;
|
2021-08-15 15:20:07 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-10-27 23:57:52 +00:00
|
|
|
using System.Linq;
|
2020-12-03 02:51:53 +00:00
|
|
|
using System.Threading;
|
2021-06-19 16:02:33 +00:00
|
|
|
using Jellyfin.Extensions;
|
2020-10-27 23:57:52 +00:00
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.BaseItemManager
|
|
|
|
{
|
|
|
|
/// <inheritdoc />
|
|
|
|
public class BaseItemManager : IBaseItemManager
|
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
|
|
|
|
2021-08-15 15:20:07 +00:00
|
|
|
private int _metadataRefreshConcurrency;
|
2020-12-03 07:07:51 +00:00
|
|
|
|
2020-10-27 23:57:52 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BaseItemManager"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
|
|
|
public BaseItemManager(IServerConfigurationManager serverConfigurationManager)
|
|
|
|
{
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2020-12-03 02:51:53 +00:00
|
|
|
|
2020-12-03 07:07:51 +00:00
|
|
|
_metadataRefreshConcurrency = GetMetadataRefreshConcurrency();
|
|
|
|
SetupMetadataThrottler();
|
2020-12-03 02:51:53 +00:00
|
|
|
|
2020-12-04 01:11:53 +00:00
|
|
|
_serverConfigurationManager.ConfigurationUpdated += OnConfigurationUpdated;
|
2020-10-27 23:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 02:51:53 +00:00
|
|
|
/// <inheritdoc />
|
2020-12-03 07:23:13 +00:00
|
|
|
public SemaphoreSlim MetadataRefreshThrottler { get; private set; }
|
2020-12-03 02:51:53 +00:00
|
|
|
|
2020-10-27 23:57:52 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public bool IsMetadataFetcherEnabled(BaseItem baseItem, LibraryOptions libraryOptions, string name)
|
|
|
|
{
|
|
|
|
if (baseItem is Channel)
|
|
|
|
{
|
2020-11-08 13:42:54 +00:00
|
|
|
// Hack alert.
|
2020-10-27 23:57:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseItem.SourceType == SourceType.Channel)
|
|
|
|
{
|
2020-11-08 13:42:49 +00:00
|
|
|
// Hack alert.
|
2020-10-27 23:57:52 +00:00
|
|
|
return !baseItem.EnableMediaSourceDisplay;
|
|
|
|
}
|
|
|
|
|
2020-12-09 00:47:13 +00:00
|
|
|
var typeOptions = libraryOptions.GetTypeOptions(baseItem.GetType().Name);
|
2020-10-27 23:57:52 +00:00
|
|
|
if (typeOptions != null)
|
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
return typeOptions.MetadataFetchers.Contains(name.AsSpan(), StringComparison.OrdinalIgnoreCase);
|
2020-10-27 23:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!libraryOptions.EnableInternetProviders)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var itemConfig = _serverConfigurationManager.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, GetType().Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
return itemConfig == null || !itemConfig.DisabledMetadataFetchers.Contains(name.AsSpan(), StringComparison.OrdinalIgnoreCase);
|
2020-10-27 23:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool IsImageFetcherEnabled(BaseItem baseItem, LibraryOptions libraryOptions, string name)
|
|
|
|
{
|
|
|
|
if (baseItem is Channel)
|
|
|
|
{
|
2020-11-08 13:42:40 +00:00
|
|
|
// Hack alert.
|
2020-10-27 23:57:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseItem.SourceType == SourceType.Channel)
|
|
|
|
{
|
2020-11-08 13:42:33 +00:00
|
|
|
// Hack alert.
|
2020-10-27 23:57:52 +00:00
|
|
|
return !baseItem.EnableMediaSourceDisplay;
|
|
|
|
}
|
|
|
|
|
2020-12-09 00:47:13 +00:00
|
|
|
var typeOptions = libraryOptions.GetTypeOptions(baseItem.GetType().Name);
|
2020-10-27 23:57:52 +00:00
|
|
|
if (typeOptions != null)
|
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
return typeOptions.ImageFetchers.Contains(name.AsSpan(), StringComparison.OrdinalIgnoreCase);
|
2020-10-27 23:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!libraryOptions.EnableInternetProviders)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var itemConfig = _serverConfigurationManager.Configuration.MetadataOptions.FirstOrDefault(i => string.Equals(i.ItemType, GetType().Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
return itemConfig == null || !itemConfig.DisabledImageFetchers.Contains(name.AsSpan(), StringComparison.OrdinalIgnoreCase);
|
2020-10-27 23:57:52 +00:00
|
|
|
}
|
2020-12-03 07:07:51 +00:00
|
|
|
|
2020-12-04 01:11:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Called when the configuration is updated.
|
|
|
|
/// It will refresh the metadata throttler if the relevant config changed.
|
|
|
|
/// </summary>
|
2021-08-15 15:20:07 +00:00
|
|
|
private void OnConfigurationUpdated(object? sender, EventArgs e)
|
2020-12-04 01:11:53 +00:00
|
|
|
{
|
|
|
|
int newMetadataRefreshConcurrency = GetMetadataRefreshConcurrency();
|
|
|
|
if (_metadataRefreshConcurrency != newMetadataRefreshConcurrency)
|
|
|
|
{
|
|
|
|
_metadataRefreshConcurrency = newMetadataRefreshConcurrency;
|
|
|
|
SetupMetadataThrottler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 07:07:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates the metadata refresh throttler.
|
|
|
|
/// </summary>
|
2021-08-15 15:20:07 +00:00
|
|
|
[MemberNotNull(nameof(MetadataRefreshThrottler))]
|
2020-12-03 07:07:51 +00:00
|
|
|
private void SetupMetadataThrottler()
|
|
|
|
{
|
2020-12-03 07:23:13 +00:00
|
|
|
MetadataRefreshThrottler = new SemaphoreSlim(_metadataRefreshConcurrency);
|
2020-12-03 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the metadata refresh concurrency.
|
|
|
|
/// </summary>
|
|
|
|
private int GetMetadataRefreshConcurrency()
|
|
|
|
{
|
|
|
|
var concurrency = _serverConfigurationManager.Configuration.LibraryMetadataRefreshConcurrency;
|
|
|
|
|
|
|
|
if (concurrency <= 0)
|
|
|
|
{
|
|
|
|
concurrency = Environment.ProcessorCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return concurrency;
|
|
|
|
}
|
2020-10-27 23:57:52 +00:00
|
|
|
}
|
2020-11-08 13:42:33 +00:00
|
|
|
}
|