VideoResolver.cs 5.7 KB

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