2021-10-26 06:49:01 -07:00
|
|
|
#nullable disable
|
|
|
|
|
2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2014-01-28 11:37:01 -07:00
|
|
|
using System.Collections.Generic;
|
2014-02-02 06:36:31 -07:00
|
|
|
using System.IO;
|
2014-01-28 11:37:01 -07:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2020-11-14 14:30:34 -07:00
|
|
|
using System.Net.Http;
|
2014-01-28 11:37:01 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2015-11-07 22:04:38 -07:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Controller.Library;
|
2016-02-25 21:09:42 -07:00
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-10-16 10:06:31 -07:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
{
|
2021-10-30 17:40:15 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Utilities for managing images attached to items.
|
|
|
|
/// </summary>
|
2014-01-28 11:37:01 -07:00
|
|
|
public class ItemImageProvider
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IProviderManager _providerManager;
|
2014-01-31 12:55:21 -07:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2020-09-07 04:20:39 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Image types that are only one per item.
|
|
|
|
/// </summary>
|
2021-05-23 15:30:41 -07:00
|
|
|
private static readonly ImageType[] _singularImages =
|
2020-09-07 04:20:39 -07:00
|
|
|
{
|
|
|
|
ImageType.Primary,
|
|
|
|
ImageType.Art,
|
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Box,
|
|
|
|
ImageType.BoxRear,
|
|
|
|
ImageType.Disc,
|
|
|
|
ImageType.Logo,
|
|
|
|
ImageType.Menu,
|
|
|
|
ImageType.Thumb
|
|
|
|
};
|
|
|
|
|
2021-10-30 17:40:15 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ItemImageProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="providerManager">The provider manager for interacting with provider image references.</param>
|
|
|
|
/// <param name="fileSystem">The filesystem.</param>
|
2020-01-08 09:52:50 -07:00
|
|
|
public ItemImageProvider(ILogger logger, IProviderManager providerManager, IFileSystem fileSystem)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_providerManager = providerManager;
|
2014-02-02 06:36:31 -07:00
|
|
|
_fileSystem = fileSystem;
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
2022-01-03 15:41:13 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Removes all existing images from the provided item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The <see cref="BaseItem"/> to remove images from.</param>
|
|
|
|
/// <returns><c>true</c> if changes were made to the item; otherwise <c>false</c>.</returns>
|
|
|
|
public bool RemoveImages(BaseItem item)
|
|
|
|
{
|
|
|
|
var singular = new List<ItemImageInfo>();
|
|
|
|
for (var i = 0; i < _singularImages.Length; i++)
|
|
|
|
{
|
|
|
|
var currentImage = item.GetImageInfo(_singularImages[i], 0);
|
|
|
|
if (currentImage != null)
|
|
|
|
{
|
|
|
|
singular.Add(currentImage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldBackdropImages = item.GetImages(ImageType.Backdrop).ToArray();
|
|
|
|
var toRemove = singular.Concat(oldBackdropImages).ToArray();
|
|
|
|
PruneImages(item, toRemove);
|
|
|
|
|
|
|
|
return toRemove.Length > 0;
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:15 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Verifies existing images have valid paths and adds any new local images provided.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The <see cref="BaseItem"/> to validate images for.</param>
|
|
|
|
/// <param name="providers">The providers to use, must include <see cref="ILocalImageProvider"/>(s) for local scanning.</param>
|
|
|
|
/// <param name="directoryService">The directory service for <see cref="ILocalImageProvider"/>s to use.</param>
|
|
|
|
/// <returns><c>true</c> if changes were made to the item; otherwise <c>false</c>.</returns>
|
2018-09-12 10:26:21 -07:00
|
|
|
public bool ValidateImages(BaseItem item, IEnumerable<IImageProvider> providers, IDirectoryService directoryService)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2015-04-10 18:42:37 -07:00
|
|
|
var hasChanges = false;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2021-08-28 15:32:50 -07:00
|
|
|
if (item is not Photo)
|
2015-04-10 18:42:37 -07:00
|
|
|
{
|
2020-03-09 08:10:02 -07:00
|
|
|
var images = providers.OfType<ILocalImageProvider>()
|
2015-05-01 11:37:01 -07:00
|
|
|
.SelectMany(i => i.GetImages(item, directoryService))
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
if (MergeImages(item, images))
|
|
|
|
{
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:15 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Refreshes from the providers according to the given options.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The <see cref="BaseItem"/> to gather images for.</param>
|
|
|
|
/// <param name="libraryOptions">The library options.</param>
|
|
|
|
/// <param name="providers">The providers to query for images.</param>
|
|
|
|
/// <param name="refreshOptions">The refresh options.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>The refresh result.</returns>
|
2020-08-07 10:26:28 -07:00
|
|
|
public async Task<RefreshResult> RefreshImages(
|
|
|
|
BaseItem item,
|
|
|
|
LibraryOptions libraryOptions,
|
2022-01-03 14:51:28 -07:00
|
|
|
IEnumerable<IImageProvider> providers,
|
2020-08-07 10:26:28 -07:00
|
|
|
ImageRefreshOptions refreshOptions,
|
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2021-11-02 08:16:06 -07:00
|
|
|
var oldBackdropImages = Array.Empty<ItemImageInfo>();
|
2014-06-14 19:24:04 -07:00
|
|
|
if (refreshOptions.IsReplacingImage(ImageType.Backdrop))
|
|
|
|
{
|
2021-11-02 08:16:06 -07:00
|
|
|
oldBackdropImages = item.GetImages(ImageType.Backdrop).ToArray();
|
2014-06-14 19:24:04 -07:00
|
|
|
}
|
2020-06-15 14:43:52 -07:00
|
|
|
|
2014-02-10 11:39:41 -07:00
|
|
|
var result = new RefreshResult { UpdateType = ItemUpdateType.None };
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var typeName = item.GetType().Name;
|
|
|
|
var typeOptions = libraryOptions.GetTypeOptions(typeName) ?? new TypeOptions { Type = typeName };
|
2014-01-28 22:17:58 -07:00
|
|
|
|
2021-10-31 16:16:11 -07:00
|
|
|
// track library limits, adding buffer to allow lazy replacing of current images
|
2021-11-02 08:16:06 -07:00
|
|
|
var backdropLimit = typeOptions.GetLimit(ImageType.Backdrop) + oldBackdropImages.Length;
|
2014-06-14 19:24:04 -07:00
|
|
|
var downloadedImages = new List<ImageType>();
|
2014-02-02 06:36:31 -07:00
|
|
|
|
2014-02-06 17:43:45 -07:00
|
|
|
foreach (var provider in providers)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2020-08-07 10:26:28 -07:00
|
|
|
if (provider is IRemoteImageProvider remoteProvider)
|
2014-02-06 17:43:45 -07:00
|
|
|
{
|
2021-11-05 13:31:12 -07:00
|
|
|
await RefreshFromProvider(item, remoteProvider, refreshOptions, typeOptions, backdropLimit, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-06 17:43:45 -07:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2020-08-07 10:26:28 -07:00
|
|
|
if (provider is IDynamicImageProvider dynamicImageProvider)
|
2014-02-06 17:43:45 -07:00
|
|
|
{
|
2020-08-07 10:26:28 -07:00
|
|
|
await RefreshFromProvider(item, dynamicImageProvider, refreshOptions, typeOptions, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-06 17:43:45 -07:00
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
2021-10-31 16:16:11 -07:00
|
|
|
// only delete existing multi-images if new ones were added
|
2021-11-02 08:16:06 -07:00
|
|
|
if (oldBackdropImages.Length > 0 && oldBackdropImages.Length < item.GetImages(ImageType.Backdrop).Count())
|
2021-10-31 16:16:11 -07:00
|
|
|
{
|
|
|
|
PruneImages(item, oldBackdropImages);
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-30 17:40:15 -07:00
|
|
|
/// Refreshes from a dynamic provider.
|
2014-01-28 11:37:01 -07:00
|
|
|
/// </summary>
|
2020-08-07 10:26:28 -07:00
|
|
|
private async Task RefreshFromProvider(
|
|
|
|
BaseItem item,
|
2014-06-13 07:24:14 -07:00
|
|
|
IDynamicImageProvider provider,
|
|
|
|
ImageRefreshOptions refreshOptions,
|
2018-09-12 10:26:21 -07:00
|
|
|
TypeOptions savedOptions,
|
2014-06-14 19:24:04 -07:00
|
|
|
ICollection<ImageType> downloadedImages,
|
2014-06-13 07:24:14 -07:00
|
|
|
RefreshResult result,
|
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-01-29 09:16:24 -07:00
|
|
|
var images = provider.GetSupportedImages(item);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2014-01-29 09:16:24 -07:00
|
|
|
foreach (var imageType in images)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2022-01-03 14:51:28 -07:00
|
|
|
if (!savedOptions.IsEnabled(imageType))
|
2020-06-20 01:35:29 -07:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-14 19:24:04 -07:00
|
|
|
|
2022-01-03 14:51:28 -07:00
|
|
|
if (!item.HasImage(imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType)))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2022-01-03 14:51:28 -07:00
|
|
|
_logger.LogDebug("Running {Provider} for {Item}", provider.GetType().Name, item.Path ?? item.Name);
|
2014-02-05 21:39:16 -07:00
|
|
|
|
2014-01-29 09:16:24 -07:00
|
|
|
var response = await provider.GetImage(item, imageType, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2014-01-29 09:16:24 -07:00
|
|
|
if (response.HasImage)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2014-01-31 12:55:21 -07:00
|
|
|
if (!string.IsNullOrEmpty(response.Path))
|
|
|
|
{
|
2015-10-16 10:06:31 -07:00
|
|
|
if (response.Protocol == MediaProtocol.Http)
|
|
|
|
{
|
2022-01-03 14:51:28 -07:00
|
|
|
_logger.LogDebug("Setting image url into item {Item}", item.Id);
|
2021-10-31 16:16:11 -07:00
|
|
|
var index = item.AllowsMultipleImages(imageType) ? item.GetImages(imageType).Count() : 0;
|
2020-08-07 10:26:28 -07:00
|
|
|
item.SetImage(
|
|
|
|
new ItemImageInfo
|
|
|
|
{
|
|
|
|
Path = response.Path,
|
|
|
|
Type = imageType
|
|
|
|
},
|
2021-10-31 16:16:11 -07:00
|
|
|
index);
|
2015-10-16 10:06:31 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var mimeType = MimeTypes.GetMimeType(response.Path);
|
|
|
|
|
2021-10-03 10:52:38 -07:00
|
|
|
var stream = AsyncFile.OpenRead(response.Path);
|
2015-10-16 10:06:31 -07:00
|
|
|
|
2016-10-17 09:35:29 -07:00
|
|
|
await _providerManager.SaveImage(item, stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
|
2015-10-16 10:06:31 -07:00
|
|
|
}
|
2014-01-31 12:55:21 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-27 04:03:43 -07:00
|
|
|
var mimeType = "image/" + response.Format.ToString().ToLowerInvariant();
|
2014-01-31 12:55:21 -07:00
|
|
|
|
2016-10-17 09:35:29 -07:00
|
|
|
await _providerManager.SaveImage(item, response.Stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
|
2014-01-31 12:55:21 -07:00
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2014-06-14 19:24:04 -07:00
|
|
|
downloadedImages.Add(imageType);
|
2020-08-07 10:26:28 -07:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2014-01-29 09:16:24 -07:00
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
result.ErrorMessage = ex.Message;
|
2021-11-09 05:14:31 -07:00
|
|
|
_logger.LogError(ex, "Error in {Provider}", provider.Name);
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-30 17:40:15 -07:00
|
|
|
/// Refreshes from a remote provider.
|
2014-01-28 11:37:01 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
2014-02-02 06:36:31 -07:00
|
|
|
/// <param name="refreshOptions">The refresh options.</param>
|
|
|
|
/// <param name="savedOptions">The saved options.</param>
|
|
|
|
/// <param name="backdropLimit">The backdrop limit.</param>
|
2014-06-14 19:24:04 -07:00
|
|
|
/// <param name="downloadedImages">The downloaded images.</param>
|
2014-01-28 11:37:01 -07:00
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2020-04-05 09:10:56 -07:00
|
|
|
private async Task RefreshFromProvider(
|
|
|
|
BaseItem item,
|
2014-10-04 11:05:24 -07:00
|
|
|
IRemoteImageProvider provider,
|
|
|
|
ImageRefreshOptions refreshOptions,
|
2018-09-12 10:26:21 -07:00
|
|
|
TypeOptions savedOptions,
|
2014-10-04 11:05:24 -07:00
|
|
|
int backdropLimit,
|
2014-06-14 19:24:04 -07:00
|
|
|
ICollection<ImageType> downloadedImages,
|
2014-10-04 11:05:24 -07:00
|
|
|
RefreshResult result,
|
2014-06-14 19:24:04 -07:00
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-08-14 06:24:30 -07:00
|
|
|
if (!item.SupportsRemoteImageDownloading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-04 11:05:24 -07:00
|
|
|
if (!refreshOptions.ReplaceAllImages &&
|
2018-09-12 10:26:21 -07:00
|
|
|
refreshOptions.ReplaceImages.Length == 0 &&
|
2021-11-05 13:31:12 -07:00
|
|
|
ContainsImages(item, provider.GetSupportedImages(item).ToList(), savedOptions, backdropLimit))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:51:28 -07:00
|
|
|
_logger.LogDebug("Running {Provider} for {Item}", provider.GetType().Name, item.Path ?? item.Name);
|
2014-02-07 13:30:41 -07:00
|
|
|
|
2020-04-05 09:10:56 -07:00
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(
|
|
|
|
item,
|
|
|
|
new RemoteImageQuery(provider.Name)
|
|
|
|
{
|
2021-09-08 14:20:11 -07:00
|
|
|
IncludeAllLanguages = true,
|
2020-04-05 09:10:56 -07:00
|
|
|
IncludeDisabledProviders = false,
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2014-02-11 20:46:27 -07:00
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
var list = images.ToList();
|
2014-02-11 20:33:30 -07:00
|
|
|
int minWidth;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2014-06-14 19:24:04 -07:00
|
|
|
foreach (var imageType in _singularImages)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2022-01-03 14:51:28 -07:00
|
|
|
if (!savedOptions.IsEnabled(imageType))
|
2020-04-05 09:10:56 -07:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-14 19:24:04 -07:00
|
|
|
|
2022-01-03 14:51:28 -07:00
|
|
|
if (!item.HasImage(imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType)))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2014-06-14 19:24:04 -07:00
|
|
|
minWidth = savedOptions.GetMinWidth(imageType);
|
2021-06-26 17:00:17 -07:00
|
|
|
var downloaded = await DownloadImage(item, provider, result, list, minWidth, imageType, cancellationToken).ConfigureAwait(false);
|
2014-06-14 19:24:04 -07:00
|
|
|
|
|
|
|
if (downloaded)
|
|
|
|
{
|
|
|
|
downloadedImages.Add(imageType);
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Backdrop);
|
2021-11-01 16:31:59 -07:00
|
|
|
await DownloadMultiImages(item, ImageType.Backdrop, refreshOptions, backdropLimit, provider, result, list, minWidth, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
result.ErrorMessage = ex.Message;
|
2021-11-09 05:14:31 -07:00
|
|
|
_logger.LogError(ex, "Error in {Provider}", provider.Name);
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:51:28 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Determines if an item already contains the given images.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="images">The images.</param>
|
|
|
|
/// <param name="savedOptions">The saved options.</param>
|
|
|
|
/// <param name="backdropLimit">The backdrop limit.</param>
|
|
|
|
/// <returns><c>true</c> if the specified item contains images; otherwise, <c>false</c>.</returns>
|
|
|
|
private bool ContainsImages(BaseItem item, List<ImageType> images, TypeOptions savedOptions, int backdropLimit)
|
2014-10-04 11:05:24 -07:00
|
|
|
{
|
2022-01-03 14:51:28 -07:00
|
|
|
// Using .Any causes the creation of a DisplayClass aka. variable capture
|
|
|
|
for (var i = 0; i < _singularImages.Length; i++)
|
|
|
|
{
|
|
|
|
var type = _singularImages[i];
|
|
|
|
if (images.Contains(type) && !item.HasImage(type) && savedOptions.GetLimit(type) > 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (images.Contains(ImageType.Backdrop) && item.GetImages(ImageType.Backdrop).Count() < backdropLimit)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-10-04 11:05:24 -07:00
|
|
|
}
|
|
|
|
|
2021-11-02 08:16:06 -07:00
|
|
|
private void PruneImages(BaseItem item, ItemImageInfo[] images)
|
2014-06-14 19:24:04 -07:00
|
|
|
{
|
2021-11-02 08:16:06 -07:00
|
|
|
for (var i = 0; i < images.Length; i++)
|
2014-06-14 19:24:04 -07:00
|
|
|
{
|
2021-10-31 16:16:11 -07:00
|
|
|
var image = images[i];
|
2015-12-29 19:42:17 -07:00
|
|
|
|
2021-10-31 16:16:11 -07:00
|
|
|
if (image.IsLocalFile)
|
2017-03-24 08:03:49 -07:00
|
|
|
{
|
2021-10-31 16:16:11 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_fileSystem.DeleteFile(image.Path);
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
2022-01-03 14:51:28 -07:00
|
|
|
// nothing to do, already gone
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
{
|
|
|
|
_logger.LogWarning(ex, "Unable to delete {Image}", image.Path);
|
2021-10-31 16:16:11 -07:00
|
|
|
}
|
2017-03-24 08:03:49 -07:00
|
|
|
}
|
2014-06-14 19:24:04 -07:00
|
|
|
}
|
|
|
|
|
2021-10-31 16:16:11 -07:00
|
|
|
item.RemoveImages(images);
|
2014-06-14 19:24:04 -07:00
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:15 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Merges a list of images into the provided item, validating existing images and replacing them or adding new images as necessary.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The <see cref="BaseItem"/> to modify.</param>
|
|
|
|
/// <param name="images">The new images to place in <c>item</c>.</param>
|
|
|
|
/// <returns><c>true</c> if changes were made to the item; otherwise <c>false</c>.</returns>
|
2021-06-26 17:00:17 -07:00
|
|
|
public bool MergeImages(BaseItem item, IReadOnlyList<LocalImageInfo> images)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2021-10-31 18:38:12 -07:00
|
|
|
var changed = item.ValidateImages(new DirectoryService(_fileSystem));
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2021-05-23 15:30:41 -07:00
|
|
|
for (var i = 0; i < _singularImages.Length; i++)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2021-05-23 15:30:41 -07:00
|
|
|
var type = _singularImages[i];
|
|
|
|
var image = GetFirstLocalImageInfoByType(images, type);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
{
|
2014-02-07 13:30:41 -07:00
|
|
|
var currentImage = item.GetImageInfo(type, 0);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2022-01-03 14:51:28 -07:00
|
|
|
if (currentImage == null || !string.Equals(currentImage.Path, image.FileInfo.FullName, StringComparison.OrdinalIgnoreCase))
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
|
|
|
item.SetImagePath(type, image.FileInfo);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-29 21:52:30 -07:00
|
|
|
var newDateModified = _fileSystem.GetLastWriteTimeUtc(image.FileInfo);
|
|
|
|
|
|
|
|
// If date changed then we need to reset saved image dimensions
|
|
|
|
if (currentImage.DateModified != newDateModified && (currentImage.Width > 0 || currentImage.Height > 0))
|
|
|
|
{
|
|
|
|
currentImage.Width = 0;
|
|
|
|
currentImage.Height = 0;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentImage.DateModified = newDateModified;
|
2014-05-13 17:46:45 -07:00
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:46:45 -07:00
|
|
|
if (UpdateMultiImages(item, images, ImageType.Backdrop))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2014-05-13 17:46:45 -07:00
|
|
|
changed = true;
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2014-05-13 17:46:45 -07:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2021-05-23 15:30:41 -07:00
|
|
|
private static LocalImageInfo GetFirstLocalImageInfoByType(IReadOnlyList<LocalImageInfo> images, ImageType type)
|
|
|
|
{
|
|
|
|
var len = images.Count;
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
var image = images[i];
|
|
|
|
if (image.Type == type)
|
|
|
|
{
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-26 17:00:17 -07:00
|
|
|
private bool UpdateMultiImages(BaseItem item, IReadOnlyList<LocalImageInfo> images, ImageType type)
|
2014-05-13 17:46:45 -07:00
|
|
|
{
|
|
|
|
var changed = false;
|
|
|
|
|
2021-05-23 15:30:41 -07:00
|
|
|
var newImageFileInfos = images
|
2021-06-26 17:00:17 -07:00
|
|
|
.Where(i => i.Type == type)
|
2021-05-23 15:30:41 -07:00
|
|
|
.Select(i => i.FileInfo)
|
|
|
|
.ToList();
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2015-05-06 05:56:26 -07:00
|
|
|
if (item.AddImages(type, newImageFileInfos))
|
|
|
|
{
|
|
|
|
changed = true;
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:20:39 -07:00
|
|
|
private async Task<bool> DownloadImage(
|
|
|
|
BaseItem item,
|
2014-10-04 11:05:24 -07:00
|
|
|
IRemoteImageProvider provider,
|
|
|
|
RefreshResult result,
|
|
|
|
IEnumerable<RemoteImageInfo> images,
|
|
|
|
int minWidth,
|
|
|
|
ImageType type,
|
2014-06-14 19:24:04 -07:00
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2016-01-23 21:21:31 -07:00
|
|
|
var eligibleImages = images
|
2021-11-12 07:21:46 -07:00
|
|
|
.Where(i => i.Type == type && (i.Width == null || i.Width >= minWidth))
|
2016-01-23 21:21:31 -07:00
|
|
|
.ToList();
|
|
|
|
|
2021-06-26 17:00:17 -07:00
|
|
|
if (EnableImageStub(item) && eligibleImages.Count > 0)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2016-01-23 21:21:31 -07:00
|
|
|
SaveImageStub(item, type, eligibleImages.Select(i => i.Url));
|
2020-08-07 10:26:28 -07:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2016-01-23 21:21:31 -07:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-11 20:33:30 -07:00
|
|
|
|
2016-01-23 21:21:31 -07:00
|
|
|
foreach (var image in eligibleImages)
|
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
var url = image.Url;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-08-17 10:54:55 -07:00
|
|
|
using var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
2021-09-21 13:47:30 -07:00
|
|
|
|
2021-09-21 14:12:38 -07:00
|
|
|
// Sometimes providers send back bad urls. Just move to the next image
|
2021-09-21 13:47:30 -07:00
|
|
|
if (response.StatusCode == HttpStatusCode.NotFound || response.StatusCode == HttpStatusCode.Forbidden)
|
|
|
|
{
|
|
|
|
_logger.LogDebug("{Url} returned {StatusCode}, ignoring", url, response.StatusCode);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("{Url} returned {StatusCode}, skipping all remaining requests", url, response.StatusCode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:43:00 -07:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2020-08-17 10:54:55 -07:00
|
|
|
await _providerManager.SaveImage(
|
|
|
|
item,
|
2020-08-17 12:22:42 -07:00
|
|
|
stream,
|
2022-01-03 14:51:28 -07:00
|
|
|
response.Content.Headers.ContentType?.MediaType,
|
2020-08-17 10:54:55 -07:00
|
|
|
type,
|
|
|
|
null,
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2020-08-07 10:26:28 -07:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2014-06-14 19:24:04 -07:00
|
|
|
return true;
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
2021-09-21 13:47:30 -07:00
|
|
|
catch (HttpRequestException)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 19:24:04 -07:00
|
|
|
|
|
|
|
return false;
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
2021-06-26 17:00:17 -07:00
|
|
|
private bool EnableImageStub(BaseItem item)
|
2015-10-17 08:05:12 -07:00
|
|
|
{
|
2016-02-25 21:09:42 -07:00
|
|
|
if (item is LiveTvProgram)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!item.IsFileProtocol)
|
2015-10-17 08:05:12 -07:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-30 17:40:15 -07:00
|
|
|
if (item is IItemByName and not MusicArtist)
|
2015-11-07 22:04:38 -07:00
|
|
|
{
|
|
|
|
var hasDualAccess = item as IHasDualAccess;
|
|
|
|
if (hasDualAccess == null || hasDualAccess.IsAccessedByName)
|
|
|
|
{
|
2015-11-20 10:55:38 -07:00
|
|
|
return true;
|
2015-11-07 22:04:38 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 17:33:19 -07:00
|
|
|
|
2020-10-19 08:28:07 -07:00
|
|
|
// We always want to use prefetched images
|
2020-05-27 10:13:41 -07:00
|
|
|
return false;
|
2015-10-17 08:05:12 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private void SaveImageStub(BaseItem item, ImageType imageType, IEnumerable<string> urls)
|
2015-10-17 08:05:12 -07:00
|
|
|
{
|
|
|
|
var newIndex = item.AllowsMultipleImages(imageType) ? item.GetImages(imageType).Count() : 0;
|
|
|
|
|
2016-01-23 21:21:31 -07:00
|
|
|
SaveImageStub(item, imageType, urls, newIndex);
|
2015-11-09 11:18:37 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private void SaveImageStub(BaseItem item, ImageType imageType, IEnumerable<string> urls, int newIndex)
|
2015-11-09 11:18:37 -07:00
|
|
|
{
|
2020-09-07 04:20:39 -07:00
|
|
|
var path = string.Join('|', urls.Take(1));
|
2015-10-17 08:05:12 -07:00
|
|
|
|
2020-09-07 04:20:39 -07:00
|
|
|
item.SetImage(
|
|
|
|
new ItemImageInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Type = imageType
|
|
|
|
},
|
|
|
|
newIndex);
|
2015-10-17 08:05:12 -07:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:31:59 -07:00
|
|
|
private async Task DownloadMultiImages(BaseItem item, ImageType imageType, ImageRefreshOptions refreshOptions, int limit, IRemoteImageProvider provider, RefreshResult result, IEnumerable<RemoteImageInfo> images, int minWidth, CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
foreach (var image in images.Where(i => i.Type == imageType))
|
|
|
|
{
|
2014-02-07 13:30:41 -07:00
|
|
|
if (item.GetImages(imageType).Count() >= limit)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-11 20:33:30 -07:00
|
|
|
if (image.Width.HasValue && image.Width.Value < minWidth)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
var url = image.Url;
|
|
|
|
|
2021-06-26 17:00:17 -07:00
|
|
|
if (EnableImageStub(item))
|
2015-10-17 08:05:12 -07:00
|
|
|
{
|
2016-01-23 21:21:31 -07:00
|
|
|
SaveImageStub(item, imageType, new[] { url });
|
2020-08-07 10:26:28 -07:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2015-11-09 11:18:37 -07:00
|
|
|
continue;
|
2015-10-17 08:05:12 -07:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
try
|
|
|
|
{
|
2020-08-17 10:54:55 -07:00
|
|
|
using var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2021-09-21 14:12:38 -07:00
|
|
|
// Sometimes providers send back bad urls. Just move to the next image
|
2021-09-21 13:47:30 -07:00
|
|
|
if (response.StatusCode == HttpStatusCode.NotFound || response.StatusCode == HttpStatusCode.Forbidden)
|
|
|
|
{
|
|
|
|
_logger.LogDebug("{Url} returned {StatusCode}, ignoring", url, response.StatusCode);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("{Url} returned {StatusCode}, skipping all remaining requests", url, response.StatusCode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:31:59 -07:00
|
|
|
// If there's already an image of the same file size, skip it unless doing a full refresh
|
|
|
|
if (response.Content.Headers.ContentLength.HasValue && !refreshOptions.IsReplacingImage(imageType))
|
2014-02-25 08:40:16 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-08-17 10:54:55 -07:00
|
|
|
if (item.GetImages(imageType).Any(i => _fileSystem.GetFileInfo(i.Path).Length == response.Content.Headers.ContentLength.Value))
|
2014-02-25 08:40:16 -07:00
|
|
|
{
|
|
|
|
response.Content.Dispose();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error examining images");
|
2014-02-25 08:40:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:43:00 -07:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 10:54:55 -07:00
|
|
|
await _providerManager.SaveImage(
|
|
|
|
item,
|
2020-08-17 12:22:42 -07:00
|
|
|
stream,
|
2022-01-03 14:51:28 -07:00
|
|
|
response.Content.Headers.ContentType?.MediaType,
|
2020-08-17 10:54:55 -07:00
|
|
|
imageType,
|
|
|
|
null,
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2022-01-03 14:51:28 -07:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
2021-09-21 13:47:30 -07:00
|
|
|
catch (HttpRequestException)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
}
|