SeriesResolver.cs 8.9 KB

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