2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2014-01-28 18:37:01 +00:00
|
|
|
using System.Collections.Generic;
|
2014-02-02 13:36:31 +00:00
|
|
|
using System.IO;
|
2014-01-28 18:37:01 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2020-11-14 21:30:34 +00:00
|
|
|
using System.Net.Http;
|
2014-01-28 18:37:01 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2015-11-08 05:04:38 +00:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2016-02-26 04:09:42 +00:00
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-10-16 17:06:31 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
{
|
|
|
|
public class ItemImageProvider
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IProviderManager _providerManager;
|
2014-01-31 19:55:21 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Image types that are only one per item.
|
|
|
|
/// </summary>
|
2021-05-23 22:30:41 +00:00
|
|
|
private static readonly ImageType[] _singularImages =
|
2020-09-07 11:20:39 +00:00
|
|
|
{
|
|
|
|
ImageType.Primary,
|
|
|
|
ImageType.Art,
|
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Box,
|
|
|
|
ImageType.BoxRear,
|
|
|
|
ImageType.Disc,
|
|
|
|
ImageType.Logo,
|
|
|
|
ImageType.Menu,
|
|
|
|
ImageType.Thumb
|
|
|
|
};
|
|
|
|
|
2020-01-08 16:52:50 +00:00
|
|
|
public ItemImageProvider(ILogger logger, IProviderManager providerManager, IFileSystem fileSystem)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_providerManager = providerManager;
|
2014-02-02 13:36:31 +00:00
|
|
|
_fileSystem = fileSystem;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool ValidateImages(BaseItem item, IEnumerable<IImageProvider> providers, IDirectoryService directoryService)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2015-04-11 01:42:37 +00:00
|
|
|
var hasChanges = false;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2015-05-06 12:56:26 +00:00
|
|
|
if (!(item is Photo))
|
2015-04-11 01:42:37 +00:00
|
|
|
{
|
2020-03-09 15:10:02 +00:00
|
|
|
var images = providers.OfType<ILocalImageProvider>()
|
2015-05-01 18:37:01 +00:00
|
|
|
.SelectMany(i => i.GetImages(item, directoryService))
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
if (MergeImages(item, images))
|
|
|
|
{
|
|
|
|
hasChanges = true;
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanges;
|
|
|
|
}
|
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
public async Task<RefreshResult> RefreshImages(
|
|
|
|
BaseItem item,
|
|
|
|
LibraryOptions libraryOptions,
|
|
|
|
List<IImageProvider> providers,
|
|
|
|
ImageRefreshOptions refreshOptions,
|
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-06-15 02:24:04 +00:00
|
|
|
if (refreshOptions.IsReplacingImage(ImageType.Backdrop))
|
|
|
|
{
|
|
|
|
ClearImages(item, ImageType.Backdrop);
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2014-06-15 02:24:04 +00:00
|
|
|
if (refreshOptions.IsReplacingImage(ImageType.Screenshot))
|
|
|
|
{
|
|
|
|
ClearImages(item, ImageType.Screenshot);
|
|
|
|
}
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
var result = new RefreshResult { UpdateType = ItemUpdateType.None };
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var typeName = item.GetType().Name;
|
|
|
|
var typeOptions = libraryOptions.GetTypeOptions(typeName) ?? new TypeOptions { Type = typeName };
|
2014-01-29 05:17:58 +00:00
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
// In order to avoid duplicates, only download these if there are none already
|
2018-09-12 17:26:21 +00:00
|
|
|
var backdropLimit = typeOptions.GetLimit(ImageType.Backdrop);
|
|
|
|
var screenshotLimit = typeOptions.GetLimit(ImageType.Screenshot);
|
2014-06-15 02:24:04 +00:00
|
|
|
var downloadedImages = new List<ImageType>();
|
2014-02-02 13:36:31 +00:00
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
foreach (var provider in providers)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
if (provider is IRemoteImageProvider remoteProvider)
|
2014-02-07 00:43:45 +00:00
|
|
|
{
|
2021-06-27 00:00:17 +00:00
|
|
|
await RefreshFromProvider(item, remoteProvider, refreshOptions, typeOptions, backdropLimit, screenshotLimit, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-07 00:43:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
if (provider is IDynamicImageProvider dynamicImageProvider)
|
2014-02-07 00:43:45 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
await RefreshFromProvider(item, dynamicImageProvider, refreshOptions, typeOptions, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-07 00:43:45 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Refreshes from provider.
|
|
|
|
/// </summary>
|
2020-08-07 17:26:28 +00:00
|
|
|
private async Task RefreshFromProvider(
|
|
|
|
BaseItem item,
|
2014-06-13 14:24:14 +00:00
|
|
|
IDynamicImageProvider provider,
|
|
|
|
ImageRefreshOptions refreshOptions,
|
2018-09-12 17:26:21 +00:00
|
|
|
TypeOptions savedOptions,
|
2014-06-15 02:24:04 +00:00
|
|
|
ICollection<ImageType> downloadedImages,
|
2014-06-13 14:24:14 +00:00
|
|
|
RefreshResult result,
|
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-01-29 16:16:24 +00:00
|
|
|
var images = provider.GetSupportedImages(item);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-01-29 16:16:24 +00:00
|
|
|
foreach (var imageType in images)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
if (!IsEnabled(savedOptions, imageType))
|
2020-06-20 08:35:29 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-15 02:24:04 +00:00
|
|
|
|
2015-11-21 03:13:03 +00:00
|
|
|
if (!HasImage(item, imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType)))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogDebug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2014-01-29 16:16:24 +00:00
|
|
|
var response = await provider.GetImage(item, imageType, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-01-29 16:16:24 +00:00
|
|
|
if (response.HasImage)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
if (!string.IsNullOrEmpty(response.Path))
|
|
|
|
{
|
2015-10-16 17:06:31 +00:00
|
|
|
if (response.Protocol == MediaProtocol.Http)
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogDebug("Setting image url into item {0}", item.Id);
|
2020-08-07 17:26:28 +00:00
|
|
|
item.SetImage(
|
|
|
|
new ItemImageInfo
|
|
|
|
{
|
|
|
|
Path = response.Path,
|
|
|
|
Type = imageType
|
|
|
|
},
|
|
|
|
0);
|
2015-10-16 17:06:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var mimeType = MimeTypes.GetMimeType(response.Path);
|
|
|
|
|
2020-01-08 16:52:50 +00:00
|
|
|
var stream = new FileStream(response.Path, FileMode.Open, FileAccess.Read, FileShare.Read, IODefaults.FileStreamBufferSize, true);
|
2015-10-16 17:06:31 +00:00
|
|
|
|
2016-10-17 16:35:29 +00:00
|
|
|
await _providerManager.SaveImage(item, stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
|
2015-10-16 17:06:31 +00:00
|
|
|
}
|
2014-01-31 19:55:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-27 11:03:43 +00:00
|
|
|
var mimeType = "image/" + response.Format.ToString().ToLowerInvariant();
|
2014-01-31 19:55:21 +00:00
|
|
|
|
2016-10-17 16:35:29 +00:00
|
|
|
await _providerManager.SaveImage(item, response.Stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
|
2014-01-31 19:55:21 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-06-15 02:24:04 +00:00
|
|
|
downloadedImages.Add(imageType);
|
2020-08-07 17:26:28 +00:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2014-01-29 16:16:24 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
result.ErrorMessage = ex.Message;
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error in {provider}", provider.Name);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private bool HasImage(BaseItem item, ImageType type)
|
2015-11-21 03:13:03 +00:00
|
|
|
{
|
2017-10-01 17:26:09 +00:00
|
|
|
return item.HasImage(type);
|
2015-11-21 03:13:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Determines if an item already contains the given images.
|
2014-01-28 18:37:01 +00:00
|
|
|
/// </summary>
|
2014-02-02 13:36:31 +00:00
|
|
|
/// <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>
|
|
|
|
/// <param name="screenshotLimit">The screenshot limit.</param>
|
|
|
|
/// <returns><c>true</c> if the specified item contains images; otherwise, <c>false</c>.</returns>
|
2018-09-12 17:26:21 +00:00
|
|
|
private bool ContainsImages(BaseItem item, List<ImageType> images, TypeOptions savedOptions, int backdropLimit, int screenshotLimit)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
// Using .Any causes the creation of a DisplayClass aka. variable capture
|
|
|
|
for (var i = 0; i < _singularImages.Length; i++)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
var type = _singularImages[i];
|
|
|
|
if (images.Contains(type) && !HasImage(item, type) && savedOptions.GetLimit(type) > 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
if (images.Contains(ImageType.Backdrop) && item.GetImages(ImageType.Backdrop).Count() < backdropLimit)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-15 02:24:04 +00:00
|
|
|
if (images.Contains(ImageType.Screenshot) && item.GetImages(ImageType.Screenshot).Count() < screenshotLimit)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Refreshes from provider.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="provider">The provider.</param>
|
2014-02-02 13:36:31 +00:00
|
|
|
/// <param name="refreshOptions">The refresh options.</param>
|
|
|
|
/// <param name="savedOptions">The saved options.</param>
|
|
|
|
/// <param name="backdropLimit">The backdrop limit.</param>
|
|
|
|
/// <param name="screenshotLimit">The screenshot limit.</param>
|
2014-06-15 02:24:04 +00:00
|
|
|
/// <param name="downloadedImages">The downloaded images.</param>
|
2014-01-28 18:37:01 +00:00
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2020-04-05 16:10:56 +00:00
|
|
|
private async Task RefreshFromProvider(
|
|
|
|
BaseItem item,
|
2014-10-04 18:05:24 +00:00
|
|
|
IRemoteImageProvider provider,
|
|
|
|
ImageRefreshOptions refreshOptions,
|
2018-09-12 17:26:21 +00:00
|
|
|
TypeOptions savedOptions,
|
2014-10-04 18:05:24 +00:00
|
|
|
int backdropLimit,
|
|
|
|
int screenshotLimit,
|
2014-06-15 02:24:04 +00:00
|
|
|
ICollection<ImageType> downloadedImages,
|
2014-10-04 18:05:24 +00:00
|
|
|
RefreshResult result,
|
2014-06-15 02:24:04 +00:00
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-08-14 13:24:30 +00:00
|
|
|
if (!item.SupportsRemoteImageDownloading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-04 18:05:24 +00:00
|
|
|
if (!refreshOptions.ReplaceAllImages &&
|
2018-09-12 17:26:21 +00:00
|
|
|
refreshOptions.ReplaceImages.Length == 0 &&
|
2014-06-15 02:24:04 +00:00
|
|
|
ContainsImages(item, provider.GetSupportedImages(item).ToList(), savedOptions, backdropLimit, screenshotLimit))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogDebug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
2014-02-07 20:30:41 +00:00
|
|
|
|
2020-04-05 16:10:56 +00:00
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(
|
|
|
|
item,
|
|
|
|
new RemoteImageQuery(provider.Name)
|
|
|
|
{
|
|
|
|
IncludeAllLanguages = false,
|
|
|
|
IncludeDisabledProviders = false,
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2014-02-12 03:46:27 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
var list = images.ToList();
|
2014-02-12 03:33:30 +00:00
|
|
|
int minWidth;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-06-15 02:24:04 +00:00
|
|
|
foreach (var imageType in _singularImages)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
if (!IsEnabled(savedOptions, imageType))
|
2020-04-05 16:10:56 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-15 02:24:04 +00:00
|
|
|
|
2015-11-21 03:13:03 +00:00
|
|
|
if (!HasImage(item, imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType)))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-06-15 02:24:04 +00:00
|
|
|
minWidth = savedOptions.GetMinWidth(imageType);
|
2021-06-27 00:00:17 +00:00
|
|
|
var downloaded = await DownloadImage(item, provider, result, list, minWidth, imageType, cancellationToken).ConfigureAwait(false);
|
2014-06-15 02:24:04 +00:00
|
|
|
|
|
|
|
if (downloaded)
|
|
|
|
{
|
|
|
|
downloadedImages.Add(imageType);
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Backdrop);
|
2021-06-27 00:00:17 +00:00
|
|
|
await DownloadBackdrops(item, ImageType.Backdrop, backdropLimit, provider, result, list, minWidth, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
if (item is IHasScreenshots hasScreenshots)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Screenshot);
|
2021-06-27 00:00:17 +00:00
|
|
|
await DownloadBackdrops(item, ImageType.Screenshot, screenshotLimit, provider, result, list, minWidth, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
result.ErrorMessage = ex.Message;
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error in {provider}", provider.Name);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
private bool IsEnabled(TypeOptions options, ImageType type)
|
2014-10-04 18:05:24 +00:00
|
|
|
{
|
|
|
|
return options.IsEnabled(type);
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private void ClearImages(BaseItem item, ImageType type)
|
2014-06-15 02:24:04 +00:00
|
|
|
{
|
|
|
|
var deleted = false;
|
2016-01-02 23:45:21 +00:00
|
|
|
var deletedImages = new List<ItemImageInfo>();
|
2014-06-15 02:24:04 +00:00
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
foreach (var image in item.GetImages(type))
|
2014-06-15 02:24:04 +00:00
|
|
|
{
|
2015-12-30 02:42:17 +00:00
|
|
|
if (!image.IsLocalFile)
|
|
|
|
{
|
2016-01-02 23:45:21 +00:00
|
|
|
deletedImages.Add(image);
|
2015-12-30 02:42:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-24 15:03:49 +00:00
|
|
|
try
|
2014-06-15 02:24:04 +00:00
|
|
|
{
|
2017-03-24 15:03:49 +00:00
|
|
|
_fileSystem.DeleteFile(image.Path);
|
2014-06-15 02:24:04 +00:00
|
|
|
deleted = true;
|
|
|
|
}
|
2017-03-24 15:03:49 +00:00
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
|
|
|
}
|
2014-06-15 02:24:04 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
item.RemoveImages(deletedImages);
|
2016-01-02 23:45:21 +00:00
|
|
|
|
2014-06-15 02:24:04 +00:00
|
|
|
if (deleted)
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
item.ValidateImages(new DirectoryService(_fileSystem));
|
2014-06-15 02:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
public bool MergeImages(BaseItem item, IReadOnlyList<LocalImageInfo> images)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
var changed = false;
|
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
for (var i = 0; i < _singularImages.Length; i++)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
var type = _singularImages[i];
|
|
|
|
var image = GetFirstLocalImageInfoByType(images, type);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
var currentImage = item.GetImageInfo(type, 0);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-05-14 00:46:45 +00:00
|
|
|
if (currentImage == null)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-02-07 22:40:03 +00:00
|
|
|
item.SetImagePath(type, image.FileInfo);
|
2014-01-28 18:37:01 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
2015-04-11 01:42:37 +00:00
|
|
|
else if (!string.Equals(currentImage.Path, image.FileInfo.FullName, StringComparison.OrdinalIgnoreCase))
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
|
|
|
item.SetImagePath(type, image.FileInfo);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-30 04:52:30 +00: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-14 00:46:45 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
2015-04-11 01:42:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var existing = item.GetImageInfo(type, 0);
|
2015-10-20 22:06:49 +00:00
|
|
|
if (existing != null)
|
2015-04-11 01:42:37 +00:00
|
|
|
{
|
2019-01-26 21:59:53 +00:00
|
|
|
if (existing.IsLocalFile && !File.Exists(existing.Path))
|
2015-10-20 22:06:49 +00:00
|
|
|
{
|
|
|
|
item.RemoveImage(existing);
|
|
|
|
changed = true;
|
|
|
|
}
|
2015-04-11 01:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 00:46:45 +00:00
|
|
|
if (UpdateMultiImages(item, images, ImageType.Backdrop))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-05-14 00:46:45 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-05-14 00:46:45 +00:00
|
|
|
var hasScreenshots = item as IHasScreenshots;
|
|
|
|
if (hasScreenshots != null)
|
|
|
|
{
|
|
|
|
if (UpdateMultiImages(item, images, ImageType.Screenshot))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-05-14 00:46:45 +00:00
|
|
|
changed = true;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 00:46:45 +00:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2021-05-23 22:30:41 +00: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-27 00:00:17 +00:00
|
|
|
private bool UpdateMultiImages(BaseItem item, IReadOnlyList<LocalImageInfo> images, ImageType type)
|
2014-05-14 00:46:45 +00:00
|
|
|
{
|
|
|
|
var changed = false;
|
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
var newImageFileInfos = images
|
2021-06-27 00:00:17 +00:00
|
|
|
.Where(i => i.Type == type)
|
2021-05-23 22:30:41 +00:00
|
|
|
.Select(i => i.FileInfo)
|
|
|
|
.ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2015-05-06 12:56:26 +00:00
|
|
|
if (item.AddImages(type, newImageFileInfos))
|
|
|
|
{
|
|
|
|
changed = true;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
private async Task<bool> DownloadImage(
|
|
|
|
BaseItem item,
|
2014-10-04 18:05:24 +00:00
|
|
|
IRemoteImageProvider provider,
|
|
|
|
RefreshResult result,
|
|
|
|
IEnumerable<RemoteImageInfo> images,
|
|
|
|
int minWidth,
|
|
|
|
ImageType type,
|
2014-06-15 02:24:04 +00:00
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2016-01-24 04:21:31 +00:00
|
|
|
var eligibleImages = images
|
|
|
|
.Where(i => i.Type == type && !(i.Width.HasValue && i.Width.Value < minWidth))
|
|
|
|
.ToList();
|
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
if (EnableImageStub(item) && eligibleImages.Count > 0)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2016-01-24 04:21:31 +00:00
|
|
|
SaveImageStub(item, type, eligibleImages.Select(i => i.Url));
|
2020-08-07 17:26:28 +00:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2016-01-24 04:21:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-12 03:33:30 +00:00
|
|
|
|
2016-01-24 04:21:31 +00:00
|
|
|
foreach (var image in eligibleImages)
|
|
|
|
{
|
2014-01-28 18:37:01 +00:00
|
|
|
var url = image.Url;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-08-17 17:54:55 +00:00
|
|
|
using var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
2021-04-01 13:27:28 +00:00
|
|
|
response.EnsureSuccessStatusCode();
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-17 17:54:55 +00:00
|
|
|
await _providerManager.SaveImage(
|
|
|
|
item,
|
2020-08-17 19:22:42 +00:00
|
|
|
stream,
|
2020-08-17 17:54:55 +00:00
|
|
|
response.Content.Headers.ContentType.MediaType,
|
|
|
|
type,
|
|
|
|
null,
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2014-06-15 02:24:04 +00:00
|
|
|
return true;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
2020-11-14 21:30:34 +00:00
|
|
|
catch (HttpRequestException ex)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-02-08 20:02:35 +00:00
|
|
|
// Sometimes providers send back bad url's. Just move to the next image
|
2020-06-22 08:06:35 +00:00
|
|
|
if (ex.StatusCode.HasValue
|
|
|
|
&& (ex.StatusCode.Value == HttpStatusCode.NotFound || ex.StatusCode.Value == HttpStatusCode.Forbidden))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-15 02:24:04 +00:00
|
|
|
|
|
|
|
return false;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
private bool EnableImageStub(BaseItem item)
|
2015-10-17 15:05:12 +00:00
|
|
|
{
|
2016-02-26 04:09:42 +00:00
|
|
|
if (item is LiveTvProgram)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!item.IsFileProtocol)
|
2015-10-17 15:05:12 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-08 05:04:38 +00:00
|
|
|
if (item is IItemByName && !(item is MusicArtist))
|
|
|
|
{
|
|
|
|
var hasDualAccess = item as IHasDualAccess;
|
|
|
|
if (hasDualAccess == null || hasDualAccess.IsAccessedByName)
|
|
|
|
{
|
2015-11-20 17:55:38 +00:00
|
|
|
return true;
|
2015-11-08 05:04:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:28:07 +00:00
|
|
|
// We always want to use prefetched images
|
2020-05-27 17:13:41 +00:00
|
|
|
return false;
|
2015-10-17 15:05:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private void SaveImageStub(BaseItem item, ImageType imageType, IEnumerable<string> urls)
|
2015-10-17 15:05:12 +00:00
|
|
|
{
|
|
|
|
var newIndex = item.AllowsMultipleImages(imageType) ? item.GetImages(imageType).Count() : 0;
|
|
|
|
|
2016-01-24 04:21:31 +00:00
|
|
|
SaveImageStub(item, imageType, urls, newIndex);
|
2015-11-09 18:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private void SaveImageStub(BaseItem item, ImageType imageType, IEnumerable<string> urls, int newIndex)
|
2015-11-09 18:18:37 +00:00
|
|
|
{
|
2020-09-07 11:20:39 +00:00
|
|
|
var path = string.Join('|', urls.Take(1));
|
2015-10-17 15:05:12 +00:00
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
item.SetImage(
|
|
|
|
new ItemImageInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Type = imageType
|
|
|
|
},
|
|
|
|
newIndex);
|
2015-10-17 15:05:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
private async Task DownloadBackdrops(BaseItem item, ImageType imageType, int limit, IRemoteImageProvider provider, RefreshResult result, IEnumerable<RemoteImageInfo> images, int minWidth, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
foreach (var image in images.Where(i => i.Type == imageType))
|
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
if (item.GetImages(imageType).Count() >= limit)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-12 03:33:30 +00:00
|
|
|
if (image.Width.HasValue && image.Width.Value < minWidth)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
var url = image.Url;
|
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
if (EnableImageStub(item))
|
2015-10-17 15:05:12 +00:00
|
|
|
{
|
2016-01-24 04:21:31 +00:00
|
|
|
SaveImageStub(item, imageType, new[] { url });
|
2020-08-07 17:26:28 +00:00
|
|
|
result.UpdateType |= ItemUpdateType.ImageUpdate;
|
2015-11-09 18:18:37 +00:00
|
|
|
continue;
|
2015-10-17 15:05:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
try
|
|
|
|
{
|
2020-08-17 17:54:55 +00:00
|
|
|
using var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-02-25 15:40:16 +00:00
|
|
|
// If there's already an image of the same size, skip it
|
2020-08-17 17:54:55 +00:00
|
|
|
if (response.Content.Headers.ContentLength.HasValue)
|
2014-02-25 15:40:16 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-08-17 17:54:55 +00:00
|
|
|
if (item.GetImages(imageType).Any(i => _fileSystem.GetFileInfo(i.Path).Length == response.Content.Headers.ContentLength.Value))
|
2014-02-25 15:40:16 +00:00
|
|
|
{
|
|
|
|
response.Content.Dispose();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error examining images");
|
2014-02-25 15:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 17:54:55 +00:00
|
|
|
await _providerManager.SaveImage(
|
|
|
|
item,
|
2020-08-17 19:22:42 +00:00
|
|
|
stream,
|
2020-08-17 17:54:55 +00:00
|
|
|
response.Content.Headers.ContentType.MediaType,
|
|
|
|
imageType,
|
|
|
|
null,
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
|
|
|
}
|
2020-11-14 21:30:34 +00:00
|
|
|
catch (HttpRequestException ex)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
// Sometimes providers send back bad urls. Just move onto the next image
|
2020-06-22 08:06:35 +00:00
|
|
|
if (ex.StatusCode.HasValue
|
|
|
|
&& (ex.StatusCode.Value == HttpStatusCode.NotFound || ex.StatusCode.Value == HttpStatusCode.Forbidden))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 13:18:25 +00:00
|
|
|
}
|