VideoResolver.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma warning disable CS1591
  2. #nullable enable
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using Emby.Naming.Common;
  7. namespace Emby.Naming.Video
  8. {
  9. public class VideoResolver
  10. {
  11. private readonly NamingOptions _options;
  12. public VideoResolver(NamingOptions options)
  13. {
  14. _options = options;
  15. }
  16. /// <summary>
  17. /// Resolves the directory.
  18. /// </summary>
  19. /// <param name="path">The path.</param>
  20. /// <returns>VideoFileInfo.</returns>
  21. public VideoFileInfo? ResolveDirectory(string path)
  22. {
  23. return Resolve(path, true);
  24. }
  25. /// <summary>
  26. /// Resolves the file.
  27. /// </summary>
  28. /// <param name="path">The path.</param>
  29. /// <returns>VideoFileInfo.</returns>
  30. public VideoFileInfo? ResolveFile(string path)
  31. {
  32. return Resolve(path, false);
  33. }
  34. /// <summary>
  35. /// Resolves the specified path.
  36. /// </summary>
  37. /// <param name="path">The path.</param>
  38. /// <param name="isDirectory">if set to <c>true</c> [is folder].</param>
  39. /// <param name="parseName">Whether or not the name should be parsed for info.</param>
  40. /// <returns>VideoFileInfo.</returns>
  41. /// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
  42. public VideoFileInfo? Resolve(string path, bool isDirectory, bool parseName = true)
  43. {
  44. if (string.IsNullOrEmpty(path))
  45. {
  46. throw new ArgumentNullException(nameof(path));
  47. }
  48. bool isStub = false;
  49. string? container = null;
  50. string? stubType = null;
  51. if (!isDirectory)
  52. {
  53. var extension = Path.GetExtension(path);
  54. // Check supported extensions
  55. if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  56. {
  57. // It's not supported. Check stub extensions
  58. if (!StubResolver.TryResolveFile(path, _options, out stubType))
  59. {
  60. return null;
  61. }
  62. isStub = true;
  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 (extraResult.ExtraType == null
  77. && TryCleanString(cleanDateTimeResult.Name, out ReadOnlySpan<char> newName))
  78. {
  79. name = newName.ToString();
  80. }
  81. year = cleanDateTimeResult.Year;
  82. }
  83. return new VideoFileInfo
  84. {
  85. Path = path,
  86. Container = container,
  87. IsStub = isStub,
  88. Name = name,
  89. Year = year,
  90. StubType = stubType,
  91. Is3D = format3DResult.Is3D,
  92. Format3D = format3DResult.Format3D,
  93. ExtraType = extraResult.ExtraType,
  94. IsDirectory = isDirectory,
  95. ExtraRule = extraResult.Rule
  96. };
  97. }
  98. public bool IsVideoFile(string path)
  99. {
  100. var extension = Path.GetExtension(path) ?? string.Empty;
  101. return _options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  102. }
  103. public bool IsStubFile(string path)
  104. {
  105. var extension = Path.GetExtension(path) ?? string.Empty;
  106. return _options.StubFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  107. }
  108. public bool TryCleanString(string name, out ReadOnlySpan<char> newName)
  109. {
  110. return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
  111. }
  112. public CleanDateTimeResult CleanDateTime(string name)
  113. {
  114. return CleanDateTimeParser.Clean(name, _options.CleanDateTimeRegexes);
  115. }
  116. }
  117. }