ExtraRuleResolver.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using Emby.Naming.Audio;
  5. using Emby.Naming.Common;
  6. namespace Emby.Naming.Video
  7. {
  8. /// <summary>
  9. /// Resolve if file is extra for video.
  10. /// </summary>
  11. public static class ExtraRuleResolver
  12. {
  13. private static readonly char[] _digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  14. /// <summary>
  15. /// Attempts to resolve if file is extra.
  16. /// </summary>
  17. /// <param name="path">Path to file.</param>
  18. /// <param name="namingOptions">The naming options.</param>
  19. /// <param name="libraryRoot">Top-level folder for the containing library.</param>
  20. /// <returns>Returns <see cref="ExtraResult"/> object.</returns>
  21. public static ExtraResult GetExtraInfo(string path, NamingOptions namingOptions, string? libraryRoot = "")
  22. {
  23. var result = new ExtraResult();
  24. for (var i = 0; i < namingOptions.VideoExtraRules.Length; i++)
  25. {
  26. var rule = namingOptions.VideoExtraRules[i];
  27. if ((rule.MediaType == MediaType.Audio && !AudioFileParser.IsAudioFile(path, namingOptions))
  28. || (rule.MediaType == MediaType.Video && !VideoResolver.IsVideoFile(path, namingOptions)))
  29. {
  30. continue;
  31. }
  32. var pathSpan = path.AsSpan();
  33. if (rule.RuleType == ExtraRuleType.Filename)
  34. {
  35. var filename = Path.GetFileNameWithoutExtension(pathSpan);
  36. if (filename.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
  37. {
  38. result.ExtraType = rule.ExtraType;
  39. result.Rule = rule;
  40. }
  41. }
  42. else if (rule.RuleType == ExtraRuleType.Suffix)
  43. {
  44. // Trim the digits from the end of the filename so we can recognize things like -trailer2
  45. var filename = Path.GetFileNameWithoutExtension(pathSpan).TrimEnd(_digits);
  46. if (filename.EndsWith(rule.Token, StringComparison.OrdinalIgnoreCase))
  47. {
  48. result.ExtraType = rule.ExtraType;
  49. result.Rule = rule;
  50. }
  51. }
  52. else if (rule.RuleType == ExtraRuleType.Regex)
  53. {
  54. var filename = Path.GetFileName(path.AsSpan());
  55. var isMatch = Regex.IsMatch(filename, rule.Token, RegexOptions.IgnoreCase | RegexOptions.Compiled);
  56. if (isMatch)
  57. {
  58. result.ExtraType = rule.ExtraType;
  59. result.Rule = rule;
  60. }
  61. }
  62. else if (rule.RuleType == ExtraRuleType.DirectoryName)
  63. {
  64. var directoryName = Path.GetFileName(Path.GetDirectoryName(pathSpan));
  65. string fullDirectory = Path.GetDirectoryName(pathSpan).ToString();
  66. if (directoryName.Equals(rule.Token, StringComparison.OrdinalIgnoreCase)
  67. && !string.Equals(fullDirectory, libraryRoot, StringComparison.OrdinalIgnoreCase))
  68. {
  69. result.ExtraType = rule.ExtraType;
  70. result.Rule = rule;
  71. }
  72. }
  73. if (result.ExtraType is not null)
  74. {
  75. return result;
  76. }
  77. }
  78. return result;
  79. }
  80. }
  81. }