VideoResolver.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.IO;
  4. using Emby.Naming.Common;
  5. using MediaBrowser.Common.Extensions;
  6. namespace Emby.Naming.Video
  7. {
  8. /// <summary>
  9. /// Resolves <see cref="VideoFileInfo"/> from file path.
  10. /// </summary>
  11. public class VideoResolver
  12. {
  13. private readonly NamingOptions _options;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="VideoResolver"/> class.
  16. /// </summary>
  17. /// <param name="options"><see cref="NamingOptions"/> object containing VideoFileExtensions, StubFileExtensions, CleanStringRegexes and CleanDateTimeRegexes
  18. /// and passes options in <see cref="StubResolver"/>, <see cref="FlagParser"/>, <see cref="Format3DParser"/> and <see cref="ExtraResolver"/>.</param>
  19. public VideoResolver(NamingOptions options)
  20. {
  21. _options = options;
  22. }
  23. /// <summary>
  24. /// Resolves the directory.
  25. /// </summary>
  26. /// <param name="path">The path.</param>
  27. /// <returns>VideoFileInfo.</returns>
  28. public VideoFileInfo? ResolveDirectory(string? path)
  29. {
  30. return Resolve(path, true);
  31. }
  32. /// <summary>
  33. /// Resolves the file.
  34. /// </summary>
  35. /// <param name="path">The path.</param>
  36. /// <returns>VideoFileInfo.</returns>
  37. public VideoFileInfo? ResolveFile(string? path)
  38. {
  39. return Resolve(path, false);
  40. }
  41. /// <summary>
  42. /// Resolves the specified path.
  43. /// </summary>
  44. /// <param name="path">The path.</param>
  45. /// <param name="isDirectory">if set to <c>true</c> [is folder].</param>
  46. /// <param name="parseName">Whether or not the name should be parsed for info.</param>
  47. /// <returns>VideoFileInfo.</returns>
  48. /// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
  49. public VideoFileInfo? Resolve(string? path, bool isDirectory, bool parseName = true)
  50. {
  51. if (string.IsNullOrEmpty(path))
  52. {
  53. return null;
  54. }
  55. bool isStub = false;
  56. ReadOnlySpan<char> container = ReadOnlySpan<char>.Empty;
  57. string? stubType = null;
  58. if (!isDirectory)
  59. {
  60. var extension = Path.GetExtension(path.AsSpan());
  61. // Check supported extensions
  62. if (!_options.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  63. {
  64. // It's not supported. Check stub extensions
  65. if (!StubResolver.TryResolveFile(path, _options, out stubType))
  66. {
  67. return null;
  68. }
  69. isStub = true;
  70. }
  71. container = extension.TrimStart('.');
  72. }
  73. var flags = new FlagParser(_options).GetFlags(path);
  74. var format3DResult = new Format3DParser(_options).Parse(flags);
  75. var extraResult = new ExtraResolver(_options).GetExtraInfo(path);
  76. var name = Path.GetFileNameWithoutExtension(path);
  77. int? year = null;
  78. if (parseName)
  79. {
  80. var cleanDateTimeResult = CleanDateTime(name);
  81. name = cleanDateTimeResult.Name;
  82. year = cleanDateTimeResult.Year;
  83. if (extraResult.ExtraType == null
  84. && TryCleanString(name, out ReadOnlySpan<char> newName))
  85. {
  86. name = newName.ToString();
  87. }
  88. }
  89. return new VideoFileInfo(
  90. path: path,
  91. container: container.IsEmpty ? null : container.ToString(),
  92. isStub: isStub,
  93. name: name,
  94. year: year,
  95. stubType: stubType,
  96. is3D: format3DResult.Is3D,
  97. format3D: format3DResult.Format3D,
  98. extraType: extraResult.ExtraType,
  99. isDirectory: isDirectory,
  100. extraRule: extraResult.Rule);
  101. }
  102. /// <summary>
  103. /// Determines if path is video file based on extension.
  104. /// </summary>
  105. /// <param name="path">Path to file.</param>
  106. /// <returns>True if is video file.</returns>
  107. public bool IsVideoFile(string path)
  108. {
  109. var extension = Path.GetExtension(path.AsSpan());
  110. return _options.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
  111. }
  112. /// <summary>
  113. /// Determines if path is video file stub based on extension.
  114. /// </summary>
  115. /// <param name="path">Path to file.</param>
  116. /// <returns>True if is video file stub.</returns>
  117. public bool IsStubFile(string path)
  118. {
  119. var extension = Path.GetExtension(path.AsSpan());
  120. return _options.StubFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
  121. }
  122. /// <summary>
  123. /// Tries to clean name of clutter.
  124. /// </summary>
  125. /// <param name="name">Raw name.</param>
  126. /// <param name="newName">Clean name.</param>
  127. /// <returns>True if cleaning of name was successful.</returns>
  128. public bool TryCleanString([NotNullWhen(true)] string? name, out ReadOnlySpan<char> newName)
  129. {
  130. return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
  131. }
  132. /// <summary>
  133. /// Tries to get name and year from raw name.
  134. /// </summary>
  135. /// <param name="name">Raw name.</param>
  136. /// <returns>Returns <see cref="CleanDateTimeResult"/> with name and optional year.</returns>
  137. public CleanDateTimeResult CleanDateTime(string name)
  138. {
  139. return CleanDateTimeParser.Clean(name, _options.CleanDateTimeRegexes);
  140. }
  141. }
  142. }