mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-21 01:24:44 +01:00
Merge pull request #7058 from cvium/the_most_important_feature
This commit is contained in:
@@ -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.Tasks;
|
||||
@@ -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,95 +2695,55 @@ 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];
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#nullable disable
|
||||
|
||||
using Emby.Naming.Common;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
|
||||
namespace Emby.Server.Implementations.Library.Resolvers
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves a Path into an instance of the <see cref="Video"/> class.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of item to resolve.</typeparam>
|
||||
public class GenericVideoResolver<T> : BaseVideoResolver<T>
|
||||
where T : Video, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GenericVideoResolver{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="namingOptions">The naming options.</param>
|
||||
public GenericVideoResolver(NamingOptions namingOptions)
|
||||
: base(namingOptions)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user