SeriesResolver.cs 7.4 KB

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