SeriesResolver.cs 8.7 KB

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