SeriesResolver.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Emby.Naming.TV;
  7. using MediaBrowser.Controller.Entities.TV;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Controller.Resolvers;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.IO;
  13. using Microsoft.Extensions.Logging;
  14. namespace Emby.Server.Implementations.Library.Resolvers.TV
  15. {
  16. /// <summary>
  17. /// Class SeriesResolver.
  18. /// </summary>
  19. public class SeriesResolver : FolderResolver<Series>
  20. {
  21. private readonly ILogger<SeriesResolver> _logger;
  22. private readonly ILibraryManager _libraryManager;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="SeriesResolver"/> class.
  25. /// </summary>
  26. /// <param name="logger">The logger.</param>
  27. /// <param name="libraryManager">The library manager.</param>
  28. public SeriesResolver(ILogger<SeriesResolver> logger, ILibraryManager libraryManager)
  29. {
  30. _logger = logger;
  31. _libraryManager = libraryManager;
  32. }
  33. /// <summary>
  34. /// Gets the priority.
  35. /// </summary>
  36. /// <value>The priority.</value>
  37. public override ResolverPriority Priority => ResolverPriority.Second;
  38. /// <summary>
  39. /// Resolves the specified args.
  40. /// </summary>
  41. /// <param name="args">The args.</param>
  42. /// <returns>Series.</returns>
  43. protected override Series Resolve(ItemResolveArgs args)
  44. {
  45. if (args.IsDirectory)
  46. {
  47. if (args.HasParent<Series>() || args.HasParent<Season>())
  48. {
  49. return null;
  50. }
  51. var collectionType = args.GetCollectionType();
  52. if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  53. {
  54. var configuredContentType = _libraryManager.GetConfiguredContentType(args.Path);
  55. if (!string.Equals(configuredContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  56. {
  57. return new Series
  58. {
  59. Path = args.Path,
  60. Name = Path.GetFileName(args.Path)
  61. };
  62. }
  63. }
  64. else if (string.IsNullOrEmpty(collectionType))
  65. {
  66. if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
  67. {
  68. if (args.Parent != null && args.Parent.IsRoot)
  69. {
  70. // For now, return null, but if we want to allow this in the future then add some additional checks to guard against a misplaced tvshow.nfo
  71. return null;
  72. }
  73. return new Series
  74. {
  75. Path = args.Path,
  76. Name = Path.GetFileName(args.Path)
  77. };
  78. }
  79. if (args.Parent != null && args.Parent.IsRoot)
  80. {
  81. return null;
  82. }
  83. if (IsSeriesFolder(args.Path, args.FileSystemChildren, _logger, _libraryManager, false))
  84. {
  85. return new Series
  86. {
  87. Path = args.Path,
  88. Name = Path.GetFileName(args.Path)
  89. };
  90. }
  91. }
  92. }
  93. return null;
  94. }
  95. public static bool IsSeriesFolder(
  96. string path,
  97. IEnumerable<FileSystemMetadata> fileSystemChildren,
  98. ILogger<SeriesResolver> logger,
  99. ILibraryManager libraryManager,
  100. bool isTvContentType)
  101. {
  102. foreach (var child in fileSystemChildren)
  103. {
  104. if (child.IsDirectory)
  105. {
  106. if (IsSeasonFolder(child.FullName, isTvContentType))
  107. {
  108. logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
  109. return true;
  110. }
  111. }
  112. else
  113. {
  114. string fullName = child.FullName;
  115. if (libraryManager.IsVideoFile(fullName))
  116. {
  117. if (isTvContentType)
  118. {
  119. return true;
  120. }
  121. var namingOptions = ((LibraryManager)libraryManager).GetNamingOptions();
  122. var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions);
  123. var episodeInfo = episodeResolver.Resolve(fullName, false, true, false, fillExtendedInfo: false);
  124. if (episodeInfo != null && episodeInfo.EpisodeNumber.HasValue)
  125. {
  126. return true;
  127. }
  128. }
  129. }
  130. }
  131. logger.LogDebug("{Path} is not a series folder.", path);
  132. return false;
  133. }
  134. /// <summary>
  135. /// Determines whether [is season folder] [the specified path].
  136. /// </summary>
  137. /// <param name="path">The path.</param>
  138. /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
  139. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  140. private static bool IsSeasonFolder(string path, bool isTvContentType)
  141. {
  142. var seasonNumber = SeasonPathParser.Parse(path, isTvContentType, isTvContentType).SeasonNumber;
  143. return seasonNumber.HasValue;
  144. }
  145. /// <summary>
  146. /// Sets the initial item values.
  147. /// </summary>
  148. /// <param name="item">The item.</param>
  149. /// <param name="args">The args.</param>
  150. protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
  151. {
  152. base.SetInitialItemValues(item, args);
  153. SetProviderIdFromPath(item, args.Path);
  154. }
  155. /// <summary>
  156. /// Sets the provider id from path.
  157. /// </summary>
  158. /// <param name="item">The item.</param>
  159. /// <param name="path">The path.</param>
  160. private static void SetProviderIdFromPath(Series item, string path)
  161. {
  162. var justName = Path.GetFileName(path);
  163. var id = justName.GetAttributeValue("tvdbid");
  164. if (!string.IsNullOrEmpty(id))
  165. {
  166. item.SetProviderId(MetadataProvider.Tvdb, id);
  167. }
  168. }
  169. }
  170. }