VideoResolver.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /// <returns>VideoFileInfo.</returns>
  20. public static VideoFileInfo? ResolveDirectory(string? path, NamingOptions namingOptions, bool parseName = true)
  21. {
  22. return Resolve(path, true, namingOptions, parseName);
  23. }
  24. /// <summary>
  25. /// Resolves the file.
  26. /// </summary>
  27. /// <param name="path">The path.</param>
  28. /// <param name="namingOptions">The naming options.</param>
  29. /// <returns>VideoFileInfo.</returns>
  30. public static VideoFileInfo? ResolveFile(string? path, NamingOptions namingOptions)
  31. {
  32. return Resolve(path, false, namingOptions);
  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="namingOptions">The naming options.</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 static VideoFileInfo? Resolve(string? path, bool isDirectory, NamingOptions namingOptions, bool parseName = true)
  44. {
  45. if (string.IsNullOrEmpty(path))
  46. {
  47. return null;
  48. }
  49. bool isStub = false;
  50. ReadOnlySpan<char> container = ReadOnlySpan<char>.Empty;
  51. string? stubType = null;
  52. if (!isDirectory)
  53. {
  54. var extension = Path.GetExtension(path.AsSpan());
  55. // Check supported extensions
  56. if (!namingOptions.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  57. {
  58. // It's not supported. Check stub extensions
  59. if (!StubResolver.TryResolveFile(path, namingOptions, out stubType))
  60. {
  61. return null;
  62. }
  63. isStub = true;
  64. }
  65. container = extension.TrimStart('.');
  66. }
  67. var format3DResult = Format3DParser.Parse(path, namingOptions);
  68. var extraResult = ExtraRuleResolver.GetExtraInfo(path, namingOptions);
  69. var name = Path.GetFileNameWithoutExtension(path);
  70. int? year = null;
  71. if (parseName)
  72. {
  73. var cleanDateTimeResult = CleanDateTime(name, namingOptions);
  74. name = cleanDateTimeResult.Name;
  75. year = cleanDateTimeResult.Year;
  76. if (extraResult.ExtraType == null
  77. && TryCleanString(name, namingOptions, out var newName))
  78. {
  79. name = newName;
  80. }
  81. }
  82. return new VideoFileInfo(
  83. path: path,
  84. container: container.IsEmpty ? null : container.ToString(),
  85. isStub: isStub,
  86. name: name,
  87. year: year,
  88. stubType: stubType,
  89. is3D: format3DResult.Is3D,
  90. format3D: format3DResult.Format3D,
  91. extraType: extraResult.ExtraType,
  92. isDirectory: isDirectory,
  93. extraRule: extraResult.Rule);
  94. }
  95. /// <summary>
  96. /// Determines if path is video file based on extension.
  97. /// </summary>
  98. /// <param name="path">Path to file.</param>
  99. /// <param name="namingOptions">The naming options.</param>
  100. /// <returns>True if is video file.</returns>
  101. public static bool IsVideoFile(string path, NamingOptions namingOptions)
  102. {
  103. var extension = Path.GetExtension(path.AsSpan());
  104. return namingOptions.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
  105. }
  106. /// <summary>
  107. /// Determines if path is video file stub based on extension.
  108. /// </summary>
  109. /// <param name="path">Path to file.</param>
  110. /// <param name="namingOptions">The naming options.</param>
  111. /// <returns>True if is video file stub.</returns>
  112. public static bool IsStubFile(string path, NamingOptions namingOptions)
  113. {
  114. var extension = Path.GetExtension(path.AsSpan());
  115. return namingOptions.StubFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
  116. }
  117. /// <summary>
  118. /// Tries to clean name of clutter.
  119. /// </summary>
  120. /// <param name="name">Raw name.</param>
  121. /// <param name="namingOptions">The naming options.</param>
  122. /// <param name="newName">Clean name.</param>
  123. /// <returns>True if cleaning of name was successful.</returns>
  124. public static bool TryCleanString([NotNullWhen(true)] string? name, NamingOptions namingOptions, out string newName)
  125. {
  126. return CleanStringParser.TryClean(name, namingOptions.CleanStringRegexes, out newName);
  127. }
  128. /// <summary>
  129. /// Tries to get name and year from raw name.
  130. /// </summary>
  131. /// <param name="name">Raw name.</param>
  132. /// <param name="namingOptions">The naming options.</param>
  133. /// <returns>Returns <see cref="CleanDateTimeResult"/> with name and optional year.</returns>
  134. public static CleanDateTimeResult CleanDateTime(string name, NamingOptions namingOptions)
  135. {
  136. return CleanDateTimeParser.Clean(name, namingOptions.CleanDateTimeRegexes);
  137. }
  138. }
  139. }