2014-02-02 06:36:31 -07:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-01-28 22:17:58 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-02-02 06:36:31 -07:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
|
using System;
|
|
|
|
|
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;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-11-07 22:04:38 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-10-29 13:02:21 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-02-25 21:09:42 -07:00
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2015-10-16 10:06:31 -07:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2017-08-09 12:56:38 -07:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
|
{
|
|
|
|
|
public class ItemImageProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-01-31 12:55:21 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
2014-02-02 06:36:31 -07:00
|
|
|
|
public ItemImageProvider(ILogger logger, IProviderManager providerManager, IServerConfigurationManager config, IFileSystem fileSystem)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_providerManager = providerManager;
|
|
|
|
|
_config = config;
|
2014-02-02 06:36:31 -07:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
public bool ValidateImages(IHasMetadata 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
|
|
|
|
|
2015-05-06 05:56:26 -07:00
|
|
|
|
if (!(item is Photo))
|
2015-04-10 18:42:37 -07:00
|
|
|
|
{
|
2015-05-06 05:56:26 -07:00
|
|
|
|
var images = providers.OfType<ILocalImageFileProvider>()
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 12:56:38 -07:00
|
|
|
|
public async Task<RefreshResult> RefreshImages(IHasMetadata item, LibraryOptions libraryOptions, List<IImageProvider> providers, ImageRefreshOptions refreshOptions, MetadataOptions savedOptions, CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
2014-06-14 19:24:04 -07:00
|
|
|
|
if (refreshOptions.IsReplacingImage(ImageType.Backdrop))
|
|
|
|
|
{
|
|
|
|
|
ClearImages(item, ImageType.Backdrop);
|
|
|
|
|
}
|
|
|
|
|
if (refreshOptions.IsReplacingImage(ImageType.Screenshot))
|
|
|
|
|
{
|
|
|
|
|
ClearImages(item, ImageType.Screenshot);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 11:39:41 -07:00
|
|
|
|
var result = new RefreshResult { UpdateType = ItemUpdateType.None };
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
2014-01-28 22:17:58 -07:00
|
|
|
|
var providerIds = new List<Guid>();
|
|
|
|
|
|
2014-02-02 06:36:31 -07:00
|
|
|
|
// In order to avoid duplicates, only download these if there are none already
|
2014-02-25 08:40:16 -07:00
|
|
|
|
var backdropLimit = savedOptions.GetLimit(ImageType.Backdrop);
|
|
|
|
|
var screenshotLimit = savedOptions.GetLimit(ImageType.Screenshot);
|
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
|
|
|
|
{
|
2014-02-06 17:43:45 -07:00
|
|
|
|
var remoteProvider = provider as IRemoteImageProvider;
|
2014-01-28 22:17:58 -07:00
|
|
|
|
|
2014-02-06 17:43:45 -07:00
|
|
|
|
if (remoteProvider != null)
|
|
|
|
|
{
|
2016-09-11 10:09:10 -07:00
|
|
|
|
await RefreshFromProvider(item, libraryOptions, remoteProvider, refreshOptions, savedOptions, backdropLimit, screenshotLimit, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-06 17:43:45 -07:00
|
|
|
|
providerIds.Add(provider.GetType().FullName.GetMD5());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
2014-02-06 17:43:45 -07:00
|
|
|
|
var dynamicImageProvider = provider as IDynamicImageProvider;
|
2014-01-28 22:17:58 -07:00
|
|
|
|
|
2014-02-06 17:43:45 -07:00
|
|
|
|
if (dynamicImageProvider != null)
|
|
|
|
|
{
|
2014-06-14 19:24:04 -07:00
|
|
|
|
await RefreshFromProvider(item, dynamicImageProvider, refreshOptions, savedOptions, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-06 17:43:45 -07:00
|
|
|
|
providerIds.Add(provider.GetType().FullName.GetMD5());
|
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 22:17:58 -07:00
|
|
|
|
result.Providers = providerIds;
|
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes from provider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="provider">The provider.</param>
|
2014-06-13 07:24:14 -07:00
|
|
|
|
/// <param name="refreshOptions">The refresh options.</param>
|
2014-02-02 06:36:31 -07:00
|
|
|
|
/// <param name="savedOptions">The saved options.</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>
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private async Task RefreshFromProvider(IHasMetadata item,
|
2014-06-13 07:24:14 -07:00
|
|
|
|
IDynamicImageProvider provider,
|
|
|
|
|
ImageRefreshOptions refreshOptions,
|
|
|
|
|
MetadataOptions 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
|
|
|
|
{
|
2014-10-04 11:05:24 -07:00
|
|
|
|
if (!IsEnabled(savedOptions, imageType, item)) continue;
|
2014-06-14 19:24:04 -07:00
|
|
|
|
|
2015-11-20 20:13:03 -07:00
|
|
|
|
if (!HasImage(item, imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType)))
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
2014-02-05 21:39:16 -07:00
|
|
|
|
_logger.Debug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Setting image url into item {0}", item.Id);
|
|
|
|
|
item.SetImage(new ItemImageInfo
|
|
|
|
|
{
|
|
|
|
|
Path = response.Path,
|
|
|
|
|
Type = imageType
|
|
|
|
|
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var mimeType = MimeTypes.GetMimeType(response.Path);
|
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
var stream = _fileSystem.GetFileStream(response.Path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true);
|
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
|
|
|
|
|
{
|
|
|
|
|
var mimeType = "image/" + response.Format.ToString().ToLower();
|
|
|
|
|
|
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);
|
2014-01-29 09:16:24 -07:00
|
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.ErrorMessage = ex.Message;
|
|
|
|
|
_logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Image types that are only one per item
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ImageType[] _singularImages =
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary,
|
|
|
|
|
ImageType.Art,
|
|
|
|
|
ImageType.Banner,
|
|
|
|
|
ImageType.Box,
|
|
|
|
|
ImageType.BoxRear,
|
|
|
|
|
ImageType.Disc,
|
|
|
|
|
ImageType.Logo,
|
|
|
|
|
ImageType.Menu,
|
|
|
|
|
ImageType.Thumb
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private bool HasImage(IHasMetadata item, ImageType type)
|
2015-11-20 20:13:03 -07:00
|
|
|
|
{
|
2017-10-01 10:26:09 -07:00
|
|
|
|
return item.HasImage(type);
|
2015-11-20 20:13:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if an item already contains the given images
|
|
|
|
|
/// </summary>
|
2014-02-02 06:36:31 -07: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>
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private bool ContainsImages(IHasMetadata item, List<ImageType> images, MetadataOptions savedOptions, int backdropLimit, int screenshotLimit)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
2015-11-20 20:13:03 -07:00
|
|
|
|
if (_singularImages.Any(i => images.Contains(i) && !HasImage(item, i) && savedOptions.GetLimit(i) > 0))
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 13:30:41 -07:00
|
|
|
|
if (images.Contains(ImageType.Backdrop) && item.GetImages(ImageType.Backdrop).Count() < backdropLimit)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-14 19:24:04 -07:00
|
|
|
|
if (images.Contains(ImageType.Screenshot) && item.GetImages(ImageType.Screenshot).Count() < screenshotLimit)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
2014-02-07 13:30:41 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes from provider.
|
|
|
|
|
/// </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>
|
|
|
|
|
/// <param name="screenshotLimit">The screenshot 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>
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private async Task RefreshFromProvider(IHasMetadata item, LibraryOptions libraryOptions,
|
2014-10-04 11:05:24 -07:00
|
|
|
|
IRemoteImageProvider provider,
|
|
|
|
|
ImageRefreshOptions refreshOptions,
|
|
|
|
|
MetadataOptions savedOptions,
|
|
|
|
|
int backdropLimit,
|
|
|
|
|
int screenshotLimit,
|
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 &&
|
|
|
|
|
refreshOptions.ReplaceImages.Count == 0 &&
|
2014-06-14 19:24:04 -07:00
|
|
|
|
ContainsImages(item, provider.GetSupportedImages(item).ToList(), savedOptions, backdropLimit, screenshotLimit))
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
2014-02-07 13:30:41 -07:00
|
|
|
|
|
2014-02-11 20:46:27 -07:00
|
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(item, new RemoteImageQuery
|
|
|
|
|
{
|
|
|
|
|
ProviderName = provider.Name,
|
|
|
|
|
IncludeAllLanguages = false,
|
|
|
|
|
IncludeDisabledProviders = false,
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
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
|
|
|
|
{
|
2014-10-04 11:05:24 -07:00
|
|
|
|
if (!IsEnabled(savedOptions, imageType, item)) continue;
|
2014-06-14 19:24:04 -07:00
|
|
|
|
|
2015-11-20 20:13:03 -07:00
|
|
|
|
if (!HasImage(item, 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);
|
2016-09-11 10:09:10 -07:00
|
|
|
|
var downloaded = await DownloadImage(item, libraryOptions, 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 11:05:24 -07:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Backdrops))
|
|
|
|
|
{
|
|
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Backdrop);
|
2016-09-11 10:09:10 -07:00
|
|
|
|
await DownloadBackdrops(item, libraryOptions, ImageType.Backdrop, backdropLimit, provider, result, list, minWidth, cancellationToken).ConfigureAwait(false);
|
2014-10-04 11:05:24 -07:00
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
2014-10-04 11:05:24 -07:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Screenshots))
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
2014-10-04 11:05:24 -07:00
|
|
|
|
var hasScreenshots = item as IHasScreenshots;
|
|
|
|
|
if (hasScreenshots != null)
|
|
|
|
|
{
|
|
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Screenshot);
|
2016-09-11 10:09:10 -07:00
|
|
|
|
await DownloadBackdrops(item, libraryOptions, ImageType.Screenshot, screenshotLimit, provider, result, list, minWidth, cancellationToken).ConfigureAwait(false);
|
2014-10-04 11:05:24 -07:00
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
result.ErrorMessage = ex.Message;
|
|
|
|
|
_logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private bool IsEnabled(MetadataOptions options, ImageType type, IHasMetadata item)
|
2014-10-04 11:05:24 -07:00
|
|
|
|
{
|
|
|
|
|
if (type == ImageType.Backdrop)
|
|
|
|
|
{
|
|
|
|
|
if (item.LockedFields.Contains(MetadataFields.Backdrops))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (type == ImageType.Screenshot)
|
|
|
|
|
{
|
|
|
|
|
if (item.LockedFields.Contains(MetadataFields.Screenshots))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (item.LockedFields.Contains(MetadataFields.Images))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return options.IsEnabled(type);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private void ClearImages(IHasMetadata item, ImageType type)
|
2014-06-14 19:24:04 -07:00
|
|
|
|
{
|
|
|
|
|
var deleted = false;
|
2016-01-02 16:45:21 -07:00
|
|
|
|
var deletedImages = new List<ItemImageInfo>();
|
2014-06-14 19:24:04 -07:00
|
|
|
|
|
|
|
|
|
foreach (var image in item.GetImages(type).ToList())
|
|
|
|
|
{
|
2015-12-29 19:42:17 -07:00
|
|
|
|
if (!image.IsLocalFile)
|
|
|
|
|
{
|
2016-01-02 16:45:21 -07:00
|
|
|
|
deletedImages.Add(image);
|
2015-12-29 19:42:17 -07:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 08:03:49 -07:00
|
|
|
|
try
|
2014-06-14 19:24:04 -07:00
|
|
|
|
{
|
2017-03-24 08:03:49 -07:00
|
|
|
|
_fileSystem.DeleteFile(image.Path);
|
2014-06-14 19:24:04 -07:00
|
|
|
|
deleted = true;
|
|
|
|
|
}
|
2017-03-24 08:03:49 -07:00
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2014-06-14 19:24:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 12:56:38 -07:00
|
|
|
|
item.RemoveImages(deletedImages);
|
2016-01-02 16:45:21 -07:00
|
|
|
|
|
2014-06-14 19:24:04 -07:00
|
|
|
|
if (deleted)
|
|
|
|
|
{
|
2015-09-13 14:32:02 -07:00
|
|
|
|
item.ValidateImages(new DirectoryService(_logger, _fileSystem));
|
2014-06-14 19:24:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
public bool MergeImages(IHasMetadata item, List<LocalImageInfo> images)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
|
|
foreach (var type in _singularImages)
|
|
|
|
|
{
|
|
|
|
|
var image = images.FirstOrDefault(i => i.Type == type);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-05-13 17:46:45 -07:00
|
|
|
|
if (currentImage == null)
|
2014-01-28 11:37:01 -07:00
|
|
|
|
{
|
2014-02-07 15:40:03 -07:00
|
|
|
|
item.SetImagePath(type, image.FileInfo);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
changed = true;
|
|
|
|
|
}
|
2015-04-10 18:42:37 -07:00
|
|
|
|
else if (!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
|
|
|
|
}
|
2015-04-10 18:42:37 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var existing = item.GetImageInfo(type, 0);
|
2015-10-20 15:06:49 -07:00
|
|
|
|
if (existing != null)
|
2015-04-10 18:42:37 -07:00
|
|
|
|
{
|
2015-10-20 15:06:49 -07:00
|
|
|
|
if (existing.IsLocalFile && !_fileSystem.FileExists(existing.Path))
|
|
|
|
|
{
|
|
|
|
|
item.RemoveImage(existing);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
2015-04-10 18:42:37 -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
|
|
|
|
var hasScreenshots = item as IHasScreenshots;
|
|
|
|
|
if (hasScreenshots != null)
|
|
|
|
|
{
|
|
|
|
|
if (UpdateMultiImages(item, images, ImageType.Screenshot))
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private bool UpdateMultiImages(IHasMetadata item, List<LocalImageInfo> images, ImageType type)
|
2014-05-13 17:46:45 -07:00
|
|
|
|
{
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
2015-04-10 18:42:37 -07:00
|
|
|
|
var newImages = images.Where(i => i.Type == type).ToList();
|
2015-05-06 05:56:26 -07:00
|
|
|
|
|
|
|
|
|
var newImageFileInfos = newImages
|
2014-02-07 15:40:03 -07:00
|
|
|
|
.Select(i => i.FileInfo)
|
2017-08-09 14:08:01 -07:00
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private async Task<bool> DownloadImage(IHasMetadata item, LibraryOptions libraryOptions,
|
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
|
|
|
|
|
.Where(i => i.Type == type && !(i.Width.HasValue && i.Width.Value < minWidth))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2016-09-11 10:09:10 -07:00
|
|
|
|
if (EnableImageStub(item, type, libraryOptions) && 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));
|
|
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-02-22 13:20:22 -07:00
|
|
|
|
await _providerManager.SaveImage(item, response.Content, response.ContentType, type, null, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
2014-06-14 19:24:04 -07:00
|
|
|
|
return true;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
2014-02-08 13:02:35 -07:00
|
|
|
|
// Sometimes providers send back bad url's. Just move to the next image
|
2014-01-28 11:37:01 -07:00
|
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-14 19:24:04 -07:00
|
|
|
|
|
|
|
|
|
return false;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private bool EnableImageStub(IHasMetadata item, ImageType type, LibraryOptions libraryOptions)
|
2015-10-17 08:05:12 -07:00
|
|
|
|
{
|
2016-02-25 21:09:42 -07:00
|
|
|
|
if (item is LiveTvProgram)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 08:05:12 -07:00
|
|
|
|
if (item.LocationType == LocationType.Remote || item.LocationType == LocationType.Virtual)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-07 22:04:38 -07:00
|
|
|
|
if (item is IItemByName && !(item is MusicArtist))
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 23:24:22 -07:00
|
|
|
|
if (libraryOptions.DownloadImagesInAdvance)
|
2015-11-20 10:55:38 -07:00
|
|
|
|
{
|
2017-10-10 23:24:22 -07:00
|
|
|
|
return false;
|
2015-11-20 10:55:38 -07:00
|
|
|
|
}
|
2017-10-10 23:24:22 -07:00
|
|
|
|
|
|
|
|
|
//if (!item.IsSaveLocalMetadataEnabled())
|
|
|
|
|
//{
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return true;
|
2015-10-17 08:05:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private void SaveImageStub(IHasMetadata 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
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private void SaveImageStub(IHasMetadata item, ImageType imageType, IEnumerable<string> urls, int newIndex)
|
2015-11-09 11:18:37 -07:00
|
|
|
|
{
|
2016-01-23 21:21:31 -07:00
|
|
|
|
var path = string.Join("|", urls.Take(1).ToArray());
|
|
|
|
|
|
2015-10-17 08:05:12 -07:00
|
|
|
|
item.SetImage(new ItemImageInfo
|
|
|
|
|
{
|
2016-01-23 21:21:31 -07:00
|
|
|
|
Path = path,
|
2015-10-17 08:05:12 -07:00
|
|
|
|
Type = imageType
|
|
|
|
|
|
|
|
|
|
}, newIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private async Task DownloadBackdrops(IHasMetadata item, LibraryOptions libraryOptions, ImageType imageType, 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;
|
|
|
|
|
|
2016-09-11 10:09:10 -07:00
|
|
|
|
if (EnableImageStub(item, imageType, libraryOptions))
|
2015-10-17 08:05:12 -07:00
|
|
|
|
{
|
2016-01-23 21:21:31 -07:00
|
|
|
|
SaveImageStub(item, imageType, new[] { url });
|
2015-10-17 08:05:12 -07:00
|
|
|
|
result.UpdateType = 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
|
|
|
|
|
{
|
|
|
|
|
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-02-25 08:40:16 -07:00
|
|
|
|
// If there's already an image of the same size, skip it
|
|
|
|
|
if (response.ContentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-10-27 11:30:20 -07:00
|
|
|
|
if (item.GetImages(imageType).Any(i => _fileSystem.GetFileInfo(i.Path).Length == response.ContentLength.Value))
|
2014-02-25 08:40:16 -07:00
|
|
|
|
{
|
|
|
|
|
response.Content.Dispose();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error examining images", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 13:20:22 -07:00
|
|
|
|
await _providerManager.SaveImage(item, response.Content, response.ContentType, imageType, null, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
// Sometimes providers send back bad url's. Just move onto the next image
|
|
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-20 20:13:03 -07:00
|
|
|
|
}
|