SeriesResolver.cs 8.2 KB

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