SeriesResolver.cs 8.3 KB

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