Merge pull request #7058 from cvium/the_most_important_feature

This commit is contained in:
Bond-009 2022-01-02 12:31:20 +01:00 committed by GitHub
commit 11d0c6827f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 205 additions and 195 deletions

View File

@ -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
/// <summary>
/// Resolve if file is extra for video.
/// </summary>
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;
}
/// <summary>
/// Finds extras matching the video info.
/// </summary>
/// <param name="files">The list of file video infos.</param>
/// <param name="videoInfo">The video to compare against.</param>
/// <param name="videoFlagDelimiters">The video flag delimiters.</param>
/// <returns>A list of video extras for [videoInfo].</returns>
public static IReadOnlyList<VideoFileInfo> GetExtras(IReadOnlyList<VideoInfo> files, VideoFileInfo videoInfo, ReadOnlySpan<char> 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<VideoFileInfo>();
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<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);
}
}
}

View File

@ -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<VideoInfo>();
@ -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);
}

View File

@ -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);

View File

@ -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)
{
nonVideoFiles.Add(file);
continue;
}
files.Add(videoInfo);
}
}
else if (!current.IsDirectory)
{
var videoInfo = VideoResolver.Resolve(current.FullName, current.IsDirectory, _namingOptions);
if (videoInfo == null)
{
nonVideoFiles.Add(current);
continue;
}
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++)
{
var currentExtra = extras[i];
var resolved = ResolvePath(_fileSystem.GetFileInfo(currentExtra.Path), null, directoryService);
if (resolved is not Video video)
if (!_extraResolver.TryGetExtraTypeForOwner(file.FullName, ownerVideoInfo, out var extraType))
{
continue;
}
var extra = GetExtra(file, extraType.Value);
if (extra != null)
{
yield return extra;
}
}
}
else if (!current.IsDirectory && _extraResolver.TryGetExtraTypeForOwner(current.FullName, ownerVideoInfo, out var extraType))
{
var extra = GetExtra(current, extraType.Value);
if (extra != null)
{
yield return extra;
}
}
}
BaseItem GetExtra(FileSystemMetadata file, ExtraType extraType)
{
var extra = ResolvePath(_fileSystem.GetFileInfo(file.FullName), directoryService, _extraResolver.GetResolversForExtraType(extraType));
if (extra is not Video && extra is not Audio)
{
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;
}
}

View File

@ -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);
}
}
}

View File

@ -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)
{
}
}
}

View File

@ -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;
}
}
}

View File

@ -82,7 +82,7 @@ namespace Jellyfin.Naming.Tests.Video
private void Test(string input, ExtraType? expectedType)
{
var extraType = ExtraResolver.GetExtraInfo(input, _videoOptions).ExtraType;
var extraType = ExtraRuleResolver.GetExtraInfo(input, _videoOptions).ExtraType;
Assert.Equal(expectedType, extraType);
}
@ -92,7 +92,7 @@ namespace Jellyfin.Naming.Tests.Video
{
var rule = new ExtraRule(ExtraType.Unknown, ExtraRuleType.Regex, @"([eE]x(tra)?\.\w+)", MediaType.Video);
var options = new NamingOptions { VideoExtraRules = new[] { rule } };
var res = ExtraResolver.GetExtraInfo("extra.mp4", options);
var res = ExtraRuleResolver.GetExtraInfo("extra.mp4", options);
Assert.Equal(rule, res.Rule);
}

View File

@ -1,16 +1,18 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AutoFixture;
using AutoFixture.AutoMoq;
using Emby.Naming.Common;
using Emby.Server.Implementations.Library.Resolvers;
using Emby.Server.Implementations.Library.Resolvers.Audio;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Controller.Sorting;
@ -32,11 +34,13 @@ public class FindExtrasTests
fixture.Register(() => new NamingOptions());
var configMock = fixture.Freeze<Mock<IServerConfigurationManager>>();
configMock.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns("/data");
var itemRepository = fixture.Freeze<Mock<IItemRepository>>();
itemRepository.Setup(i => i.RetrieveItem(It.IsAny<Guid>())).Returns<BaseItem>(null);
_fileSystemMock = fixture.Freeze<Mock<IFileSystem>>();
_fileSystemMock.Setup(f => f.GetFileInfo(It.IsAny<string>())).Returns<string>(path => new FileSystemMetadata { FullName = path });
_libraryManager = fixture.Build<Emby.Server.Implementations.Library.LibraryManager>().Do(s => s.AddParts(
fixture.Create<IEnumerable<IResolverIgnoreRule>>(),
new List<IItemResolver> { new VideoExtraResolver(fixture.Create<NamingOptions>()), new AudioResolver(fixture.Create<NamingOptions>()) },
new List<IItemResolver> { new AudioResolver(fixture.Create<NamingOptions>()) },
fixture.Create<IEnumerable<IIntroProvider>>(),
fixture.Create<IEnumerable<IBaseItemComparer>>(),
fixture.Create<IEnumerable<ILibraryPostScanTask>>()))
@ -104,7 +108,7 @@ public class FindExtrasTests
Name = "some trailer.mkv",
IsDirectory = false
}
});
}).Verifiable();
_fileSystemMock.Setup(f => f.GetFiles(
"/movies/Up/behind the scenes",
@ -119,7 +123,7 @@ public class FindExtrasTests
Name = "the making of Up.mkv",
IsDirectory = false
}
});
}).Verifiable();
_fileSystemMock.Setup(f => f.GetFiles(
"/movies/Up/theme-music",
@ -134,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());
@ -153,7 +158,9 @@ public class FindExtrasTests
Assert.Equal(ExtraType.BehindTheScenes, extras[2].ExtraType);
Assert.Equal(ExtraType.Sample, extras[3].ExtraType);
Assert.Equal(ExtraType.ThemeSong, extras[4].ExtraType);
Assert.Equal(typeof(Audio), extras[4].GetType());
Assert.Equal(ExtraType.ThemeSong, extras[5].ExtraType);
Assert.Equal(typeof(Audio), extras[5].GetType());
}
[Fact]
@ -209,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<string>
{
"/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<string[]>(),
false,
false))
.Returns(new List<FileSystemMetadata>
{
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()
{