MovieResolver.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Providers.Movies;
  6. using MediaBrowser.Controller.Resolvers;
  7. using MediaBrowser.Model.Entities;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies
  12. {
  13. /// <summary>
  14. /// Class MovieResolver
  15. /// </summary>
  16. public class MovieResolver : BaseVideoResolver<Movie>
  17. {
  18. /// <summary>
  19. /// Gets the priority.
  20. /// </summary>
  21. /// <value>The priority.</value>
  22. public override ResolverPriority Priority
  23. {
  24. get
  25. {
  26. // Give plugins a chance to catch iso's first
  27. // Also since we have to loop through child files looking for videos,
  28. // see if we can avoid some of that by letting other resolvers claim folders first
  29. return ResolverPriority.Second;
  30. }
  31. }
  32. /// <summary>
  33. /// Resolves the specified args.
  34. /// </summary>
  35. /// <param name="args">The args.</param>
  36. /// <returns>Movie.</returns>
  37. protected override Movie Resolve(ItemResolveArgs args)
  38. {
  39. // Must be a directory and under a 'Movies' VF
  40. if (args.IsDirectory)
  41. {
  42. // Avoid expensive tests against VF's and all their children by not allowing this
  43. if (args.Parent == null || args.Parent.IsRoot)
  44. {
  45. return null;
  46. }
  47. // If the parent is not a boxset, the only other allowed parent type is Folder
  48. if (!(args.Parent is BoxSet))
  49. {
  50. if (args.Parent.GetType() != typeof(Folder))
  51. {
  52. return null;
  53. }
  54. }
  55. // Optimization to avoid running all these tests against Top folders
  56. if (args.Parent != null && args.Parent.IsRoot)
  57. {
  58. return null;
  59. }
  60. // The movie must be a video file
  61. return FindMovie(args);
  62. }
  63. return null;
  64. }
  65. /// <summary>
  66. /// Sets the initial item values.
  67. /// </summary>
  68. /// <param name="item">The item.</param>
  69. /// <param name="args">The args.</param>
  70. protected override void SetInitialItemValues(Movie item, ItemResolveArgs args)
  71. {
  72. base.SetInitialItemValues(item, args);
  73. SetProviderIdFromPath(item);
  74. }
  75. /// <summary>
  76. /// Sets the provider id from path.
  77. /// </summary>
  78. /// <param name="item">The item.</param>
  79. private void SetProviderIdFromPath(Movie item)
  80. {
  81. //we need to only look at the name of this actual item (not parents)
  82. var justName = Path.GetFileName(item.Path);
  83. var id = justName.GetAttributeValue("tmdbid");
  84. if (!string.IsNullOrEmpty(id))
  85. {
  86. item.SetProviderId(MetadataProviders.Tmdb, id);
  87. }
  88. }
  89. /// <summary>
  90. /// Finds a movie based on a child file system entries
  91. /// </summary>
  92. /// <param name="args">The args.</param>
  93. /// <returns>Movie.</returns>
  94. private Movie FindMovie(ItemResolveArgs args)
  95. {
  96. // Since the looping is expensive, this is an optimization to help us avoid it
  97. if (args.ContainsMetaFileByName("series.xml") || args.Path.IndexOf("[tvdbid", StringComparison.OrdinalIgnoreCase) != -1)
  98. {
  99. return null;
  100. }
  101. // Optimization to avoid having to resolve every file
  102. bool? isKnownMovie = null;
  103. var movies = new List<Movie>();
  104. // Loop through each child file/folder and see if we find a video
  105. foreach (var child in args.FileSystemChildren)
  106. {
  107. if (child.IsDirectory)
  108. {
  109. if (IsDvdDirectory(child.cFileName))
  110. {
  111. return new Movie
  112. {
  113. Path = args.Path,
  114. VideoType = VideoType.Dvd
  115. };
  116. }
  117. if (IsBluRayDirectory(child.cFileName))
  118. {
  119. return new Movie
  120. {
  121. Path = args.Path,
  122. VideoType = VideoType.BluRay
  123. };
  124. }
  125. if (IsHdDvdDirectory(child.cFileName))
  126. {
  127. return new Movie
  128. {
  129. Path = args.Path,
  130. VideoType = VideoType.HdDvd
  131. };
  132. }
  133. continue;
  134. }
  135. var childArgs = new ItemResolveArgs
  136. {
  137. FileInfo = child,
  138. Path = child.Path
  139. };
  140. var item = base.Resolve(childArgs);
  141. if (item != null)
  142. {
  143. // If we already know it's a movie, we can stop looping
  144. if (!isKnownMovie.HasValue)
  145. {
  146. isKnownMovie = args.ContainsMetaFileByName("movie.xml") || args.ContainsMetaFileByName(MovieDbProvider.LOCAL_META_FILE_NAME) || args.Path.IndexOf("[tmdbid", StringComparison.OrdinalIgnoreCase) != -1;
  147. }
  148. if (isKnownMovie.Value)
  149. {
  150. return item;
  151. }
  152. movies.Add(item);
  153. }
  154. }
  155. // If there are multiple video files, return null, and let the VideoResolver catch them later as plain videos
  156. return movies.Count == 1 ? movies[0] : null;
  157. }
  158. /// <summary>
  159. /// Determines whether [is DVD directory] [the specified directory name].
  160. /// </summary>
  161. /// <param name="directoryName">Name of the directory.</param>
  162. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  163. private bool IsDvdDirectory(string directoryName)
  164. {
  165. return directoryName.Equals("video_ts", StringComparison.OrdinalIgnoreCase);
  166. }
  167. /// <summary>
  168. /// Determines whether [is hd DVD directory] [the specified directory name].
  169. /// </summary>
  170. /// <param name="directoryName">Name of the directory.</param>
  171. /// <returns><c>true</c> if [is hd DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  172. private bool IsHdDvdDirectory(string directoryName)
  173. {
  174. return directoryName.Equals("hvdvd_ts", StringComparison.OrdinalIgnoreCase);
  175. }
  176. /// <summary>
  177. /// Determines whether [is blu ray directory] [the specified directory name].
  178. /// </summary>
  179. /// <param name="directoryName">Name of the directory.</param>
  180. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  181. private bool IsBluRayDirectory(string directoryName)
  182. {
  183. return directoryName.Equals("bdmv", StringComparison.OrdinalIgnoreCase);
  184. }
  185. }
  186. }