VideoResolver.cs 6.4 KB

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