From 2749509f001505d35863db4b53bb4bc6c3af6fa4 Mon Sep 17 00:00:00 2001 From: cvium Date: Tue, 28 Dec 2021 00:37:40 +0100 Subject: Use dedicated resolvers for extras --- .../Library/LibraryManager.cs | 87 +++++++--------------- 1 file changed, 25 insertions(+), 62 deletions(-) (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 270264dba..694501d38 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -13,7 +13,7 @@ using System.Threading; using System.Threading.Tasks; using Emby.Naming.Common; using Emby.Naming.TV; -using Emby.Naming.Video; +using Emby.Server.Implementations.Library.Resolvers; using Emby.Server.Implementations.Library.Validators; using Emby.Server.Implementations.Playlists; using Emby.Server.Implementations.ScheduledTasks; @@ -78,6 +78,7 @@ namespace Emby.Server.Implementations.Library private readonly IItemRepository _itemRepository; private readonly IImageProcessor _imageProcessor; private readonly NamingOptions _namingOptions; + private readonly ExtraResolver _extraResolver; /// /// The _root folder sync lock. @@ -146,6 +147,8 @@ namespace Emby.Server.Implementations.Library _memoryCache = memoryCache; _namingOptions = namingOptions; + _extraResolver = new ExtraResolver(namingOptions); + _configurationManager.ConfigurationUpdated += ConfigurationUpdated; RecordConfigurationValues(configurationManager.Configuration); @@ -2692,8 +2695,6 @@ namespace Emby.Server.Implementations.Library } var count = fileSystemChildren.Count; - var files = new List(); - var nonVideoFiles = new List(); for (var i = 0; i < count; i++) { var current = fileSystemChildren[i]; @@ -2702,85 +2703,47 @@ namespace Emby.Server.Implementations.Library var filesInSubFolder = _fileSystem.GetFiles(current.FullName, _namingOptions.VideoFileExtensions, false, false); foreach (var file in filesInSubFolder) { - var videoInfo = VideoResolver.Resolve(file.FullName, file.IsDirectory, _namingOptions); - if (videoInfo == null) + if (!_extraResolver.TryGetExtraTypeForOwner(file.FullName, ownerVideoInfo, out var extraType)) { - nonVideoFiles.Add(file); continue; } - files.Add(videoInfo); + var extra = GetExtra(file, extraType.Value); + if (extra != null) + { + yield return extra; + } } } - else if (!current.IsDirectory) + else if (!current.IsDirectory && _extraResolver.TryGetExtraTypeForOwner(current.FullName, ownerVideoInfo, out var extraType)) { - var videoInfo = VideoResolver.Resolve(current.FullName, current.IsDirectory, _namingOptions); - if (videoInfo == null) + var extra = GetExtra(current, extraType.Value); + if (extra != null) { - nonVideoFiles.Add(current); - continue; + yield return extra; } - - files.Add(videoInfo); } } - if (files.Count == 0) - { - yield break; - } - - var videos = VideoListResolver.Resolve(files, _namingOptions); - // owner video info cannot be null as that implies it has no path - var extras = ExtraResolver.GetExtras(videos, ownerVideoInfo, _namingOptions.VideoFlagDelimiters); - for (var i = 0; i < extras.Count; i++) + BaseItem GetExtra(FileSystemMetadata file, ExtraType extraType) { - var currentExtra = extras[i]; - var resolved = ResolvePath(_fileSystem.GetFileInfo(currentExtra.Path), null, directoryService); - if (resolved is not Video video) - { - continue; - } - - // Try to retrieve it from the db. If we don't find it, use the resolved version - if (GetItemById(resolved.Id) is Video dbItem) + var extra = ResolvePath(_fileSystem.GetFileInfo(file.FullName), directoryService, _extraResolver.GetResolversForExtraType(extraType)); + if (extra is not Video && extra is not Audio) { - video = dbItem; - } - - video.ExtraType = currentExtra.ExtraType; - video.ParentId = Guid.Empty; - video.OwnerId = owner.Id; - yield return video; - } - - // TODO: theme songs must be handled "manually" (but should we?) since they aren't video files - for (var i = 0; i < nonVideoFiles.Count; i++) - { - var current = nonVideoFiles[i]; - var extraInfo = ExtraResolver.GetExtraInfo(current.FullName, _namingOptions); - if (extraInfo.ExtraType != ExtraType.ThemeSong) - { - continue; - } - - var resolved = ResolvePath(current, null, directoryService); - if (resolved is not Audio themeSong) - { - continue; + return null; } // Try to retrieve it from the db. If we don't find it, use the resolved version - if (GetItemById(themeSong.Id) is Audio dbItem) + var itemById = GetItemById(extra.Id); + if (itemById != null) { - themeSong = dbItem; + extra = itemById; } - themeSong.ExtraType = ExtraType.ThemeSong; - themeSong.OwnerId = owner.Id; - themeSong.ParentId = Guid.Empty; - - yield return themeSong; + extra.ExtraType = extraType; + extra.ParentId = Guid.Empty; + extra.OwnerId = owner.Id; + return extra; } } -- cgit v1.2.3 From 28c2ac9cc0c2c7d9ed6e38b8de83755899e210f5 Mon Sep 17 00:00:00 2001 From: cvium Date: Sat, 1 Jan 2022 20:07:03 +0100 Subject: Remove file extension filter and fix build --- .../Library/LibraryManager.cs | 2 +- .../Library/Resolvers/GenericVideoResolver.cs | 4 +- .../Library/LibraryManager/FindExtrasTests.cs | 49 ++++++++++++++++++++-- 3 files changed, 48 insertions(+), 7 deletions(-) (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 694501d38..855fcc813 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -2700,7 +2700,7 @@ namespace Emby.Server.Implementations.Library var current = fileSystemChildren[i]; if (current.IsDirectory && _namingOptions.AllExtrasTypesFolderNames.ContainsKey(current.Name)) { - var filesInSubFolder = _fileSystem.GetFiles(current.FullName, _namingOptions.VideoFileExtensions, false, false); + var filesInSubFolder = _fileSystem.GetFiles(current.FullName, null, false, false); foreach (var file in filesInSubFolder) { if (!_extraResolver.TryGetExtraTypeForOwner(file.FullName, ownerVideoInfo, out var extraType)) diff --git a/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs b/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs index 9b6838660..b8554bd51 100644 --- a/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs +++ b/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs @@ -6,14 +6,14 @@ using MediaBrowser.Controller.Entities; namespace Emby.Server.Implementations.Library.Resolvers { /// - /// Resolves a Path into a Video or Video subclass. + /// Resolves a Path into an instance of the class. /// /// The type of item to resolve. public class GenericVideoResolver : BaseVideoResolver where T : Video, new() { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The naming options. public GenericVideoResolver(NamingOptions namingOptions) diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/FindExtrasTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/FindExtrasTests.cs index 5be111ad9..de4421320 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/FindExtrasTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/FindExtrasTests.cs @@ -108,7 +108,7 @@ public class FindExtrasTests Name = "some trailer.mkv", IsDirectory = false } - }); + }).Verifiable(); _fileSystemMock.Setup(f => f.GetFiles( "/movies/Up/behind the scenes", @@ -123,7 +123,7 @@ public class FindExtrasTests Name = "the making of Up.mkv", IsDirectory = false } - }); + }).Verifiable(); _fileSystemMock.Setup(f => f.GetFiles( "/movies/Up/theme-music", @@ -138,17 +138,18 @@ public class FindExtrasTests Name = "theme2.mp3", IsDirectory = false } - }); + }).Verifiable(); var files = paths.Select(p => new FileSystemMetadata { FullName = p, Name = Path.GetFileName(p), - IsDirectory = string.IsNullOrEmpty(Path.GetExtension(p)) + IsDirectory = !Path.HasExtension(p) }).ToList(); var extras = _libraryManager.FindExtras(owner, files, new DirectoryService(_fileSystemMock.Object)).OrderBy(e => e.ExtraType).ToList(); + _fileSystemMock.Verify(); Assert.Equal(6, extras.Count); Assert.Equal(ExtraType.Trailer, extras[0].ExtraType); Assert.Equal(typeof(Trailer), extras[0].GetType()); @@ -215,6 +216,46 @@ public class FindExtrasTests Assert.Equal("/movies/Up/trailer.mkv", extras[0].Path); } + [Fact] + public void FindExtras_WrongExtensions_FindsNoExtras() + { + var owner = new Movie { Name = "Up", Path = "/movies/Up/Up.mkv" }; + var paths = new List + { + "/movies/Up/Up.mkv", + "/movies/Up/trailer.noext", + "/movies/Up/theme.png", + "/movies/Up/trailers" + }; + + var files = paths.Select(p => new FileSystemMetadata + { + FullName = p, + Name = Path.GetFileName(p), + IsDirectory = !Path.HasExtension(p) + }).ToList(); + + _fileSystemMock.Setup(f => f.GetFiles( + "/movies/Up/trailers", + It.IsAny(), + false, + false)) + .Returns(new List + { + new() + { + FullName = "/movies/Up/trailers/trailer.jpg", + Name = "trailer.jpg", + IsDirectory = false + } + }).Verifiable(); + + var extras = _libraryManager.FindExtras(owner, files, new DirectoryService(_fileSystemMock.Object)).OrderBy(e => e.ExtraType).ToList(); + + _fileSystemMock.Verify(); + Assert.Empty(extras); + } + [Fact] public void FindExtras_SeriesWithTrailers_FindsCorrectExtras() { -- cgit v1.2.3