From 2749509f001505d35863db4b53bb4bc6c3af6fa4 Mon Sep 17 00:00:00 2001 From: cvium Date: Tue, 28 Dec 2021 00:37:40 +0100 Subject: [PATCH 1/3] Use dedicated resolvers for extras --- ...{ExtraResolver.cs => ExtraRuleResolver.cs} | 65 +------------ Emby.Naming/Video/VideoListResolver.cs | 7 +- Emby.Naming/Video/VideoResolver.cs | 2 +- .../Library/LibraryManager.cs | 87 +++++------------ .../Library/Resolvers/ExtraResolver.cs | 93 +++++++++++++++++++ .../Library/Resolvers/GenericVideoResolver.cs | 18 ++++ .../Library/Resolvers/VideoExtraResolver.cs | 55 ----------- .../Jellyfin.Naming.Tests/Video/ExtraTests.cs | 4 +- .../Library/LibraryManager/FindExtrasTests.cs | 12 ++- 9 files changed, 153 insertions(+), 190 deletions(-) rename Emby.Naming/Video/{ExtraResolver.cs => ExtraRuleResolver.cs} (51%) create mode 100644 Emby.Server.Implementations/Library/Resolvers/ExtraResolver.cs create mode 100644 Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs delete mode 100644 Emby.Server.Implementations/Library/Resolvers/VideoExtraResolver.cs diff --git a/Emby.Naming/Video/ExtraResolver.cs b/Emby.Naming/Video/ExtraRuleResolver.cs similarity index 51% rename from Emby.Naming/Video/ExtraResolver.cs rename to Emby.Naming/Video/ExtraRuleResolver.cs index fbdca859f..0970e509a 100644 --- a/Emby.Naming/Video/ExtraResolver.cs +++ b/Emby.Naming/Video/ExtraRuleResolver.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text.RegularExpressions; using Emby.Naming.Audio; using Emby.Naming.Common; @@ -11,7 +9,7 @@ namespace Emby.Naming.Video /// /// Resolve if file is extra for video. /// - public static class ExtraResolver + public static class ExtraRuleResolver { private static readonly char[] _digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; @@ -86,66 +84,5 @@ namespace Emby.Naming.Video return result; } - - /// - /// Finds extras matching the video info. - /// - /// The list of file video infos. - /// The video to compare against. - /// The video flag delimiters. - /// A list of video extras for [videoInfo]. - public static IReadOnlyList GetExtras(IReadOnlyList files, VideoFileInfo videoInfo, ReadOnlySpan videoFlagDelimiters) - { - var parentDir = videoInfo.IsDirectory ? videoInfo.Path : Path.GetDirectoryName(videoInfo.Path.AsSpan()); - - var trimmedFileNameWithoutExtension = TrimFilenameDelimiters(videoInfo.FileNameWithoutExtension, videoFlagDelimiters); - var trimmedVideoInfoName = TrimFilenameDelimiters(videoInfo.Name, videoFlagDelimiters); - - var result = new List(); - for (var pos = files.Count - 1; pos >= 0; pos--) - { - var current = files[pos]; - // ignore non-extras and multi-file (can this happen?) - if (current.ExtraType == null || current.Files.Count > 1) - { - continue; - } - - var currentFile = current.Files[0]; - var trimmedCurrentFileName = TrimFilenameDelimiters(currentFile.Name, videoFlagDelimiters); - - // first check filenames - bool isValid = StartsWith(trimmedCurrentFileName, trimmedFileNameWithoutExtension) - || (StartsWith(trimmedCurrentFileName, trimmedVideoInfoName) && currentFile.Year == videoInfo.Year); - - // then by directory - if (!isValid) - { - // When the extra rule type is DirectoryName we must go one level higher to get the "real" dir name - var currentParentDir = currentFile.ExtraRule?.RuleType == ExtraRuleType.DirectoryName - ? Path.GetDirectoryName(Path.GetDirectoryName(currentFile.Path.AsSpan())) - : Path.GetDirectoryName(currentFile.Path.AsSpan()); - - isValid = !currentParentDir.IsEmpty && !parentDir.IsEmpty && currentParentDir.Equals(parentDir, StringComparison.OrdinalIgnoreCase); - } - - if (isValid) - { - result.Add(currentFile); - } - } - - return result.OrderBy(r => r.Path).ToArray(); - } - - private static ReadOnlySpan TrimFilenameDelimiters(ReadOnlySpan name, ReadOnlySpan videoFlagDelimiters) - { - return name.IsEmpty ? name : name.TrimEnd().TrimEnd(videoFlagDelimiters).TrimEnd(); - } - - private static bool StartsWith(ReadOnlySpan fileName, ReadOnlySpan baseName) - { - return !baseName.IsEmpty && fileName.StartsWith(baseName, StringComparison.OrdinalIgnoreCase); - } } } diff --git a/Emby.Naming/Video/VideoListResolver.cs b/Emby.Naming/Video/VideoListResolver.cs index 4fc849256..11f82525f 100644 --- a/Emby.Naming/Video/VideoListResolver.cs +++ b/Emby.Naming/Video/VideoListResolver.cs @@ -42,11 +42,14 @@ namespace Emby.Naming.Video continue; } - remainingFiles.Add(current); if (current.ExtraType == null) { standaloneMedia.Add(current); } + else + { + remainingFiles.Add(current); + } } var list = new List(); @@ -69,8 +72,6 @@ namespace Emby.Naming.Video var info = new VideoInfo(media.Name) { Files = new[] { media } }; info.Year = info.Files[0].Year; - - remainingFiles.Remove(media); list.Add(info); } diff --git a/Emby.Naming/Video/VideoResolver.cs b/Emby.Naming/Video/VideoResolver.cs index 9cadc1465..de8e177d8 100644 --- a/Emby.Naming/Video/VideoResolver.cs +++ b/Emby.Naming/Video/VideoResolver.cs @@ -75,7 +75,7 @@ namespace Emby.Naming.Video var format3DResult = Format3DParser.Parse(path, namingOptions); - var extraResult = ExtraResolver.GetExtraInfo(path, namingOptions); + var extraResult = ExtraRuleResolver.GetExtraInfo(path, namingOptions); var name = Path.GetFileNameWithoutExtension(path); 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) + BaseItem GetExtra(FileSystemMetadata file, ExtraType extraType) { - 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++) - { - var currentExtra = extras[i]; - var resolved = ResolvePath(_fileSystem.GetFileInfo(currentExtra.Path), null, directoryService); - if (resolved is not Video video) + var extra = ResolvePath(_fileSystem.GetFileInfo(file.FullName), directoryService, _extraResolver.GetResolversForExtraType(extraType)); + if (extra is not Video && extra is not Audio) { - continue; + return null; } // 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 itemById = GetItemById(extra.Id); + if (itemById != null) { - video = dbItem; + extra = itemById; } - 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; - } - - // Try to retrieve it from the db. If we don't find it, use the resolved version - if (GetItemById(themeSong.Id) is Audio dbItem) - { - themeSong = dbItem; - } - - 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; } } diff --git a/Emby.Server.Implementations/Library/Resolvers/ExtraResolver.cs b/Emby.Server.Implementations/Library/Resolvers/ExtraResolver.cs new file mode 100644 index 000000000..3d06ceb5e --- /dev/null +++ b/Emby.Server.Implementations/Library/Resolvers/ExtraResolver.cs @@ -0,0 +1,93 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using Emby.Naming.Common; +using Emby.Naming.Video; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Resolvers; +using MediaBrowser.Model.Entities; +using static Emby.Naming.Video.ExtraRuleResolver; + +namespace Emby.Server.Implementations.Library.Resolvers +{ + /// + /// Resolves a Path into a Video or Video subclass. + /// + internal class ExtraResolver + { + private readonly NamingOptions _namingOptions; + private readonly IItemResolver[] _trailerResolvers; + private readonly IItemResolver[] _videoResolvers; + + /// + /// Initializes an new instance of the class. + /// + /// An instance of . + public ExtraResolver(NamingOptions namingOptions) + { + _namingOptions = namingOptions; + _trailerResolvers = new IItemResolver[] { new GenericVideoResolver(namingOptions) }; + _videoResolvers = new IItemResolver[] { new GenericVideoResolver