SeriesResolver.cs 6.8 KB

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