SeriesResolver.cs 7.9 KB

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