improve reaction to image stub failures

This commit is contained in:
Luke Pulverenti 2016-01-23 23:21:31 -05:00
parent 0b9b62063e
commit 94ec1a2c6c
2 changed files with 50 additions and 27 deletions

View File

@ -480,22 +480,21 @@ namespace MediaBrowser.Providers.Manager
ImageType type, ImageType type,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
foreach (var image in images.Where(i => i.Type == type)) var eligibleImages = images
.Where(i => i.Type == type && !(i.Width.HasValue && i.Width.Value < minWidth))
.ToList();
if (EnableImageStub(item, type) && eligibleImages.Count > 0)
{ {
if (image.Width.HasValue && image.Width.Value < minWidth) SaveImageStub(item, type, eligibleImages.Select(i => i.Url));
{ result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
continue; return true;
} }
foreach (var image in eligibleImages)
{
var url = image.Url; var url = image.Url;
if (EnableImageStub(item, type))
{
SaveImageStub(item, type, url);
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
return true;
}
try try
{ {
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false); var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
@ -557,18 +556,20 @@ namespace MediaBrowser.Providers.Manager
} }
} }
private void SaveImageStub(IHasImages item, ImageType imageType, string url) private void SaveImageStub(IHasImages item, ImageType imageType, IEnumerable<string> urls)
{ {
var newIndex = item.AllowsMultipleImages(imageType) ? item.GetImages(imageType).Count() : 0; var newIndex = item.AllowsMultipleImages(imageType) ? item.GetImages(imageType).Count() : 0;
SaveImageStub(item, imageType, url, newIndex); SaveImageStub(item, imageType, urls, newIndex);
} }
private void SaveImageStub(IHasImages item, ImageType imageType, string url, int newIndex) private void SaveImageStub(IHasImages item, ImageType imageType, IEnumerable<string> urls, int newIndex)
{ {
var path = string.Join("|", urls.Take(1).ToArray());
item.SetImage(new ItemImageInfo item.SetImage(new ItemImageInfo
{ {
Path = url, Path = path,
Type = imageType Type = imageType
}, newIndex); }, newIndex);
@ -592,7 +593,7 @@ namespace MediaBrowser.Providers.Manager
if (EnableImageStub(item, imageType)) if (EnableImageStub(item, imageType))
{ {
SaveImageStub(item, imageType, url); SaveImageStub(item, imageType, new[] { url });
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate; result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
continue; continue;
} }

View File

@ -33,11 +33,13 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommonIO; using CommonIO;
using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Library; using MediaBrowser.Model.Library;
using MediaBrowser.Model.Net;
using MoreLinq; using MoreLinq;
using SortOrder = MediaBrowser.Model.Entities.SortOrder; using SortOrder = MediaBrowser.Model.Entities.SortOrder;
@ -2478,20 +2480,40 @@ namespace MediaBrowser.Server.Implementations.Library
private readonly SemaphoreSlim _dynamicImageResourcePool = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _dynamicImageResourcePool = new SemaphoreSlim(1, 1);
public async Task<ItemImageInfo> ConvertImageToLocal(IHasImages item, ItemImageInfo image, int imageIndex) public async Task<ItemImageInfo> ConvertImageToLocal(IHasImages item, ItemImageInfo image, int imageIndex)
{ {
_logger.Debug("ConvertImageToLocal item {0}", item.Id); foreach (var url in image.Path.Split('|'))
await _providerManagerFactory().SaveImage(item, image.Path, _dynamicImageResourcePool, image.Type, imageIndex, CancellationToken.None).ConfigureAwait(false);
var newImage = item.GetImageInfo(image.Type, imageIndex);
if (newImage != null)
{ {
newImage.IsPlaceholder = image.IsPlaceholder; try
{
_logger.Debug("ConvertImageToLocal item {0} - image url: {1}", item.Id, url);
await _providerManagerFactory().SaveImage(item, url, _dynamicImageResourcePool, image.Type, imageIndex, CancellationToken.None).ConfigureAwait(false);
var newImage = item.GetImageInfo(image.Type, imageIndex);
if (newImage != null)
{
newImage.IsPlaceholder = image.IsPlaceholder;
}
await item.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);
return item.GetImageInfo(image.Type, imageIndex);
}
catch (HttpException ex)
{
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
{
continue;
}
throw;
}
} }
// Remove this image to prevent it from retrying over and over
item.RemoveImage(image);
await item.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false); await item.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);
return item.GetImageInfo(image.Type, imageIndex); throw new InvalidOperationException();
} }
} }
} }