SeriesResolver.cs 9.1 KB

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