瀏覽代碼

Convert non-local image to local before computing blurhash

Vasily 5 年之前
父節點
當前提交
9208acd5ae
共有 2 個文件被更改,包括 70 次插入10 次删除
  1. 30 10
      Emby.Server.Implementations/Library/LibraryManager.cs
  2. 40 0
      MediaBrowser.Controller/Entities/BaseItem.cs

+ 30 - 10
Emby.Server.Implementations/Library/LibraryManager.cs

@@ -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)
@@ -1850,7 +1850,7 @@ namespace Emby.Server.Implementations.Library
                 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)
             {
                 RegisterItem(item);
@@ -1859,26 +1859,46 @@ namespace Emby.Server.Implementations.Library
 
             foreach (var img in outdated)
             {
-                ImageDimensions size = _imageProcessor.GetImageDimensions(item, img);
-                img.Width = size.Width;
-                img.Height = size.Height;
+                var image = img;
+                if (!img.IsLocalFile)
+                {
+                    try
+                    {
+                        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
                 {
-                    img.BlurHash = _imageProcessor.GetImageBlurHash(img.Path);
+                    image.BlurHash = _imageProcessor.GetImageBlurHash(image.Path);
                 }
                 catch (Exception ex)
                 {
-                    _logger.LogError(ex, "Cannot compute blurhash for {0}", img.Path);
-                    img.BlurHash = string.Empty;
+                    _logger.LogError(ex, "Cannot compute blurhash for {0}", image.Path);
+                    image.BlurHash = string.Empty;
                 }
 
                 try
                 {
-                    img.DateModified = _fileSystem.GetLastWriteTimeUtc(img.Path);
+                    image.DateModified = _fileSystem.GetLastWriteTimeUtc(image.Path);
                 }
                 catch (Exception ex)
                 {
-                    _logger.LogError(ex, "Cannot update DateModified for {0}", img.Path);
+                    _logger.LogError(ex, "Cannot update DateModified for {0}", image.Path);
                 }
             }
 

+ 40 - 0
MediaBrowser.Controller/Entities/BaseItem.cs

@@ -2375,6 +2375,46 @@ namespace MediaBrowser.Controller.Entities
                 .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)
         {
             if (imageType == ImageType.Chapter)