2014-02-02 13:36:31 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-01-31 19:55:21 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-02-02 13:36:31 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-01-28 18:37:01 +00: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 13:36:31 +00:00
|
|
|
|
using System.IO;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
|
{
|
|
|
|
|
public class ItemImageProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-01-31 19:55:21 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
public ItemImageProvider(ILogger logger, IProviderManager providerManager, IServerConfigurationManager config, IFileSystem fileSystem)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_providerManager = providerManager;
|
|
|
|
|
_config = config;
|
2014-02-02 13:36:31 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool ValidateImages(IHasImages 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
|
|
|
|
{
|
2015-05-06 12:56:26 +00:00
|
|
|
|
var images = providers.OfType<ILocalImageFileProvider>()
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
public async Task<RefreshResult> RefreshImages(IHasImages item, IEnumerable<IImageProvider> imageProviders, ImageRefreshOptions refreshOptions, MetadataOptions savedOptions, 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);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2014-02-11 04:55:01 +00:00
|
|
|
|
var providers = imageProviders.ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
var providerIds = new List<Guid>();
|
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
|
// In order to avoid duplicates, only download these if there are none already
|
2014-02-25 15:40:16 +00:00
|
|
|
|
var backdropLimit = savedOptions.GetLimit(ImageType.Backdrop);
|
|
|
|
|
var screenshotLimit = savedOptions.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
|
|
|
|
{
|
2014-02-07 00:43:45 +00:00
|
|
|
|
var remoteProvider = provider as IRemoteImageProvider;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
if (remoteProvider != null)
|
|
|
|
|
{
|
2014-06-15 02:24:04 +00:00
|
|
|
|
await RefreshFromProvider(item, remoteProvider, refreshOptions, savedOptions, backdropLimit, screenshotLimit, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-07 00:43:45 +00:00
|
|
|
|
providerIds.Add(provider.GetType().FullName.GetMD5());
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
var dynamicImageProvider = provider as IDynamicImageProvider;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
if (dynamicImageProvider != null)
|
|
|
|
|
{
|
2014-06-15 02:24:04 +00:00
|
|
|
|
await RefreshFromProvider(item, dynamicImageProvider, refreshOptions, savedOptions, downloadedImages, result, cancellationToken).ConfigureAwait(false);
|
2014-02-07 00:43:45 +00:00
|
|
|
|
providerIds.Add(provider.GetType().FullName.GetMD5());
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
result.Providers = providerIds;
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refreshes from provider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="provider">The provider.</param>
|
2014-06-13 14:24:14 +00:00
|
|
|
|
/// <param name="refreshOptions">The refresh options.</param>
|
2014-02-02 13:36:31 +00:00
|
|
|
|
/// <param name="savedOptions">The saved options.</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>
|
2014-06-13 14:24:14 +00:00
|
|
|
|
private async Task RefreshFromProvider(IHasImages item,
|
|
|
|
|
IDynamicImageProvider provider,
|
|
|
|
|
ImageRefreshOptions refreshOptions,
|
|
|
|
|
MetadataOptions 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
|
|
|
|
{
|
2014-10-04 18:05:24 +00:00
|
|
|
|
if (!IsEnabled(savedOptions, imageType, item)) continue;
|
2014-06-15 02:24:04 +00:00
|
|
|
|
|
|
|
|
|
if (!item.HasImage(imageType) || (refreshOptions.IsReplacingImage(imageType) && !downloadedImages.Contains(imageType)))
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
_logger.Debug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
|
|
|
|
|
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-03-13 19:37:19 +00:00
|
|
|
|
var mimeType = MimeTypes.GetMimeType(response.Path);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
|
var stream = _fileSystem.GetFileStream(response.Path, FileMode.Open, FileAccess.Read, FileShare.Read, true);
|
2014-01-31 19:55:21 +00:00
|
|
|
|
|
2015-03-13 19:37:19 +00:00
|
|
|
|
await _providerManager.SaveImage(item, stream, mimeType, imageType, null, response.InternalCacheKey, cancellationToken).ConfigureAwait(false);
|
2014-01-31 19:55:21 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var mimeType = "image/" + response.Format.ToString().ToLower();
|
|
|
|
|
|
2015-03-13 19:37:19 +00:00
|
|
|
|
await _providerManager.SaveImage(item, response.Stream, mimeType, imageType, null, response.InternalCacheKey, 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);
|
2014-01-29 16:16:24 +00:00
|
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00: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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if an item already contains the given images
|
|
|
|
|
/// </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>
|
|
|
|
|
private bool ContainsImages(IHasImages item, List<ImageType> images, MetadataOptions savedOptions, int backdropLimit, int screenshotLimit)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-02-02 13:36:31 +00:00
|
|
|
|
if (_singularImages.Any(i => images.Contains(i) && !item.HasImage(i) && savedOptions.GetLimit(i) > 0))
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
2014-10-04 18:05:24 +00:00
|
|
|
|
private async Task RefreshFromProvider(IHasImages item,
|
|
|
|
|
IRemoteImageProvider provider,
|
|
|
|
|
ImageRefreshOptions refreshOptions,
|
|
|
|
|
MetadataOptions savedOptions,
|
|
|
|
|
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 &&
|
|
|
|
|
refreshOptions.ReplaceImages.Count == 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Running {0} for {1}", provider.GetType().Name, item.Path ?? item.Name);
|
2014-02-07 20:30:41 +00:00
|
|
|
|
|
2014-02-12 03:46:27 +00:00
|
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(item, new RemoteImageQuery
|
|
|
|
|
{
|
|
|
|
|
ProviderName = provider.Name,
|
|
|
|
|
IncludeAllLanguages = false,
|
|
|
|
|
IncludeDisabledProviders = false,
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
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
|
|
|
|
{
|
2014-10-04 18:05:24 +00:00
|
|
|
|
if (!IsEnabled(savedOptions, imageType, item)) continue;
|
2014-06-15 02:24:04 +00:00
|
|
|
|
|
|
|
|
|
if (!item.HasImage(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);
|
|
|
|
|
var downloaded = await DownloadImage(item, provider, result, list, minWidth, imageType, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (downloaded)
|
|
|
|
|
{
|
|
|
|
|
downloadedImages.Add(imageType);
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 18:05:24 +00:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Backdrops))
|
|
|
|
|
{
|
|
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Backdrop);
|
|
|
|
|
await DownloadBackdrops(item, ImageType.Backdrop, backdropLimit, provider, result, list, minWidth, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
2014-10-04 18:05:24 +00:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Screenshots))
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
2014-10-04 18:05:24 +00:00
|
|
|
|
var hasScreenshots = item as IHasScreenshots;
|
|
|
|
|
if (hasScreenshots != null)
|
|
|
|
|
{
|
|
|
|
|
minWidth = savedOptions.GetMinWidth(ImageType.Screenshot);
|
|
|
|
|
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;
|
|
|
|
|
_logger.ErrorException("Error in {0}", ex, provider.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 18:05:24 +00:00
|
|
|
|
private bool IsEnabled(MetadataOptions options, ImageType type, IHasImages item)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-15 02:24:04 +00:00
|
|
|
|
private void ClearImages(IHasImages item, ImageType type)
|
|
|
|
|
{
|
|
|
|
|
var deleted = false;
|
|
|
|
|
|
|
|
|
|
foreach (var image in item.GetImages(type).ToList())
|
|
|
|
|
{
|
|
|
|
|
// Delete the source file
|
|
|
|
|
var currentFile = new FileInfo(image.Path);
|
|
|
|
|
|
|
|
|
|
// Deletion will fail if the file is hidden so remove the attribute first
|
|
|
|
|
if (currentFile.Exists)
|
|
|
|
|
{
|
|
|
|
|
if ((currentFile.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
|
|
|
|
{
|
|
|
|
|
currentFile.Attributes &= ~FileAttributes.Hidden;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-13 03:46:44 +00:00
|
|
|
|
_fileSystem.DeleteFile(currentFile.FullName);
|
2014-06-15 02:24:04 +00:00
|
|
|
|
deleted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (deleted)
|
|
|
|
|
{
|
2015-09-13 21:32:02 +00:00
|
|
|
|
item.ValidateImages(new DirectoryService(_logger, _fileSystem));
|
2014-06-15 02:24:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 04:52:52 +00:00
|
|
|
|
public bool MergeImages(IHasImages item, List<LocalImageInfo> images)
|
2014-01-28 18:37:01 +00:00
|
|
|
|
{
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
|
|
foreach (var type in _singularImages)
|
|
|
|
|
{
|
|
|
|
|
var image = images.FirstOrDefault(i => i.Type == type);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
currentImage.DateModified = _fileSystem.GetLastWriteTimeUtc(image.FileInfo);
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
2015-04-11 01:42:37 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var existing = item.GetImageInfo(type, 0);
|
2015-09-13 21:32:02 +00:00
|
|
|
|
if (existing != null && !_fileSystem.FileExists(existing.Path))
|
2015-04-11 01:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
item.RemoveImage(existing);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool UpdateMultiImages(IHasImages item, List<LocalImageInfo> images, ImageType type)
|
|
|
|
|
{
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
2015-04-11 01:42:37 +00:00
|
|
|
|
var newImages = images.Where(i => i.Type == type).ToList();
|
2015-05-06 12:56:26 +00:00
|
|
|
|
|
|
|
|
|
var newImageFileInfos = newImages
|
2014-02-07 22:40:03 +00:00
|
|
|
|
.Select(i => i.FileInfo)
|
2014-02-07 20:30:41 +00:00
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 18:05:24 +00:00
|
|
|
|
private async Task<bool> DownloadImage(IHasImages item,
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
foreach (var image in images.Where(i => i.Type == type))
|
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-02-22 20:20:22 +00:00
|
|
|
|
await _providerManager.SaveImage(item, response.Content, response.ContentType, type, null, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
|
2014-06-15 02:24:04 +00:00
|
|
|
|
return true;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
2014-02-08 20:02:35 +00:00
|
|
|
|
// Sometimes providers send back bad url's. Just move to the next image
|
2014-01-28 18:37:01 +00:00
|
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-15 02:24:04 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-12 03:33:30 +00:00
|
|
|
|
private async Task DownloadBackdrops(IHasImages 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;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-02-25 15:40:16 +00:00
|
|
|
|
// If there's already an image of the same size, skip it
|
|
|
|
|
if (response.ContentLength.HasValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (item.GetImages(imageType).Any(i => new FileInfo(i.Path).Length == response.ContentLength.Value))
|
|
|
|
|
{
|
|
|
|
|
response.Content.Dispose();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error examining images", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 20:20:22 +00:00
|
|
|
|
await _providerManager.SaveImage(item, response.Content, response.ContentType, imageType, null, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|