Convert non-local image to local before computing blurhash

This commit is contained in:
Vasily 2020-05-28 17:55:29 +03:00
parent ed791dee46
commit 9208acd5ae
2 changed files with 70 additions and 10 deletions

View File

@ -1840,7 +1840,7 @@ namespace Emby.Server.Implementations.Library
} }
} }
return false; return image.Path != null && !image.IsLocalFile;
} }
public void UpdateImages(BaseItem item, bool forceUpdate = false) public void UpdateImages(BaseItem item, bool forceUpdate = false)
@ -1850,7 +1850,7 @@ namespace Emby.Server.Implementations.Library
throw new ArgumentNullException(nameof(item)); throw new ArgumentNullException(nameof(item));
} }
var outdated = forceUpdate ? item.ImageInfos.Where(i => i.IsLocalFile).ToArray() : item.ImageInfos.Where(ImageNeedsRefresh).ToArray(); var outdated = forceUpdate ? item.ImageInfos.Where(i => i.Path != null).ToArray() : item.ImageInfos.Where(ImageNeedsRefresh).ToArray();
if (outdated.Length == 0) if (outdated.Length == 0)
{ {
RegisterItem(item); RegisterItem(item);
@ -1859,26 +1859,46 @@ namespace Emby.Server.Implementations.Library
foreach (var img in outdated) foreach (var img in outdated)
{ {
ImageDimensions size = _imageProcessor.GetImageDimensions(item, img); var image = img;
img.Width = size.Width; if (!img.IsLocalFile)
img.Height = size.Height; {
try try
{ {
img.BlurHash = _imageProcessor.GetImageBlurHash(img.Path); var index = item.GetImageIndex(img);
image = ConvertImageToLocal(item, img, index).ConfigureAwait(false).GetAwaiter().GetResult();
}
catch (ArgumentException)
{
_logger.LogWarning("Cannot get image index for {0}", img.Path);
continue;
}
catch (InvalidOperationException)
{
_logger.LogWarning("Cannot fetch image from {0}", img.Path);
continue;
}
}
ImageDimensions size = _imageProcessor.GetImageDimensions(item, image);
image.Width = size.Width;
image.Height = size.Height;
try
{
image.BlurHash = _imageProcessor.GetImageBlurHash(image.Path);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Cannot compute blurhash for {0}", img.Path); _logger.LogError(ex, "Cannot compute blurhash for {0}", image.Path);
img.BlurHash = string.Empty; image.BlurHash = string.Empty;
} }
try try
{ {
img.DateModified = _fileSystem.GetLastWriteTimeUtc(img.Path); image.DateModified = _fileSystem.GetLastWriteTimeUtc(image.Path);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Cannot update DateModified for {0}", img.Path); _logger.LogError(ex, "Cannot update DateModified for {0}", image.Path);
} }
} }

View File

@ -2375,6 +2375,46 @@ namespace MediaBrowser.Controller.Entities
.ElementAtOrDefault(imageIndex); .ElementAtOrDefault(imageIndex);
} }
/// <summary>
/// Computes image index for given image or raises if no matching image found.
/// </summary>
/// <param name="image">Image to compute index for.</param>
/// <exception cref="ArgumentException">Image index cannot be computed as no matching image found.
/// </exception>
/// <returns>Image index.</returns>
public int GetImageIndex(ItemImageInfo image)
{
if (image == null)
{
throw new ArgumentNullException(nameof(image));
}
if (image.Type == ImageType.Chapter)
{
var chapters = ItemRepository.GetChapters(this);
for (var i = 0; i < chapters.Count; i++)
{
if (chapters[i].ImagePath == image.Path)
{
return i;
}
}
throw new ArgumentException("No chapter index found for image path", image.Path);
}
var images = GetImages(image.Type).ToArray();
for (var i = 0; i < images.Length; i++)
{
if (images[i].Path == image.Path)
{
return i;
}
}
throw new ArgumentException("No image index found for image path", image.Path);
}
public IEnumerable<ItemImageInfo> GetImages(ImageType imageType) public IEnumerable<ItemImageInfo> GetImages(ImageType imageType)
{ {
if (imageType == ImageType.Chapter) if (imageType == ImageType.Chapter)