EpisodeResolver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.IO;
  3. using Emby.Naming.Common;
  4. using Emby.Naming.Video;
  5. using Jellyfin.Extensions;
  6. namespace Emby.Naming.TV
  7. {
  8. /// <summary>
  9. /// Used to resolve information about episode from path.
  10. /// </summary>
  11. public class EpisodeResolver
  12. {
  13. private readonly NamingOptions _options;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="EpisodeResolver"/> class.
  16. /// </summary>
  17. /// <param name="options"><see cref="NamingOptions"/> object containing VideoFileExtensions and passed to <see cref="StubResolver"/>, <see cref="Format3DParser"/> and <see cref="EpisodePathParser"/>.</param>
  18. public EpisodeResolver(NamingOptions options)
  19. {
  20. _options = options;
  21. }
  22. /// <summary>
  23. /// Resolve information about episode from path.
  24. /// </summary>
  25. /// <param name="path">Path.</param>
  26. /// <param name="isDirectory">Is path for a directory or file.</param>
  27. /// <param name="isNamed">Do we want to use IsNamed expressions.</param>
  28. /// <param name="isOptimistic">Do we want to use Optimistic expressions.</param>
  29. /// <param name="supportsAbsoluteNumbers">Do we want to use expressions supporting absolute episode numbers.</param>
  30. /// <param name="fillExtendedInfo">Should we attempt to retrieve extended information.</param>
  31. /// <returns>Returns null or <see cref="EpisodeInfo"/> object if successful.</returns>
  32. public EpisodeInfo? Resolve(
  33. string path,
  34. bool isDirectory,
  35. bool? isNamed = null,
  36. bool? isOptimistic = null,
  37. bool? supportsAbsoluteNumbers = null,
  38. bool fillExtendedInfo = true)
  39. {
  40. bool isStub = false;
  41. string? container = null;
  42. string? stubType = null;
  43. if (!isDirectory)
  44. {
  45. var extension = Path.GetExtension(path);
  46. // Check supported extensions
  47. if (!_options.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
  48. {
  49. // It's not supported. Check stub extensions
  50. if (!StubResolver.TryResolveFile(path, _options, out stubType))
  51. {
  52. return null;
  53. }
  54. isStub = true;
  55. }
  56. container = extension.TrimStart('.');
  57. }
  58. var format3DResult = Format3DParser.Parse(path, _options);
  59. var parsingResult = new EpisodePathParser(_options)
  60. .Parse(path, isDirectory, isNamed, isOptimistic, supportsAbsoluteNumbers, fillExtendedInfo);
  61. if (!parsingResult.Success && !isStub)
  62. {
  63. return null;
  64. }
  65. return new EpisodeInfo(path)
  66. {
  67. Container = container,
  68. IsStub = isStub,
  69. EndingEpisodeNumber = parsingResult.EndingEpisodeNumber,
  70. EpisodeNumber = parsingResult.EpisodeNumber,
  71. SeasonNumber = parsingResult.SeasonNumber,
  72. SeriesName = parsingResult.SeriesName,
  73. StubType = stubType,
  74. Is3D = format3DResult.Is3D,
  75. Format3D = format3DResult.Format3D,
  76. IsByDate = parsingResult.IsByDate,
  77. Day = parsingResult.Day,
  78. Month = parsingResult.Month,
  79. Year = parsingResult.Year
  80. };
  81. }
  82. }
  83. }