aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations
diff options
context:
space:
mode:
authorcvium <clausvium@gmail.com>2021-12-28 00:37:40 +0100
committercvium <clausvium@gmail.com>2021-12-28 00:37:40 +0100
commit2749509f001505d35863db4b53bb4bc6c3af6fa4 (patch)
tree3da58eea3f1132eb52d7861748663721b3892927 /Emby.Server.Implementations
parent4441513ca4a64fdf61541e56cbb1091e7aa0395b (diff)
Use dedicated resolvers for extras
Diffstat (limited to 'Emby.Server.Implementations')
-rw-r--r--Emby.Server.Implementations/Library/LibraryManager.cs87
-rw-r--r--Emby.Server.Implementations/Library/Resolvers/ExtraResolver.cs93
-rw-r--r--Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs18
-rw-r--r--Emby.Server.Implementations/Library/Resolvers/VideoExtraResolver.cs55
4 files changed, 136 insertions, 117 deletions
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;
/// <summary>
/// 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<VideoFileInfo>();
- var nonVideoFiles = new List<FileSystemMetadata>();
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;
}
}
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
+{
+ /// <summary>
+ /// Resolves a Path into a Video or Video subclass.
+ /// </summary>
+ internal class ExtraResolver
+ {
+ private readonly NamingOptions _namingOptions;
+ private readonly IItemResolver[] _trailerResolvers;
+ private readonly IItemResolver[] _videoResolvers;
+
+ /// <summary>
+ /// Initializes an new instance of the <see cref="ExtraResolver"/> class.
+ /// </summary>
+ /// <param name="namingOptions">An instance of <see cref="NamingOptions"/>.</param>
+ public ExtraResolver(NamingOptions namingOptions)
+ {
+ _namingOptions = namingOptions;
+ _trailerResolvers = new IItemResolver[] { new GenericVideoResolver<Trailer>(namingOptions) };
+ _videoResolvers = new IItemResolver[] { new GenericVideoResolver<Video>(namingOptions) };
+ }
+
+ /// <summary>
+ /// Gets the resolvers for the extra type.
+ /// </summary>
+ /// <param name="extraType">The extra type.</param>
+ /// <returns>The resolvers for the extra type.</returns>
+ public IItemResolver[]? GetResolversForExtraType(ExtraType extraType) => extraType switch
+ {
+ ExtraType.Trailer => _trailerResolvers,
+ // For audio we'll have to rely on the AudioResolver, which is a "built-in"
+ ExtraType.ThemeSong => null,
+ _ => _videoResolvers
+ };
+
+ public bool TryGetExtraTypeForOwner(string path, VideoFileInfo ownerVideoFileInfo, [NotNullWhen(true)] out ExtraType? extraType)
+ {
+ var extraResult = GetExtraInfo(path, _namingOptions);
+ if (extraResult.ExtraType == null)
+ {
+ extraType = null;
+ return false;
+ }
+
+ var cleanDateTimeResult = CleanDateTimeParser.Clean(Path.GetFileNameWithoutExtension(path), _namingOptions.CleanDateTimeRegexes);
+ var name = cleanDateTimeResult.Name;
+ var year = cleanDateTimeResult.Year;
+
+ var parentDir = ownerVideoFileInfo.IsDirectory ? ownerVideoFileInfo.Path : Path.GetDirectoryName(ownerVideoFileInfo.Path.AsSpan());
+
+ var trimmedFileNameWithoutExtension = TrimFilenameDelimiters(ownerVideoFileInfo.FileNameWithoutExtension, _namingOptions.VideoFlagDelimiters);
+ var trimmedVideoInfoName = TrimFilenameDelimiters(ownerVideoFileInfo.Name, _namingOptions.VideoFlagDelimiters);
+ var trimmedExtraFileName = TrimFilenameDelimiters(name, _namingOptions.VideoFlagDelimiters);
+
+ // first check filenames
+ bool isValid = StartsWith(trimmedExtraFileName, trimmedFileNameWithoutExtension)
+ || (StartsWith(trimmedExtraFileName, trimmedVideoInfoName) && year == ownerVideoFileInfo.Year);
+
+ if (!isValid)
+ {
+ // When the extra rule type is DirectoryName we must go one level higher to get the "real" dir name
+ var currentParentDir = extraResult.Rule?.RuleType == ExtraRuleType.DirectoryName
+ ? Path.GetDirectoryName(Path.GetDirectoryName(path.AsSpan()))
+ : Path.GetDirectoryName(path.AsSpan());
+
+ isValid = !currentParentDir.IsEmpty && !parentDir.IsEmpty && currentParentDir.Equals(parentDir, StringComparison.OrdinalIgnoreCase);
+ }
+
+ extraType = extraResult.ExtraType;
+ return isValid;
+ }
+
+ private static ReadOnlySpan<char> TrimFilenameDelimiters(ReadOnlySpan<char> name, ReadOnlySpan<char> videoFlagDelimiters)
+ {
+ return name.IsEmpty ? name : name.TrimEnd().TrimEnd(videoFlagDelimiters).TrimEnd();
+ }
+
+ private static bool StartsWith(ReadOnlySpan<char> fileName, ReadOnlySpan<char> baseName)
+ {
+ return !baseName.IsEmpty && fileName.StartsWith(baseName, StringComparison.OrdinalIgnoreCase);
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs b/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs
new file mode 100644
index 000000000..27d781148
--- /dev/null
+++ b/Emby.Server.Implementations/Library/Resolvers/GenericVideoResolver.cs
@@ -0,0 +1,18 @@
+#nullable disable
+
+#pragma warning disable CS1591
+
+using Emby.Naming.Common;
+using MediaBrowser.Controller.Entities;
+
+namespace Emby.Server.Implementations.Library.Resolvers
+{
+ public class GenericVideoResolver<T> : BaseVideoResolver<T>
+ where T : Video, new()
+ {
+ public GenericVideoResolver(NamingOptions namingOptions)
+ : base(namingOptions)
+ {
+ }
+ }
+}
diff --git a/Emby.Server.Implementations/Library/Resolvers/VideoExtraResolver.cs b/Emby.Server.Implementations/Library/Resolvers/VideoExtraResolver.cs
deleted file mode 100644
index 9aadce88c..000000000
--- a/Emby.Server.Implementations/Library/Resolvers/VideoExtraResolver.cs
+++ /dev/null
@@ -1,55 +0,0 @@
-#nullable disable
-
-using Emby.Naming.Common;
-using MediaBrowser.Controller.Entities;
-using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.Resolvers;
-using MediaBrowser.Model.Entities;
-
-namespace Emby.Server.Implementations.Library.Resolvers
-{
- /// <summary>
- /// Resolves a Path into a Video or Video subclass.
- /// </summary>
- public class VideoExtraResolver : BaseVideoResolver<Video>
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="VideoExtraResolver"/> class.
- /// </summary>
- /// <param name="namingOptions">The naming options.</param>
- public VideoExtraResolver(NamingOptions namingOptions)
- : base(namingOptions)
- {
- }
-
- /// <summary>
- /// Gets the priority.
- /// </summary>
- /// <value>The priority.</value>
- public override ResolverPriority Priority => ResolverPriority.Last;
-
- /// <summary>
- /// Resolves the specified args.
- /// </summary>
- /// <param name="args">The args.</param>
- /// <returns>The video extra or null if not handled by this resolver.</returns>
- public override Video Resolve(ItemResolveArgs args)
- {
- // Only handle owned items
- if (args.Parent != null)
- {
- return null;
- }
-
- var ownedItem = base.Resolve(args);
-
- // Re-resolve items that have their own type
- if (ownedItem.ExtraType == ExtraType.Trailer)
- {
- ownedItem = ResolveVideo<Trailer>(args, false);
- }
-
- return ownedItem;
- }
- }
-}