MovieResolver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Resolvers;
  7. using MediaBrowser.Model.Entities;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies
  13. {
  14. /// <summary>
  15. /// Class MovieResolver
  16. /// </summary>
  17. public class MovieResolver : BaseVideoResolver<Video>
  18. {
  19. private readonly IServerApplicationPaths _applicationPaths;
  20. private readonly ILibraryManager _libraryManager;
  21. public MovieResolver(IServerApplicationPaths appPaths, ILibraryManager libraryManager)
  22. {
  23. _applicationPaths = appPaths;
  24. _libraryManager = libraryManager;
  25. }
  26. /// <summary>
  27. /// Gets the priority.
  28. /// </summary>
  29. /// <value>The priority.</value>
  30. public override ResolverPriority Priority
  31. {
  32. get
  33. {
  34. // Give plugins a chance to catch iso's first
  35. // Also since we have to loop through child files looking for videos,
  36. // see if we can avoid some of that by letting other resolvers claim folders first
  37. return ResolverPriority.Second;
  38. }
  39. }
  40. /// <summary>
  41. /// Resolves the specified args.
  42. /// </summary>
  43. /// <param name="args">The args.</param>
  44. /// <returns>Video.</returns>
  45. protected override Video Resolve(ItemResolveArgs args)
  46. {
  47. // Avoid expensive tests against VF's and all their children by not allowing this
  48. if (args.Parent != null)
  49. {
  50. if (args.Parent.IsRoot)
  51. {
  52. return null;
  53. }
  54. // If the parent is not a boxset, the only other allowed parent type is Folder
  55. if (!(args.Parent is BoxSet))
  56. {
  57. if (args.Parent.GetType() != typeof(Folder))
  58. {
  59. return null;
  60. }
  61. }
  62. }
  63. // Since the looping is expensive, this is an optimization to help us avoid it
  64. if (args.Path.IndexOf("[tvdbid", StringComparison.OrdinalIgnoreCase) != -1)
  65. {
  66. return null;
  67. }
  68. var isDirectory = args.IsDirectory;
  69. if (isDirectory)
  70. {
  71. // Since the looping is expensive, this is an optimization to help us avoid it
  72. if (args.ContainsMetaFileByName("series.xml"))
  73. {
  74. return null;
  75. }
  76. }
  77. var collectionType = args.GetCollectionType();
  78. // Find movies with their own folders
  79. if (isDirectory)
  80. {
  81. if (args.Path.IndexOf("[trailers]", StringComparison.OrdinalIgnoreCase) != -1 ||
  82. string.Equals(collectionType, CollectionType.Trailers, StringComparison.OrdinalIgnoreCase))
  83. {
  84. return FindMovie<Trailer>(args.Path, args.FileSystemChildren);
  85. }
  86. if (args.Path.IndexOf("[musicvideos]", StringComparison.OrdinalIgnoreCase) != -1 ||
  87. string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  88. {
  89. return FindMovie<MusicVideo>(args.Path, args.FileSystemChildren);
  90. }
  91. if (args.Path.IndexOf("[adultvideos]", StringComparison.OrdinalIgnoreCase) != -1 ||
  92. string.Equals(collectionType, CollectionType.AdultVideos, StringComparison.OrdinalIgnoreCase))
  93. {
  94. return FindMovie<AdultVideo>(args.Path, args.FileSystemChildren);
  95. }
  96. if (string.IsNullOrEmpty(collectionType) ||
  97. string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) ||
  98. string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))
  99. {
  100. return FindMovie<Movie>(args.Path, args.FileSystemChildren);
  101. }
  102. return null;
  103. }
  104. // Find movies that are mixed in the same folder
  105. if (args.Path.IndexOf("[trailers]", StringComparison.OrdinalIgnoreCase) != -1 ||
  106. string.Equals(collectionType, CollectionType.Trailers, StringComparison.OrdinalIgnoreCase))
  107. {
  108. return ResolveVideo<Trailer>(args);
  109. }
  110. Video item = null;
  111. if (args.Path.IndexOf("[musicvideos]", StringComparison.OrdinalIgnoreCase) != -1 ||
  112. string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  113. {
  114. item = ResolveVideo<MusicVideo>(args);
  115. }
  116. if (args.Path.IndexOf("[adultvideos]", StringComparison.OrdinalIgnoreCase) != -1 ||
  117. string.Equals(collectionType, CollectionType.AdultVideos, StringComparison.OrdinalIgnoreCase))
  118. {
  119. item = ResolveVideo<AdultVideo>(args);
  120. }
  121. // To find a movie file, the collection type must be movies or boxsets
  122. // Otherwise we'll consider it a plain video and let the video resolver handle it
  123. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) ||
  124. string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))
  125. {
  126. item = ResolveVideo<Movie>(args);
  127. }
  128. if (item != null)
  129. {
  130. item.IsInMixedFolder = true;
  131. }
  132. return item;
  133. }
  134. /// <summary>
  135. /// Sets the initial item values.
  136. /// </summary>
  137. /// <param name="item">The item.</param>
  138. /// <param name="args">The args.</param>
  139. protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
  140. {
  141. base.SetInitialItemValues(item, args);
  142. SetProviderIdFromPath(item);
  143. }
  144. /// <summary>
  145. /// Sets the provider id from path.
  146. /// </summary>
  147. /// <param name="item">The item.</param>
  148. private void SetProviderIdFromPath(Video item)
  149. {
  150. //we need to only look at the name of this actual item (not parents)
  151. var justName = Path.GetFileName(item.Path);
  152. var id = justName.GetAttributeValue("tmdbid");
  153. if (!string.IsNullOrEmpty(id))
  154. {
  155. item.SetProviderId(MetadataProviders.Tmdb, id);
  156. }
  157. }
  158. /// <summary>
  159. /// Finds a movie based on a child file system entries
  160. /// </summary>
  161. /// <typeparam name="T"></typeparam>
  162. /// <param name="path">The path.</param>
  163. /// <param name="fileSystemEntries">The file system entries.</param>
  164. /// <returns>Movie.</returns>
  165. private T FindMovie<T>(string path, IEnumerable<FileSystemInfo> fileSystemEntries)
  166. where T : Video, new()
  167. {
  168. var movies = new List<T>();
  169. var multiDiscFolders = new List<FileSystemInfo>();
  170. // Loop through each child file/folder and see if we find a video
  171. foreach (var child in fileSystemEntries)
  172. {
  173. var filename = child.Name;
  174. if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  175. {
  176. if (IsDvdDirectory(filename))
  177. {
  178. return new T
  179. {
  180. Path = path,
  181. VideoType = VideoType.Dvd
  182. };
  183. }
  184. if (IsBluRayDirectory(filename))
  185. {
  186. return new T
  187. {
  188. Path = path,
  189. VideoType = VideoType.BluRay
  190. };
  191. }
  192. if (EntityResolutionHelper.IsMultiPartFile(filename))
  193. {
  194. multiDiscFolders.Add(child);
  195. }
  196. continue;
  197. }
  198. // Don't misidentify xbmc trailers as a movie
  199. if (filename.IndexOf(BaseItem.XbmcTrailerFileSuffix, StringComparison.OrdinalIgnoreCase) != -1)
  200. {
  201. continue;
  202. }
  203. var childArgs = new ItemResolveArgs(_applicationPaths, _libraryManager)
  204. {
  205. FileInfo = child,
  206. Path = child.FullName
  207. };
  208. var item = ResolveVideo<T>(childArgs);
  209. if (item != null)
  210. {
  211. item.IsInMixedFolder = false;
  212. movies.Add(item);
  213. }
  214. }
  215. if (movies.Count > 1)
  216. {
  217. return GetMultiFileMovie(movies);
  218. }
  219. if (movies.Count == 1)
  220. {
  221. return movies[0];
  222. }
  223. if (multiDiscFolders.Count > 0)
  224. {
  225. return GetMultiDiscMovie<T>(multiDiscFolders);
  226. }
  227. return null;
  228. }
  229. /// <summary>
  230. /// Gets the multi disc movie.
  231. /// </summary>
  232. /// <typeparam name="T"></typeparam>
  233. /// <param name="folders">The folders.</param>
  234. /// <returns>``0.</returns>
  235. private T GetMultiDiscMovie<T>(List<FileSystemInfo> folders)
  236. where T : Video, new()
  237. {
  238. var videoType = VideoType.BluRay;
  239. var folderPaths = folders.Select(i => i.FullName).Where(i =>
  240. {
  241. var subfolders = Directory.GetDirectories(i).Select(Path.GetFileName).ToList();
  242. if (subfolders.Any(IsDvdDirectory))
  243. {
  244. videoType = VideoType.Dvd;
  245. return true;
  246. }
  247. if (subfolders.Any(IsBluRayDirectory))
  248. {
  249. videoType = VideoType.BluRay;
  250. return true;
  251. }
  252. return false;
  253. }).OrderBy(i => i).ToList();
  254. if (folderPaths.Count == 0)
  255. {
  256. return null;
  257. }
  258. return new T
  259. {
  260. Path = folderPaths[0],
  261. IsMultiPart = true,
  262. VideoType = videoType
  263. };
  264. }
  265. /// <summary>
  266. /// Gets the multi file movie.
  267. /// </summary>
  268. /// <typeparam name="T"></typeparam>
  269. /// <param name="movies">The movies.</param>
  270. /// <returns>``0.</returns>
  271. private T GetMultiFileMovie<T>(List<T> movies)
  272. where T : Video, new()
  273. {
  274. var multiPartMovies = movies.OrderBy(i => i.Path)
  275. .Where(i => EntityResolutionHelper.IsMultiPartFile(i.Path))
  276. .ToList();
  277. // They must all be part of the sequence
  278. if (multiPartMovies.Count != movies.Count)
  279. {
  280. return null;
  281. }
  282. var firstPart = multiPartMovies[0];
  283. firstPart.IsMultiPart = true;
  284. return firstPart;
  285. }
  286. /// <summary>
  287. /// Determines whether [is DVD directory] [the specified directory name].
  288. /// </summary>
  289. /// <param name="directoryName">Name of the directory.</param>
  290. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  291. private bool IsDvdDirectory(string directoryName)
  292. {
  293. return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
  294. }
  295. /// <summary>
  296. /// Determines whether [is blu ray directory] [the specified directory name].
  297. /// </summary>
  298. /// <param name="directoryName">Name of the directory.</param>
  299. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  300. private bool IsBluRayDirectory(string directoryName)
  301. {
  302. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  303. }
  304. }
  305. }