VideoResolver.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /// <param name="parseName">Whether or not the name should be parsed for info</param>
  38. /// <returns>VideoFileInfo.</returns>
  39. /// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
  40. public VideoFileInfo Resolve(string path, bool isDirectory, bool parseName = true)
  41. {
  42. if (string.IsNullOrEmpty(path))
  43. {
  44. throw new ArgumentNullException(nameof(path));
  45. }
  46. bool isStub = false;
  47. string container = null;
  48. string stubType = null;
  49. if (!isDirectory)
  50. {
  51. var extension = Path.GetExtension(path);
  52. // Check supported extensions
  53. if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  54. {
  55. var stubResult = StubResolver.ResolveFile(path, _options);
  56. isStub = stubResult.IsStub;
  57. // It's not supported. Check stub extensions
  58. if (!isStub)
  59. {
  60. return null;
  61. }
  62. stubType = stubResult.StubType;
  63. }
  64. container = extension.TrimStart('.');
  65. }
  66. var flags = new FlagParser(_options).GetFlags(path);
  67. var format3DResult = new Format3DParser(_options).Parse(flags);
  68. var extraResult = new ExtraResolver(_options).GetExtraInfo(path);
  69. var name = isDirectory
  70. ? Path.GetFileName(path)
  71. : Path.GetFileNameWithoutExtension(path);
  72. int? year = null;
  73. if (parseName)
  74. {
  75. var cleanDateTimeResult = CleanDateTime(name);
  76. if (string.IsNullOrEmpty(extraResult.ExtraType))
  77. {
  78. name = CleanString(cleanDateTimeResult.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. }