VideoResolver.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Emby.Naming.Common;
  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="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(nameof(path));
  44. }
  45. bool isStub = false;
  46. string container = null;
  47. string stubType = null;
  48. if (!IsDirectory)
  49. {
  50. var extension = Path.GetExtension(path);
  51. // Check supported extensions
  52. if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  53. {
  54. var stubResult = StubResolver.ResolveFile(path, _options);
  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.GetFileName(path)
  70. : Path.GetFileNameWithoutExtension(path);
  71. int? year = null;
  72. if (parseName)
  73. {
  74. var cleanDateTimeResult = CleanDateTime(name);
  75. if (string.IsNullOrEmpty(extraResult.ExtraType))
  76. {
  77. name = CleanString(cleanDateTimeResult.Name).Name;
  78. }
  79. year = cleanDateTimeResult.Year;
  80. }
  81. return new VideoFileInfo
  82. {
  83. Path = path,
  84. Container = container,
  85. IsStub = isStub,
  86. Name = name,
  87. Year = year,
  88. StubType = stubType,
  89. Is3D = format3DResult.Is3D,
  90. Format3D = format3DResult.Format3D,
  91. ExtraType = extraResult.ExtraType,
  92. IsDirectory = IsDirectory,
  93. ExtraRule = extraResult.Rule
  94. };
  95. }
  96. public bool IsVideoFile(string path)
  97. {
  98. var extension = Path.GetExtension(path) ?? string.Empty;
  99. return _options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  100. }
  101. public bool IsStubFile(string path)
  102. {
  103. var extension = Path.GetExtension(path) ?? string.Empty;
  104. return _options.StubFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  105. }
  106. public CleanStringResult CleanString(string name)
  107. {
  108. return new CleanStringParser().Clean(name, _options.CleanStringRegexes);
  109. }
  110. public CleanDateTimeResult CleanDateTime(string name)
  111. {
  112. return new CleanDateTimeParser(_options).Clean(name);
  113. }
  114. }
  115. }