ExtraResolver.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using Emby.Naming.Audio;
  7. using Emby.Naming.Common;
  8. namespace Emby.Naming.Video
  9. {
  10. public class ExtraResolver
  11. {
  12. private readonly NamingOptions _options;
  13. public ExtraResolver(NamingOptions options)
  14. {
  15. _options = options;
  16. }
  17. public ExtraResult GetExtraInfo(string path)
  18. {
  19. return _options.VideoExtraRules
  20. .Select(i => GetExtraInfo(path, i))
  21. .FirstOrDefault(i => i.ExtraType != null) ?? new ExtraResult();
  22. }
  23. private ExtraResult GetExtraInfo(string path, ExtraRule rule)
  24. {
  25. var result = new ExtraResult();
  26. if (rule.MediaType == MediaType.Audio)
  27. {
  28. if (!AudioFileParser.IsAudioFile(path, _options))
  29. {
  30. return result;
  31. }
  32. }
  33. else if (rule.MediaType == MediaType.Video)
  34. {
  35. if (!new VideoResolver(_options).IsVideoFile(path))
  36. {
  37. return result;
  38. }
  39. }
  40. else
  41. {
  42. // Currently unreachable code if new rule.MediaType is desired add if clause with proper tests
  43. throw new InvalidOperationException();
  44. }
  45. if (rule.RuleType == ExtraRuleType.Filename)
  46. {
  47. var filename = Path.GetFileNameWithoutExtension(path);
  48. if (string.Equals(filename, rule.Token, StringComparison.OrdinalIgnoreCase))
  49. {
  50. result.ExtraType = rule.ExtraType;
  51. result.Rule = rule;
  52. }
  53. }
  54. else if (rule.RuleType == ExtraRuleType.Suffix)
  55. {
  56. var filename = Path.GetFileNameWithoutExtension(path);
  57. if (filename.IndexOf(rule.Token, StringComparison.OrdinalIgnoreCase) > 0)
  58. {
  59. result.ExtraType = rule.ExtraType;
  60. result.Rule = rule;
  61. }
  62. }
  63. else if (rule.RuleType == ExtraRuleType.Regex)
  64. {
  65. // Currently unreachable code if new rule.MediaType is desired add if clause with proper tests
  66. throw new InvalidOperationException();
  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. }
  77. else if (rule.RuleType == ExtraRuleType.DirectoryName)
  78. {
  79. var directoryName = Path.GetFileName(Path.GetDirectoryName(path));
  80. if (string.Equals(directoryName, rule.Token, StringComparison.OrdinalIgnoreCase))
  81. {
  82. result.ExtraType = rule.ExtraType;
  83. result.Rule = rule;
  84. }
  85. }
  86. return result;
  87. }
  88. }
  89. }