ExtraResolver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 class ExtraResolver
  12. {
  13. private readonly NamingOptions _options;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="ExtraResolver"/> class.
  16. /// </summary>
  17. /// <param name="options"><see cref="NamingOptions"/> object containing VideoExtraRules and passed to <see cref="AudioFileParser"/> and <see cref="VideoResolver"/>.</param>
  18. public ExtraResolver(NamingOptions options)
  19. {
  20. _options = options;
  21. }
  22. /// <summary>
  23. /// Attempts to resolve if file is extra.
  24. /// </summary>
  25. /// <param name="path">Path to file.</param>
  26. /// <returns>Returns <see cref="ExtraResult"/> object.</returns>
  27. public ExtraResult GetExtraInfo(string path)
  28. {
  29. var result = new ExtraResult();
  30. for (var i = 0; i < _options.VideoExtraRules.Length; i++)
  31. {
  32. var rule = _options.VideoExtraRules[i];
  33. if (rule.MediaType == MediaType.Audio)
  34. {
  35. if (!AudioFileParser.IsAudioFile(path, _options))
  36. {
  37. continue;
  38. }
  39. }
  40. else if (rule.MediaType == MediaType.Video)
  41. {
  42. if (!VideoResolver.IsVideoFile(path, _options))
  43. {
  44. continue;
  45. }
  46. }
  47. var pathSpan = path.AsSpan();
  48. if (rule.RuleType == ExtraRuleType.Filename)
  49. {
  50. var filename = Path.GetFileNameWithoutExtension(pathSpan);
  51. if (filename.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
  52. {
  53. result.ExtraType = rule.ExtraType;
  54. result.Rule = rule;
  55. }
  56. }
  57. else if (rule.RuleType == ExtraRuleType.Suffix)
  58. {
  59. var filename = Path.GetFileNameWithoutExtension(pathSpan);
  60. if (filename.Contains(rule.Token, StringComparison.OrdinalIgnoreCase))
  61. {
  62. result.ExtraType = rule.ExtraType;
  63. result.Rule = rule;
  64. }
  65. }
  66. else if (rule.RuleType == ExtraRuleType.Regex)
  67. {
  68. var filename = Path.GetFileName(path);
  69. var regex = new Regex(rule.Token, RegexOptions.IgnoreCase);
  70. if (regex.IsMatch(filename))
  71. {
  72. result.ExtraType = rule.ExtraType;
  73. result.Rule = rule;
  74. }
  75. }
  76. else if (rule.RuleType == ExtraRuleType.DirectoryName)
  77. {
  78. var directoryName = Path.GetFileName(Path.GetDirectoryName(pathSpan));
  79. if (directoryName.Equals(rule.Token, StringComparison.OrdinalIgnoreCase))
  80. {
  81. result.ExtraType = rule.ExtraType;
  82. result.Rule = rule;
  83. }
  84. }
  85. if (result.ExtraType != null)
  86. {
  87. return result;
  88. }
  89. }
  90. return result;
  91. }
  92. }
  93. }