VideoResolver.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Emby.Naming.Common;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Emby.Naming.Video
  6. {
  7. public class VideoResolver
  8. {
  9. private readonly NamingOptions _options;
  10. public VideoResolver(NamingOptions options)
  11. {
  12. _options = options;
  13. }
  14. /// <summary>
  15. /// Resolves the directory.
  16. /// </summary>
  17. /// <param name="path">The path.</param>
  18. /// <returns>VideoFileInfo.</returns>
  19. public VideoFileInfo ResolveDirectory(string path)
  20. {
  21. return Resolve(path, true);
  22. }
  23. /// <summary>
  24. /// Resolves the file.
  25. /// </summary>
  26. /// <param name="path">The path.</param>
  27. /// <returns>VideoFileInfo.</returns>
  28. public VideoFileInfo ResolveFile(string path)
  29. {
  30. return Resolve(path, false);
  31. }
  32. /// <summary>
  33. /// Resolves the specified path.
  34. /// </summary>
  35. /// <param name="path">The path.</param>
  36. /// <param name="IsDirectory">if set to <c>true</c> [is folder].</param>
  37. /// <returns>VideoFileInfo.</returns>
  38. /// <exception cref="System.ArgumentNullException">path</exception>
  39. public VideoFileInfo Resolve(string path, bool IsDirectory, bool parseName = true)
  40. {
  41. if (string.IsNullOrEmpty(path))
  42. {
  43. throw new ArgumentNullException("path");
  44. }
  45. var isStub = false;
  46. string container = null;
  47. string stubType = null;
  48. if (!IsDirectory)
  49. {
  50. var extension = Path.GetExtension(path) ?? string.Empty;
  51. // Check supported extensions
  52. if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  53. {
  54. var stubResult = new StubResolver(_options).ResolveFile(path);
  55. isStub = stubResult.IsStub;
  56. // It's not supported. Check stub extensions
  57. if (!isStub)
  58. {
  59. return null;
  60. }
  61. stubType = stubResult.StubType;
  62. }
  63. container = extension.TrimStart('.');
  64. }
  65. var flags = new FlagParser(_options).GetFlags(path);
  66. var format3DResult = new Format3DParser(_options).Parse(flags);
  67. var extraResult = new ExtraResolver(_options).GetExtraInfo(path);
  68. var name = !IsDirectory
  69. ? Path.GetFileNameWithoutExtension(path)
  70. : Path.GetFileName(path);
  71. int? year = null;
  72. if (parseName)
  73. {
  74. var cleanDateTimeResult = CleanDateTime(name);
  75. if (string.IsNullOrEmpty(extraResult.ExtraType))
  76. {
  77. name = cleanDateTimeResult.Name;
  78. name = CleanString(name).Name;
  79. }
  80. year = cleanDateTimeResult.Year;
  81. }
  82. return new VideoFileInfo
  83. {
  84. Path = path,
  85. Container = container,
  86. IsStub = isStub,
  87. Name = name,
  88. Year = year,
  89. StubType = stubType,
  90. Is3D = format3DResult.Is3D,
  91. Format3D = format3DResult.Format3D,
  92. ExtraType = extraResult.ExtraType,
  93. IsDirectory = IsDirectory,
  94. ExtraRule = extraResult.Rule
  95. };
  96. }
  97. public bool IsVideoFile(string path)
  98. {
  99. var extension = Path.GetExtension(path) ?? string.Empty;
  100. return _options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  101. }
  102. public bool IsStubFile(string path)
  103. {
  104. var extension = Path.GetExtension(path) ?? string.Empty;
  105. return _options.StubFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  106. }
  107. public CleanStringResult CleanString(string name)
  108. {
  109. return new CleanStringParser().Clean(name, _options.CleanStringRegexes);
  110. }
  111. public CleanDateTimeResult CleanDateTime(string name)
  112. {
  113. return new CleanDateTimeParser(_options).Clean(name);
  114. }
  115. }
  116. }