diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 8ed51a1949..814c101961 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -973,7 +973,7 @@ namespace Emby.Server.Implementations yield return typeof(IServerApplicationHost).Assembly; // Include composable parts in the Providers assembly - yield return typeof(ProviderUtils).Assembly; + yield return typeof(ProviderManager).Assembly; // Include composable parts in the Photos assembly yield return typeof(PhotoProvider).Assembly; diff --git a/MediaBrowser.Providers/Manager/MetadataService.cs b/MediaBrowser.Providers/Manager/MetadataService.cs index 86ef51f5f3..eee3d3bd18 100644 --- a/MediaBrowser.Providers/Manager/MetadataService.cs +++ b/MediaBrowser.Providers/Manager/MetadataService.cs @@ -8,8 +8,10 @@ using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Diacritics.Extensions; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Configuration; @@ -875,16 +877,6 @@ namespace MediaBrowser.Providers.Manager } } - protected virtual void MergeData( - MetadataResult source, - MetadataResult target, - MetadataField[] lockedFields, - bool replaceData, - bool mergeMetadataSettings) - { - ProviderUtils.MergeBaseItemData(source, target, lockedFields, replaceData, mergeMetadataSettings); - } - private bool HasChanged(BaseItem item, IHasItemChangeMonitor changeMonitor, IDirectoryService directoryService) { try @@ -904,5 +896,300 @@ namespace MediaBrowser.Providers.Manager return false; } } + + /// + /// Merges metadata from source into target. + /// + /// The source for new metadata. + /// The target to insert new metadata into. + /// The fields that are locked and should not be updated. + /// true if existing data should be replaced. + /// true if the metadata settings in target should be updated to match source. + /// Thrown if source or target are null. + protected virtual void MergeData( + MetadataResult source, + MetadataResult target, + MetadataField[] lockedFields, + bool replaceData, + bool mergeMetadataSettings) + { + MergeBaseItemData(source, target, lockedFields, replaceData, mergeMetadataSettings); + } + + internal static void MergeBaseItemData( + MetadataResult sourceResult, + MetadataResult targetResult, + MetadataField[] lockedFields, + bool replaceData, + bool mergeMetadataSettings) + { + var source = sourceResult.Item; + var target = targetResult.Item; + + if (source == null) + { + throw new ArgumentException("Item cannot be null.", nameof(sourceResult)); + } + + if (target == null) + { + throw new ArgumentException("Item cannot be null.", nameof(targetResult)); + } + + if (!lockedFields.Contains(MetadataField.Name)) + { + if (replaceData || string.IsNullOrEmpty(target.Name)) + { + // Safeguard against incoming data having an empty name + if (!string.IsNullOrWhiteSpace(source.Name)) + { + target.Name = source.Name; + } + } + } + + if (replaceData || string.IsNullOrEmpty(target.OriginalTitle)) + { + // Safeguard against incoming data having an empty name + if (!string.IsNullOrWhiteSpace(source.OriginalTitle)) + { + target.OriginalTitle = source.OriginalTitle; + } + } + + if (replaceData || !target.CommunityRating.HasValue) + { + target.CommunityRating = source.CommunityRating; + } + + if (replaceData || !target.EndDate.HasValue) + { + target.EndDate = source.EndDate; + } + + if (!lockedFields.Contains(MetadataField.Genres)) + { + if (replaceData || target.Genres.Length == 0) + { + target.Genres = source.Genres; + } + } + + if (replaceData || !target.IndexNumber.HasValue) + { + target.IndexNumber = source.IndexNumber; + } + + if (!lockedFields.Contains(MetadataField.OfficialRating)) + { + if (replaceData || string.IsNullOrEmpty(target.OfficialRating)) + { + target.OfficialRating = source.OfficialRating; + } + } + + if (replaceData || string.IsNullOrEmpty(target.CustomRating)) + { + target.CustomRating = source.CustomRating; + } + + if (replaceData || string.IsNullOrEmpty(target.Tagline)) + { + target.Tagline = source.Tagline; + } + + if (!lockedFields.Contains(MetadataField.Overview)) + { + if (replaceData || string.IsNullOrEmpty(target.Overview)) + { + target.Overview = source.Overview; + } + } + + if (replaceData || !target.ParentIndexNumber.HasValue) + { + target.ParentIndexNumber = source.ParentIndexNumber; + } + + if (!lockedFields.Contains(MetadataField.Cast)) + { + if (replaceData || targetResult.People == null || targetResult.People.Count == 0) + { + targetResult.People = sourceResult.People; + } + else if (targetResult.People != null && sourceResult.People != null) + { + MergePeople(sourceResult.People, targetResult.People); + } + } + + if (replaceData || !target.PremiereDate.HasValue) + { + target.PremiereDate = source.PremiereDate; + } + + if (replaceData || !target.ProductionYear.HasValue) + { + target.ProductionYear = source.ProductionYear; + } + + if (!lockedFields.Contains(MetadataField.Runtime)) + { + if (replaceData || !target.RunTimeTicks.HasValue) + { + if (target is not Audio && target is not Video) + { + target.RunTimeTicks = source.RunTimeTicks; + } + } + } + + if (!lockedFields.Contains(MetadataField.Studios)) + { + if (replaceData || target.Studios.Length == 0) + { + target.Studios = source.Studios; + } + } + + if (!lockedFields.Contains(MetadataField.Tags)) + { + if (replaceData || target.Tags.Length == 0) + { + target.Tags = source.Tags; + } + } + + if (!lockedFields.Contains(MetadataField.ProductionLocations)) + { + if (replaceData || target.ProductionLocations.Length == 0) + { + target.ProductionLocations = source.ProductionLocations; + } + } + + foreach (var id in source.ProviderIds) + { + var key = id.Key; + + // Don't replace existing Id's. + if (replaceData || !target.ProviderIds.ContainsKey(key)) + { + target.ProviderIds[key] = id.Value; + } + } + + MergeAlbumArtist(source, target, replaceData); + MergeCriticRating(source, target, replaceData); + MergeTrailers(source, target, replaceData); + MergeVideoInfo(source, target, replaceData); + MergeDisplayOrder(source, target, replaceData); + + if (replaceData || string.IsNullOrEmpty(target.ForcedSortName)) + { + var forcedSortName = source.ForcedSortName; + + if (!string.IsNullOrWhiteSpace(forcedSortName)) + { + target.ForcedSortName = forcedSortName; + } + } + + if (mergeMetadataSettings) + { + target.LockedFields = source.LockedFields; + target.IsLocked = source.IsLocked; + + // Grab the value if it's there, but if not then don't overwrite with the default + if (source.DateCreated != default) + { + target.DateCreated = source.DateCreated; + } + + target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode; + target.PreferredMetadataLanguage = source.PreferredMetadataLanguage; + } + } + + private static void MergePeople(List source, List target) + { + foreach (var person in target) + { + var normalizedName = person.Name.RemoveDiacritics(); + var personInSource = source.FirstOrDefault(i => string.Equals(i.Name.RemoveDiacritics(), normalizedName, StringComparison.OrdinalIgnoreCase)); + + if (personInSource != null) + { + foreach (var providerId in personInSource.ProviderIds) + { + if (!person.ProviderIds.ContainsKey(providerId.Key)) + { + person.ProviderIds[providerId.Key] = providerId.Value; + } + } + + if (string.IsNullOrWhiteSpace(person.ImageUrl)) + { + person.ImageUrl = personInSource.ImageUrl; + } + } + } + } + + private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData) + { + if (source is IHasDisplayOrder sourceHasDisplayOrder + && target is IHasDisplayOrder targetHasDisplayOrder) + { + if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder)) + { + var displayOrder = sourceHasDisplayOrder.DisplayOrder; + + if (!string.IsNullOrWhiteSpace(displayOrder)) + { + targetHasDisplayOrder.DisplayOrder = displayOrder; + } + } + } + } + + private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData) + { + if (source is IHasAlbumArtist sourceHasAlbumArtist + && target is IHasAlbumArtist targetHasAlbumArtist) + { + if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0) + { + targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists; + } + } + } + + private static void MergeCriticRating(BaseItem source, BaseItem target, bool replaceData) + { + if (replaceData || !target.CriticRating.HasValue) + { + target.CriticRating = source.CriticRating; + } + } + + private static void MergeTrailers(BaseItem source, BaseItem target, bool replaceData) + { + if (replaceData || target.RemoteTrailers.Count == 0) + { + target.RemoteTrailers = source.RemoteTrailers; + } + } + + private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData) + { + if (source is Video sourceCast && target is Video targetCast) + { + if (replaceData || targetCast.Video3DFormat == null) + { + targetCast.Video3DFormat = sourceCast.Video3DFormat; + } + } + } } } diff --git a/MediaBrowser.Providers/Manager/ProviderUtils.cs b/MediaBrowser.Providers/Manager/ProviderUtils.cs deleted file mode 100644 index 798fd0d681..0000000000 --- a/MediaBrowser.Providers/Manager/ProviderUtils.cs +++ /dev/null @@ -1,306 +0,0 @@ -#nullable disable - -using System; -using System.Collections.Generic; -using System.Linq; -using Diacritics.Extensions; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Entities.Audio; -using MediaBrowser.Controller.Providers; -using MediaBrowser.Model.Entities; - -namespace MediaBrowser.Providers.Manager -{ - /// - /// Class ProviderUtils. - /// - public static class ProviderUtils - { - /// - /// Merges metadata from source into target. - /// - /// The source for new metadata. - /// The target to insert new metadata into. - /// The fields that are locked and should not be updated. - /// true if existing data should be replaced. - /// true if the metadata settings in target should be updated to match source. - /// The type being acted upon. - /// Thrown if source or target are null. - public static void MergeBaseItemData( - MetadataResult sourceResult, - MetadataResult targetResult, - MetadataField[] lockedFields, - bool replaceData, - bool mergeMetadataSettings) - where T : BaseItem - { - var source = sourceResult.Item; - var target = targetResult.Item; - - if (source == null) - { - throw new ArgumentException("Item cannot be null.", nameof(sourceResult)); - } - - if (target == null) - { - throw new ArgumentException("Item cannot be null.", nameof(targetResult)); - } - - if (!lockedFields.Contains(MetadataField.Name)) - { - if (replaceData || string.IsNullOrEmpty(target.Name)) - { - // Safeguard against incoming data having an empty name - if (!string.IsNullOrWhiteSpace(source.Name)) - { - target.Name = source.Name; - } - } - } - - if (replaceData || string.IsNullOrEmpty(target.OriginalTitle)) - { - // Safeguard against incoming data having an empty name - if (!string.IsNullOrWhiteSpace(source.OriginalTitle)) - { - target.OriginalTitle = source.OriginalTitle; - } - } - - if (replaceData || !target.CommunityRating.HasValue) - { - target.CommunityRating = source.CommunityRating; - } - - if (replaceData || !target.EndDate.HasValue) - { - target.EndDate = source.EndDate; - } - - if (!lockedFields.Contains(MetadataField.Genres)) - { - if (replaceData || target.Genres.Length == 0) - { - target.Genres = source.Genres; - } - } - - if (replaceData || !target.IndexNumber.HasValue) - { - target.IndexNumber = source.IndexNumber; - } - - if (!lockedFields.Contains(MetadataField.OfficialRating)) - { - if (replaceData || string.IsNullOrEmpty(target.OfficialRating)) - { - target.OfficialRating = source.OfficialRating; - } - } - - if (replaceData || string.IsNullOrEmpty(target.CustomRating)) - { - target.CustomRating = source.CustomRating; - } - - if (replaceData || string.IsNullOrEmpty(target.Tagline)) - { - target.Tagline = source.Tagline; - } - - if (!lockedFields.Contains(MetadataField.Overview)) - { - if (replaceData || string.IsNullOrEmpty(target.Overview)) - { - target.Overview = source.Overview; - } - } - - if (replaceData || !target.ParentIndexNumber.HasValue) - { - target.ParentIndexNumber = source.ParentIndexNumber; - } - - if (!lockedFields.Contains(MetadataField.Cast)) - { - if (replaceData || targetResult.People == null || targetResult.People.Count == 0) - { - targetResult.People = sourceResult.People; - } - else if (targetResult.People != null && sourceResult.People != null) - { - MergePeople(sourceResult.People, targetResult.People); - } - } - - if (replaceData || !target.PremiereDate.HasValue) - { - target.PremiereDate = source.PremiereDate; - } - - if (replaceData || !target.ProductionYear.HasValue) - { - target.ProductionYear = source.ProductionYear; - } - - if (!lockedFields.Contains(MetadataField.Runtime)) - { - if (replaceData || !target.RunTimeTicks.HasValue) - { - if (target is not Audio && target is not Video) - { - target.RunTimeTicks = source.RunTimeTicks; - } - } - } - - if (!lockedFields.Contains(MetadataField.Studios)) - { - if (replaceData || target.Studios.Length == 0) - { - target.Studios = source.Studios; - } - } - - if (!lockedFields.Contains(MetadataField.Tags)) - { - if (replaceData || target.Tags.Length == 0) - { - target.Tags = source.Tags; - } - } - - if (!lockedFields.Contains(MetadataField.ProductionLocations)) - { - if (replaceData || target.ProductionLocations.Length == 0) - { - target.ProductionLocations = source.ProductionLocations; - } - } - - foreach (var id in source.ProviderIds) - { - var key = id.Key; - - // Don't replace existing Id's. - if (replaceData || !target.ProviderIds.ContainsKey(key)) - { - target.ProviderIds[key] = id.Value; - } - } - - MergeAlbumArtist(source, target, replaceData); - MergeCriticRating(source, target, replaceData); - MergeTrailers(source, target, replaceData); - MergeVideoInfo(source, target, replaceData); - MergeDisplayOrder(source, target, replaceData); - - if (replaceData || string.IsNullOrEmpty(target.ForcedSortName)) - { - var forcedSortName = source.ForcedSortName; - - if (!string.IsNullOrWhiteSpace(forcedSortName)) - { - target.ForcedSortName = forcedSortName; - } - } - - if (mergeMetadataSettings) - { - target.LockedFields = source.LockedFields; - target.IsLocked = source.IsLocked; - - // Grab the value if it's there, but if not then don't overwrite with the default - if (source.DateCreated != default) - { - target.DateCreated = source.DateCreated; - } - - target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode; - target.PreferredMetadataLanguage = source.PreferredMetadataLanguage; - } - } - - private static void MergePeople(List source, List target) - { - foreach (var person in target) - { - var normalizedName = person.Name.RemoveDiacritics(); - var personInSource = source.FirstOrDefault(i => string.Equals(i.Name.RemoveDiacritics(), normalizedName, StringComparison.OrdinalIgnoreCase)); - - if (personInSource != null) - { - foreach (var providerId in personInSource.ProviderIds) - { - if (!person.ProviderIds.ContainsKey(providerId.Key)) - { - person.ProviderIds[providerId.Key] = providerId.Value; - } - } - - if (string.IsNullOrWhiteSpace(person.ImageUrl)) - { - person.ImageUrl = personInSource.ImageUrl; - } - } - } - } - - private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData) - { - if (source is IHasDisplayOrder sourceHasDisplayOrder - && target is IHasDisplayOrder targetHasDisplayOrder) - { - if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder)) - { - var displayOrder = sourceHasDisplayOrder.DisplayOrder; - - if (!string.IsNullOrWhiteSpace(displayOrder)) - { - targetHasDisplayOrder.DisplayOrder = displayOrder; - } - } - } - } - - private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData) - { - if (source is IHasAlbumArtist sourceHasAlbumArtist - && target is IHasAlbumArtist targetHasAlbumArtist) - { - if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0) - { - targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists; - } - } - } - - private static void MergeCriticRating(BaseItem source, BaseItem target, bool replaceData) - { - if (replaceData || !target.CriticRating.HasValue) - { - target.CriticRating = source.CriticRating; - } - } - - private static void MergeTrailers(BaseItem source, BaseItem target, bool replaceData) - { - if (replaceData || target.RemoteTrailers.Count == 0) - { - target.RemoteTrailers = source.RemoteTrailers; - } - } - - private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData) - { - if (source is Video sourceCast && target is Video targetCast) - { - if (replaceData || targetCast.Video3DFormat == null) - { - targetCast.Video3DFormat = sourceCast.Video3DFormat; - } - } - } - } -} diff --git a/tests/Jellyfin.Providers.Tests/Manager/ProviderUtilsTests.cs b/tests/Jellyfin.Providers.Tests/Manager/MetadataServiceTests.cs similarity index 74% rename from tests/Jellyfin.Providers.Tests/Manager/ProviderUtilsTests.cs rename to tests/Jellyfin.Providers.Tests/Manager/MetadataServiceTests.cs index 9e4bbef136..b74b331b7f 100644 --- a/tests/Jellyfin.Providers.Tests/Manager/ProviderUtilsTests.cs +++ b/tests/Jellyfin.Providers.Tests/Manager/MetadataServiceTests.cs @@ -11,7 +11,7 @@ using Xunit; namespace Jellyfin.Providers.Tests.Manager { - public class ProviderUtilsTests + public class MetadataServiceTests { [Theory] [InlineData(false, false)] @@ -55,7 +55,7 @@ namespace Jellyfin.Providers.Tests.Manager } }; - ProviderUtils.MergeBaseItemData(source, target, Array.Empty(), true, mergeMetadataSettings); + MetadataService.MergeBaseItemData(source, target, Array.Empty(), true, mergeMetadataSettings); if (mergeMetadataSettings) { @@ -90,19 +90,19 @@ namespace Jellyfin.Providers.Tests.Manager var newValue = "New"; // Use type Series to hit DisplayOrder - Assert.False(TestMergeBaseItemData(propName, oldValue, newValue, null, false, out _)); + Assert.False(TestMergeBaseItemData(propName, oldValue, newValue, null, false, out _)); if (lockField != null) { - Assert.False(TestMergeBaseItemData(propName, oldValue, newValue, lockField, true, out _)); - Assert.False(TestMergeBaseItemData(propName, null, newValue, lockField, false, out _)); - Assert.False(TestMergeBaseItemData(propName, string.Empty, newValue, lockField, false, out _)); + Assert.False(TestMergeBaseItemData(propName, oldValue, newValue, lockField, true, out _)); + Assert.False(TestMergeBaseItemData(propName, null, newValue, lockField, false, out _)); + Assert.False(TestMergeBaseItemData(propName, string.Empty, newValue, lockField, false, out _)); } - Assert.True(TestMergeBaseItemData(propName, oldValue, newValue, null, true, out _)); - Assert.True(TestMergeBaseItemData(propName, null, newValue, null, false, out _)); - Assert.True(TestMergeBaseItemData(propName, string.Empty, newValue, null, false, out _)); + Assert.True(TestMergeBaseItemData(propName, oldValue, newValue, null, true, out _)); + Assert.True(TestMergeBaseItemData(propName, null, newValue, null, false, out _)); + Assert.True(TestMergeBaseItemData(propName, string.Empty, newValue, null, false, out _)); - var replacedWithEmpty = TestMergeBaseItemData(propName, oldValue, string.Empty, null, true, out _); + var replacedWithEmpty = TestMergeBaseItemData(propName, oldValue, string.Empty, null, true, out _); Assert.Equal(replacesWithEmpty, replacedWithEmpty); } @@ -119,17 +119,17 @@ namespace Jellyfin.Providers.Tests.Manager var newValue = new[] { "New" }; // Use type Audio to hit AlbumArtists - Assert.False(TestMergeBaseItemData