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. name = cleanDateTimeResult.Name;
  77. year = cleanDateTimeResult.Year;
  78. if (extraResult.ExtraType == null
  79. && TryCleanString(name, out ReadOnlySpan<char> newName))
  80. {
  81. name = newName.ToString();
  82. }
  83. }
  84. return new VideoFileInfo
  85. {
  86. Path = path,
  87. Container = container,
  88. IsStub = isStub,
  89. Name = name,
  90. Year = year,
  91. StubType = stubType,
  92. Is3D = format3DResult.Is3D,
  93. Format3D = format3DResult.Format3D,
  94. ExtraType = extraResult.ExtraType,
  95. IsDirectory = isDirectory,
  96. ExtraRule = extraResult.Rule
  97. };
  98. }
  99. public bool IsVideoFile(string path)
  100. {
  101. var extension = Path.GetExtension(path) ?? string.Empty;
  102. return _options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  103. }
  104. public bool IsStubFile(string path)
  105. {
  106. var extension = Path.GetExtension(path) ?? string.Empty;
  107. return _options.StubFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  108. }
  109. public bool TryCleanString(string name, out ReadOnlySpan<char> newName)
  110. {
  111. return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
  112. }
  113. public CleanDateTimeResult CleanDateTime(string name)
  114. {
  115. return CleanDateTimeParser.Clean(name, _options.CleanDateTimeRegexes);
  116. }
  117. }
  118. }