ExtraResolver.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using Emby.Naming.Audio;
  6. using Emby.Naming.Common;
  7. namespace Emby.Naming.Video
  8. {
  9. /// <summary>
  10. /// Resolve if file is extra for video.
  11. /// </summary>
  12. public class ExtraResolver
  13. {
  14. private readonly NamingOptions _options;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="ExtraResolver"/> class.
  17. /// </summary>
  18. /// <param name="options"><see cref="NamingOptions"/> object containing VideoExtraRules and passed to <see cref="AudioFileParser"/> and <see cref="VideoResolver"/>.</param>
  19. public ExtraResolver(NamingOptions options)
  20. {
  21. _options = options;
  22. }
  23. /// <summary>
  24. /// Attempts to resolve if file is extra.
  25. /// </summary>
  26. /// <param name="path">Path to file.</param>
  27. /// <returns>Returns <see cref="ExtraResult"/> object.</returns>
  28. public ExtraResult GetExtraInfo(string path)
  29. {
  30. return _options.VideoExtraRules
  31. .Select(i => GetExtraInfo(path, i))
  32. .FirstOrDefault(i => i.ExtraType != null) ?? new ExtraResult();
  33. }
  34. private ExtraResult GetExtraInfo(string path, ExtraRule rule)
  35. {
  36. var result = new ExtraResult();
  37. if (rule.MediaType == MediaType.Audio)
  38. {
  39. if (!AudioFileParser.IsAudioFile(path, _options))
  40. {
  41. return result;
  42. }
  43. }
  44. else if (rule.MediaType == MediaType.Video)
  45. {
  46. if (!new VideoResolver(_options).IsVideoFile(path))
  47. {
  48. return result;
  49. }
  50. }
  51. if (rule.RuleType == ExtraRuleType.Filename)
  52. {
  53. var filename = Path.GetFileNameWithoutExtension(path);
  54. if (string.Equals(filename, rule.Token, StringComparison.OrdinalIgnoreCase))
  55. {
  56. result.ExtraType = rule.ExtraType;
  57. result.Rule = rule;
  58. }
  59. }
  60. else if (rule.RuleType == ExtraRuleType.Suffix)
  61. {
  62. var filename = Path.GetFileNameWithoutExtension(path);
  63. if (filename.IndexOf(rule.Token, StringComparison.OrdinalIgnoreCase) > 0)
  64. {
  65. result.ExtraType = rule.ExtraType;
  66. result.Rule = rule;
  67. }
  68. }
  69. else if (rule.RuleType == ExtraRuleType.Regex)
  70. {
  71. var filename = Path.GetFileName(path);
  72. var regex = new Regex(rule.Token, RegexOptions.IgnoreCase);
  73. if (regex.IsMatch(filename))
  74. {
  75. result.ExtraType = rule.ExtraType;
  76. result.Rule = rule;
  77. }
  78. }
  79. else if (rule.RuleType == ExtraRuleType.DirectoryName)
  80. {
  81. var directoryName = Path.GetFileName(Path.GetDirectoryName(path));
  82. if (string.Equals(directoryName, rule.Token, StringComparison.OrdinalIgnoreCase))
  83. {
  84. result.ExtraType = rule.ExtraType;
  85. result.Rule = rule;
  86. }
  87. }
  88. return result;
  89. }
  90. }
  91. }