VideoResolver.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. #nullable enable
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using Emby.Naming.Common;
  8. namespace Emby.Naming.Video
  9. {
  10. public class VideoResolver
  11. {
  12. private readonly NamingOptions _options;
  13. public VideoResolver(NamingOptions options)
  14. {
  15. _options = options;
  16. }
  17. /// <summary>
  18. /// Resolves the directory.
  19. /// </summary>
  20. /// <param name="path">The path.</param>
  21. /// <returns>VideoFileInfo.</returns>
  22. public VideoFileInfo? ResolveDirectory(string path)
  23. {
  24. return Resolve(path, true);
  25. }
  26. /// <summary>
  27. /// Resolves the file.
  28. /// </summary>
  29. /// <param name="path">The path.</param>
  30. /// <returns>VideoFileInfo.</returns>
  31. public VideoFileInfo? ResolveFile(string path)
  32. {
  33. return Resolve(path, false);
  34. }
  35. /// <summary>
  36. /// Resolves the specified path.
  37. /// </summary>
  38. /// <param name="path">The path.</param>
  39. /// <param name="isDirectory">if set to <c>true</c> [is folder].</param>
  40. /// <param name="parseName">Whether or not the name should be parsed for info.</param>
  41. /// <returns>VideoFileInfo.</returns>
  42. /// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
  43. public VideoFileInfo? Resolve(string path, bool isDirectory, bool parseName = true)
  44. {
  45. if (string.IsNullOrEmpty(path))
  46. {
  47. throw new ArgumentNullException(nameof(path));
  48. }
  49. bool isStub = false;
  50. string? container = null;
  51. string? stubType = null;
  52. if (!isDirectory)
  53. {
  54. var extension = Path.GetExtension(path);
  55. // Check supported extensions
  56. if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  57. {
  58. // It's not supported. Check stub extensions
  59. if (!StubResolver.TryResolveFile(path, _options, out stubType))
  60. {
  61. return null;
  62. }
  63. isStub = true;
  64. }
  65. container = extension.TrimStart('.');
  66. }
  67. var flags = new FlagParser(_options).GetFlags(path);
  68. var format3DResult = new Format3DParser(_options).Parse(flags);
  69. var extraResult = new ExtraResolver(_options).GetExtraInfo(path);
  70. var name = isDirectory
  71. ? Path.GetFileName(path)
  72. : Path.GetFileNameWithoutExtension(path);
  73. int? year = null;
  74. if (parseName)
  75. {
  76. var cleanDateTimeResult = CleanDateTime(name);
  77. if (extraResult.ExtraType == null
  78. && TryCleanString(cleanDateTimeResult.Name, out ReadOnlySpan<char> newName))
  79. {
  80. name = newName.ToString();
  81. }
  82. year = cleanDateTimeResult.Year;
  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. }