Parcourir la source

Compute hash only when one is not computed in DB, small optimizations here and there

Vasily il y a 5 ans
Parent
commit
a226a4ee03

+ 3 - 13
Emby.Server.Implementations/Data/SqliteItemRepository.cs

@@ -1141,20 +1141,10 @@ namespace Emby.Server.Implementations.Data
 
         public string ToValueString(ItemImageInfo image)
         {
-            var delimeter = "*";
+            const string delimeter = "*";
 
-            var path = image.Path;
-            var hash = image.Hash;
-
-            if (path == null)
-            {
-                path = string.Empty;
-            }
-
-            if (hash == null)
-            {
-                hash = string.Empty;
-            }
+            var path = image.Path ?? string.Empty;
+            var hash = image.Hash ?? string.Empty;
 
             return GetPathToSave(path) +
                    delimeter +

+ 20 - 39
Emby.Server.Implementations/Library/LibraryManager.cs

@@ -1825,17 +1825,26 @@ namespace Emby.Server.Implementations.Library
 
         public void UpdateImages(BaseItem item)
         {
-            item.ImageInfos
-                .Where(i => (i.Width == 0 || i.Height == 0))
-                .ToList()
-                .ForEach(x =>
-                {
-                    string blurhash = ImageProcessor.GetImageHash(x.Path);
-                    ImageDimensions size = ImageProcessor.GetImageDimensions(item, x);
-                    x.Width = size.Width;
-                    x.Height = size.Height;
-                    x.Hash = blurhash;
-                });
+            if (item == null)
+            {
+                throw new ArgumentNullException(nameof(item));
+            }
+
+            var outdated = item.ImageInfos
+                .Where(i => (i.Width == 0 || i.Height == 0 || string.IsNullOrEmpty(i.Hash)))
+                .ToList();
+            if (outdated.Count == 0)
+            {
+                return;
+            }
+
+            outdated.ForEach(img =>
+            {
+                ImageDimensions size = ImageProcessor.GetImageDimensions(item, img);
+                img.Width = size.Width;
+                img.Height = size.Height;
+                img.Hash = ImageProcessor.GetImageHash(img.Path);
+            });
 
             _itemRepository.SaveImages(item);
 
@@ -1905,34 +1914,6 @@ namespace Emby.Server.Implementations.Library
             UpdateItems(new[] { item }, parent, updateReason, cancellationToken);
         }
 
-        /// <summary>
-        /// Updates everything in the database.
-        /// </summary>
-        public void UpdateAll()
-        {
-            Task.Run(() =>
-            {
-                var items = _itemRepository.GetItemList(new InternalItemsQuery {
-                    Recursive = true
-                });
-                foreach (var item in items)
-                {
-                    _logger.LogDebug("Updating item {Name} ({ItemId})",
-                        item.Name,
-                        item.Id);
-                    try
-                    {
-                        item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
-                    }
-                    catch (Exception ex)
-                    {
-                        _logger.LogError(ex, "Updating item {ItemId} failed", item.Id);
-                    }
-                }
-                _logger.LogDebug("All items have been updated");
-            });
-        }
-
         /// <summary>
         /// Reports the item removed.
         /// </summary>

+ 13 - 9
MediaBrowser.Api/Images/ImageService.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Globalization;
 using System.IO;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Threading;
 using System.Threading.Tasks;
 using MediaBrowser.Common.Extensions;
@@ -280,9 +281,16 @@ namespace MediaBrowser.Api.Images
         public List<ImageInfo> GetItemImageInfos(BaseItem item)
         {
             var list = new List<ImageInfo>();
-
             var itemImages = item.ImageInfos;
 
+            if (itemImages.Length == 0)
+            {
+                // short-circuit
+                return list;
+            }
+
+            _libraryManager.UpdateImages(item); // this makes sure dimensions and hashes are correct
+
             foreach (var image in itemImages)
             {
                 if (!item.AllowsMultipleImages(image.Type))
@@ -323,7 +331,7 @@ namespace MediaBrowser.Api.Images
         {
             int? width = null;
             int? height = null;
-            string? blurhash = null;
+            string blurhash = null;
             long length = 0;
 
             try
@@ -333,13 +341,9 @@ namespace MediaBrowser.Api.Images
                     var fileInfo = _fileSystem.GetFileInfo(info.Path);
                     length = fileInfo.Length;
 
-                    blurhash = _imageProcessor.GetImageHash(info.Path);
-                    info.Hash = blurhash; // TODO: this doesn't seem like the right thing to do
-
-                    ImageDimensions size = _imageProcessor.GetImageDimensions(item, info);
-                    _libraryManager.UpdateImages(item);
-                    width = size.Width;
-                    height = size.Height;
+                    blurhash = info.Hash;
+                    width = info.Width;
+                    height = info.Height;
 
                     if (width <= 0 || height <= 0)
                     {

+ 0 - 6
MediaBrowser.Api/ItemUpdateService.cs

@@ -198,12 +198,6 @@ namespace MediaBrowser.Api
 
         public void Post(UpdateItem request)
         {
-            if (request.ItemId == "*")
-            {
-                // Special case: Refresh everything in database. Probably not a great idea to run often.
-                _libraryManager.UpdateAll();
-                return;
-            }
             var item = _libraryManager.GetItemById(request.ItemId);
 
             var newLockData = request.LockData ?? false;

+ 1 - 1
MediaBrowser.Controller/Library/ILibraryManager.cs

@@ -195,8 +195,8 @@ namespace MediaBrowser.Controller.Library
         /// Updates the item.
         /// </summary>
         void UpdateItems(IEnumerable<BaseItem> items, BaseItem parent, ItemUpdateType updateReason, CancellationToken cancellationToken);
+
         void UpdateItem(BaseItem item, BaseItem parent, ItemUpdateType updateReason, CancellationToken cancellationToken);
-        void UpdateAll();
 
         /// <summary>
         /// Retrieves the item.