VideoResolver.cs 6.0 KB

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