2
0

ExtraResolver.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.IO;
  4. using Emby.Naming.Common;
  5. using Emby.Naming.Video;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Controller.Resolvers;
  10. using MediaBrowser.Model.Entities;
  11. using Microsoft.Extensions.Logging;
  12. using static Emby.Naming.Video.ExtraRuleResolver;
  13. namespace Emby.Server.Implementations.Library.Resolvers
  14. {
  15. /// <summary>
  16. /// Resolves a Path into a Video or Video subclass.
  17. /// </summary>
  18. internal class ExtraResolver : BaseVideoResolver<Video>
  19. {
  20. private readonly NamingOptions _namingOptions;
  21. private readonly IItemResolver[] _trailerResolvers;
  22. private readonly IItemResolver[] _videoResolvers;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="ExtraResolver"/> class.
  25. /// </summary>
  26. /// <param name="logger">The logger.</param>
  27. /// <param name="namingOptions">An instance of <see cref="NamingOptions"/>.</param>
  28. /// <param name="directoryService">The directory service.</param>
  29. public ExtraResolver(ILogger<ExtraResolver> logger, NamingOptions namingOptions, IDirectoryService directoryService)
  30. : base(logger, namingOptions, directoryService)
  31. {
  32. _namingOptions = namingOptions;
  33. _trailerResolvers = new IItemResolver[] { new GenericVideoResolver<Trailer>(logger, namingOptions, directoryService) };
  34. _videoResolvers = new IItemResolver[] { this };
  35. }
  36. protected override Video Resolve(ItemResolveArgs args)
  37. {
  38. return ResolveVideo<Video>(args, true);
  39. }
  40. /// <summary>
  41. /// Gets the resolvers for the extra type.
  42. /// </summary>
  43. /// <param name="extraType">The extra type.</param>
  44. /// <returns>The resolvers for the extra type.</returns>
  45. public IItemResolver[]? GetResolversForExtraType(ExtraType extraType) => extraType switch
  46. {
  47. ExtraType.Trailer => _trailerResolvers,
  48. // For audio we'll have to rely on the AudioResolver, which is a "built-in"
  49. ExtraType.ThemeSong => null,
  50. _ => _videoResolvers
  51. };
  52. public bool TryGetExtraTypeForOwner(string path, VideoFileInfo ownerVideoFileInfo, [NotNullWhen(true)] out ExtraType? extraType, string? libraryRoot = "")
  53. {
  54. var extraResult = GetExtraInfo(path, _namingOptions, libraryRoot);
  55. if (extraResult.ExtraType is null)
  56. {
  57. extraType = null;
  58. return false;
  59. }
  60. var cleanDateTimeResult = CleanDateTimeParser.Clean(Path.GetFileNameWithoutExtension(path), _namingOptions.CleanDateTimeRegexes);
  61. var name = cleanDateTimeResult.Name;
  62. var year = cleanDateTimeResult.Year;
  63. var parentDir = ownerVideoFileInfo.IsDirectory ? ownerVideoFileInfo.Path : Path.GetDirectoryName(ownerVideoFileInfo.Path.AsSpan());
  64. var trimmedFileNameWithoutExtension = TrimFilenameDelimiters(ownerVideoFileInfo.FileNameWithoutExtension, _namingOptions.VideoFlagDelimiters);
  65. var trimmedVideoInfoName = TrimFilenameDelimiters(ownerVideoFileInfo.Name, _namingOptions.VideoFlagDelimiters);
  66. var trimmedExtraFileName = TrimFilenameDelimiters(name, _namingOptions.VideoFlagDelimiters);
  67. // first check filenames
  68. bool isValid = StartsWith(trimmedExtraFileName, trimmedFileNameWithoutExtension)
  69. || (StartsWith(trimmedExtraFileName, trimmedVideoInfoName) && year == ownerVideoFileInfo.Year);
  70. if (!isValid)
  71. {
  72. // When the extra rule type is DirectoryName we must go one level higher to get the "real" dir name
  73. var currentParentDir = extraResult.Rule?.RuleType == ExtraRuleType.DirectoryName
  74. ? Path.GetDirectoryName(Path.GetDirectoryName(path.AsSpan()))
  75. : Path.GetDirectoryName(path.AsSpan());
  76. isValid = !currentParentDir.IsEmpty && !parentDir.IsEmpty && currentParentDir.Equals(parentDir, StringComparison.OrdinalIgnoreCase);
  77. }
  78. extraType = extraResult.ExtraType;
  79. return isValid;
  80. }
  81. private static ReadOnlySpan<char> TrimFilenameDelimiters(ReadOnlySpan<char> name, ReadOnlySpan<char> videoFlagDelimiters)
  82. {
  83. return name.IsEmpty ? name : name.TrimEnd().TrimEnd(videoFlagDelimiters).TrimEnd();
  84. }
  85. private static bool StartsWith(ReadOnlySpan<char> fileName, ReadOnlySpan<char> baseName)
  86. {
  87. return !baseName.IsEmpty && fileName.StartsWith(baseName, StringComparison.OrdinalIgnoreCase);
  88. }
  89. }
  90. }