SeriesResolver.cs 7.4 KB

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