SeriesResolver.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 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, args.GetLibraryOptions(), 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. LibraryOptions libraryOptions,
  114. bool isTvContentType)
  115. {
  116. foreach (var child in fileSystemChildren)
  117. {
  118. //if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  119. //{
  120. // //logger.LogDebug("Igoring series file or folder marked hidden: {0}", child.FullName);
  121. // continue;
  122. //}
  123. // Can't enforce this because files saved by Bitcasa are always marked System
  124. //if ((attributes & FileAttributes.System) == FileAttributes.System)
  125. //{
  126. // logger.LogDebug("Igoring series subfolder marked system: {0}", child.FullName);
  127. // continue;
  128. //}
  129. if (child.IsDirectory)
  130. {
  131. if (IsSeasonFolder(child.FullName, isTvContentType, libraryManager))
  132. {
  133. logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
  134. return true;
  135. }
  136. }
  137. else
  138. {
  139. string fullName = child.FullName;
  140. if (libraryManager.IsVideoFile(fullName, libraryOptions))
  141. {
  142. if (isTvContentType)
  143. {
  144. return true;
  145. }
  146. var namingOptions = ((LibraryManager)libraryManager).GetNamingOptions();
  147. var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions);
  148. var episodeInfo = episodeResolver.Resolve(fullName, false, true, false, fillExtendedInfo: false);
  149. if (episodeInfo != null && episodeInfo.EpisodeNumber.HasValue)
  150. {
  151. return true;
  152. }
  153. }
  154. }
  155. }
  156. logger.LogDebug("{Path} is not a series folder.", path);
  157. return false;
  158. }
  159. /// <summary>
  160. /// Determines whether [is place holder] [the specified path].
  161. /// </summary>
  162. /// <param name="path">The path.</param>
  163. /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
  164. /// <exception cref="ArgumentNullException">path</exception>
  165. private static bool IsVideoPlaceHolder(string path)
  166. {
  167. if (string.IsNullOrEmpty(path))
  168. {
  169. throw new ArgumentNullException(nameof(path));
  170. }
  171. var extension = Path.GetExtension(path);
  172. return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
  173. }
  174. /// <summary>
  175. /// Determines whether [is season folder] [the specified path].
  176. /// </summary>
  177. /// <param name="path">The path.</param>
  178. /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
  179. /// <param name="libraryManager">The library manager.</param>
  180. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  181. private static bool IsSeasonFolder(string path, bool isTvContentType, ILibraryManager libraryManager)
  182. {
  183. var seasonNumber = new SeasonPathParser().Parse(path, isTvContentType, isTvContentType).SeasonNumber;
  184. return seasonNumber.HasValue;
  185. }
  186. /// <summary>
  187. /// Sets the initial item values.
  188. /// </summary>
  189. /// <param name="item">The item.</param>
  190. /// <param name="args">The args.</param>
  191. protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
  192. {
  193. base.SetInitialItemValues(item, args);
  194. SetProviderIdFromPath(item, args.Path);
  195. }
  196. /// <summary>
  197. /// Sets the provider id from path.
  198. /// </summary>
  199. /// <param name="item">The item.</param>
  200. /// <param name="path">The path.</param>
  201. private static void SetProviderIdFromPath(Series item, string path)
  202. {
  203. var justName = Path.GetFileName(path);
  204. var id = justName.GetAttributeValue("tvdbid");
  205. if (!string.IsNullOrEmpty(id))
  206. {
  207. item.SetProviderId(MetadataProviders.Tvdb, id);
  208. }
  209. }
  210. }
  211. }