MovieResolver.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  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.Entities;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Naming.Common;
  12. using MediaBrowser.Naming.IO;
  13. using MediaBrowser.Naming.Video;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. namespace MediaBrowser.Server.Implementations.Library.Resolvers.Movies
  19. {
  20. /// <summary>
  21. /// Class MovieResolver
  22. /// </summary>
  23. public class MovieResolver : BaseVideoResolver<Video>, IMultiItemResolver
  24. {
  25. private readonly IServerApplicationPaths _applicationPaths;
  26. private readonly ILogger _logger;
  27. private readonly IFileSystem _fileSystem;
  28. public MovieResolver(ILibraryManager libraryManager, IServerApplicationPaths applicationPaths, ILogger logger, IFileSystem fileSystem)
  29. : base(libraryManager)
  30. {
  31. _applicationPaths = applicationPaths;
  32. _logger = logger;
  33. _fileSystem = fileSystem;
  34. }
  35. /// <summary>
  36. /// Gets the priority.
  37. /// </summary>
  38. /// <value>The priority.</value>
  39. public override ResolverPriority Priority
  40. {
  41. get
  42. {
  43. // Give plugins a chance to catch iso's first
  44. // Also since we have to loop through child files looking for videos,
  45. // see if we can avoid some of that by letting other resolvers claim folders first
  46. // Also run after series resolver
  47. return ResolverPriority.Third;
  48. }
  49. }
  50. public MultiItemResolverResult ResolveMultiple(Folder parent,
  51. List<FileSystemInfo> files,
  52. string collectionType,
  53. IDirectoryService directoryService)
  54. {
  55. if (IsInvalid(parent, collectionType, files))
  56. {
  57. return null;
  58. }
  59. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  60. {
  61. return ResolveVideos<MusicVideo>(parent, files, directoryService, collectionType);
  62. }
  63. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  64. {
  65. return ResolveVideos<Video>(parent, files, directoryService, collectionType);
  66. }
  67. if (string.IsNullOrEmpty(collectionType))
  68. {
  69. // Owned items should just use the plain video type
  70. if (parent == null)
  71. {
  72. return ResolveVideos<Video>(parent, files, directoryService, collectionType);
  73. }
  74. return ResolveVideos<Movie>(parent, files, directoryService, collectionType);
  75. }
  76. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) ||
  77. string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))
  78. {
  79. return ResolveVideos<Movie>(parent, files, directoryService, collectionType);
  80. }
  81. return null;
  82. }
  83. private MultiItemResolverResult ResolveVideos<T>(Folder parent, IEnumerable<FileSystemInfo> fileSystemEntries, IDirectoryService directoryService, string collectionType)
  84. where T : Video, new()
  85. {
  86. var files = new List<FileSystemInfo>();
  87. var videos = new List<BaseItem>();
  88. var leftOver = new List<FileSystemInfo>();
  89. // Loop through each child file/folder and see if we find a video
  90. foreach (var child in fileSystemEntries)
  91. {
  92. if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  93. {
  94. leftOver.Add(child);
  95. }
  96. else
  97. {
  98. files.Add(child);
  99. }
  100. }
  101. var resolver = new VideoListResolver(new ExtendedNamingOptions(), new Naming.Logging.NullLogger());
  102. var resolverResult = resolver.Resolve(files.Select(i => new PortableFileInfo
  103. {
  104. FullName = i.FullName,
  105. Type = FileInfoType.File
  106. }).ToList()).ToList();
  107. var result = new MultiItemResolverResult
  108. {
  109. ExtraFiles = leftOver,
  110. Items = videos
  111. };
  112. var isInMixedFolder = resolverResult.Count > 0;
  113. foreach (var video in resolverResult)
  114. {
  115. var firstVideo = video.Files.First();
  116. var videoItem = new T
  117. {
  118. Path = video.Files[0].Path,
  119. IsInMixedFolder = isInMixedFolder,
  120. ProductionYear = video.Year,
  121. Name = video.Name,
  122. AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToList()
  123. };
  124. SetVideoType(videoItem, firstVideo);
  125. Set3DFormat(videoItem, firstVideo);
  126. result.Items.Add(videoItem);
  127. }
  128. return result;
  129. }
  130. /// <summary>
  131. /// Resolves the specified args.
  132. /// </summary>
  133. /// <param name="args">The args.</param>
  134. /// <returns>Video.</returns>
  135. protected override Video Resolve(ItemResolveArgs args)
  136. {
  137. var collectionType = args.GetCollectionType();
  138. if (IsInvalid(args.Parent, collectionType, args.FileSystemChildren))
  139. {
  140. return null;
  141. }
  142. // Find movies with their own folders
  143. if (args.IsDirectory)
  144. {
  145. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  146. {
  147. return FindMovie<MusicVideo>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType, false);
  148. }
  149. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  150. {
  151. return FindMovie<Video>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType, false);
  152. }
  153. if (string.IsNullOrEmpty(collectionType))
  154. {
  155. // Owned items should just use the plain video type
  156. if (args.Parent == null)
  157. {
  158. return FindMovie<Video>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType);
  159. }
  160. // Since the looping is expensive, this is an optimization to help us avoid it
  161. if (args.ContainsMetaFileByName("series.xml"))
  162. {
  163. return null;
  164. }
  165. return FindMovie<Movie>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType);
  166. }
  167. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) ||
  168. string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))
  169. {
  170. return FindMovie<Movie>(args.Path, args.Parent, args.FileSystemChildren.ToList(), args.DirectoryService, collectionType);
  171. }
  172. return null;
  173. }
  174. // Owned items will be caught by the plain video resolver
  175. if (args.Parent == null)
  176. {
  177. return null;
  178. }
  179. Video item = null;
  180. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  181. {
  182. item = ResolveVideo<MusicVideo>(args, false);
  183. }
  184. // To find a movie file, the collection type must be movies or boxsets
  185. else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase) ||
  186. string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))
  187. {
  188. item = ResolveVideo<Movie>(args, true);
  189. }
  190. else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  191. {
  192. item = ResolveVideo<Video>(args, false);
  193. }
  194. else if (string.IsNullOrEmpty(collectionType))
  195. {
  196. item = ResolveVideo<Movie>(args, false);
  197. }
  198. if (item != null)
  199. {
  200. item.IsInMixedFolder = true;
  201. }
  202. return item;
  203. }
  204. /// <summary>
  205. /// Sets the initial item values.
  206. /// </summary>
  207. /// <param name="item">The item.</param>
  208. /// <param name="args">The args.</param>
  209. protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
  210. {
  211. base.SetInitialItemValues(item, args);
  212. SetProviderIdFromPath(item);
  213. }
  214. /// <summary>
  215. /// Sets the provider id from path.
  216. /// </summary>
  217. /// <param name="item">The item.</param>
  218. private void SetProviderIdFromPath(Video item)
  219. {
  220. //we need to only look at the name of this actual item (not parents)
  221. var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath);
  222. var id = justName.GetAttributeValue("tmdbid");
  223. if (!string.IsNullOrEmpty(id))
  224. {
  225. item.SetProviderId(MetadataProviders.Tmdb, id);
  226. }
  227. }
  228. /// <summary>
  229. /// Finds a movie based on a child file system entries
  230. /// </summary>
  231. /// <typeparam name="T"></typeparam>
  232. /// <param name="path">The path.</param>
  233. /// <param name="parent">The parent.</param>
  234. /// <param name="fileSystemEntries">The file system entries.</param>
  235. /// <param name="directoryService">The directory service.</param>
  236. /// <param name="collectionType">Type of the collection.</param>
  237. /// <param name="supportMultiVersion">if set to <c>true</c> [support multi version].</param>
  238. /// <returns>Movie.</returns>
  239. private T FindMovie<T>(string path, Folder parent, List<FileSystemInfo> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool supportMultiVersion = true)
  240. where T : Video, new()
  241. {
  242. var multiDiscFolders = new List<FileSystemInfo>();
  243. // Search for a folder rip
  244. foreach (var child in fileSystemEntries)
  245. {
  246. var filename = child.Name;
  247. if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  248. {
  249. if (IsDvdDirectory(filename))
  250. {
  251. var movie = new T
  252. {
  253. Path = path,
  254. VideoType = VideoType.Dvd
  255. };
  256. Set3DFormat(movie);
  257. return movie;
  258. }
  259. if (IsBluRayDirectory(filename))
  260. {
  261. var movie = new T
  262. {
  263. Path = path,
  264. VideoType = VideoType.BluRay
  265. };
  266. Set3DFormat(movie);
  267. return movie;
  268. }
  269. multiDiscFolders.Add(child);
  270. }
  271. else if (IsDvdFile(filename))
  272. {
  273. var movie = new T
  274. {
  275. Path = path,
  276. VideoType = VideoType.Dvd
  277. };
  278. Set3DFormat(movie);
  279. return movie;
  280. }
  281. }
  282. var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, collectionType);
  283. // Test for multi-editions
  284. if (result.Items.Count > 1 && supportMultiVersion)
  285. {
  286. var filenamePrefix = Path.GetFileName(path);
  287. if (!string.IsNullOrWhiteSpace(filenamePrefix))
  288. {
  289. if (result.Items.All(i => _fileSystem.GetFileNameWithoutExtension(i.Path).StartsWith(filenamePrefix + " - ", StringComparison.OrdinalIgnoreCase)))
  290. {
  291. var movie = (T)result.Items[0];
  292. movie.Name = filenamePrefix;
  293. movie.LocalAlternateVersions = result.Items.Skip(1).Select(i => i.Path).ToList();
  294. movie.IsInMixedFolder = false;
  295. _logger.Debug("Multi-version video found: " + movie.Path);
  296. return movie;
  297. }
  298. }
  299. }
  300. if (result.Items.Count == 1)
  301. {
  302. var movie = (T)result.Items[0];
  303. movie.IsInMixedFolder = false;
  304. return movie;
  305. }
  306. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  307. {
  308. return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
  309. }
  310. return null;
  311. }
  312. /// <summary>
  313. /// Gets the multi disc movie.
  314. /// </summary>
  315. /// <typeparam name="T"></typeparam>
  316. /// <param name="multiDiscFolders">The folders.</param>
  317. /// <param name="directoryService">The directory service.</param>
  318. /// <returns>``0.</returns>
  319. private T GetMultiDiscMovie<T>(List<FileSystemInfo> multiDiscFolders, IDirectoryService directoryService)
  320. where T : Video, new()
  321. {
  322. var videoTypes = new List<VideoType>();
  323. var folderPaths = multiDiscFolders.Select(i => i.FullName).Where(i =>
  324. {
  325. var subFileEntries = directoryService.GetFileSystemEntries(i)
  326. .ToList();
  327. var subfolders = subFileEntries
  328. .Where(e => (e.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  329. .Select(d => d.Name)
  330. .ToList();
  331. if (subfolders.Any(IsDvdDirectory))
  332. {
  333. videoTypes.Add(VideoType.Dvd);
  334. return true;
  335. }
  336. if (subfolders.Any(IsBluRayDirectory))
  337. {
  338. videoTypes.Add(VideoType.BluRay);
  339. return true;
  340. }
  341. var subFiles = subFileEntries
  342. .Where(e => (e.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
  343. .Select(d => d.Name);
  344. if (subFiles.Any(IsDvdFile))
  345. {
  346. videoTypes.Add(VideoType.Dvd);
  347. return true;
  348. }
  349. return false;
  350. }).OrderBy(i => i).ToList();
  351. // If different video types were found, don't allow this
  352. if (videoTypes.Distinct().Count() > 1)
  353. {
  354. return null;
  355. }
  356. if (folderPaths.Count == 0)
  357. {
  358. return null;
  359. }
  360. var resolver = new StackResolver(new ExtendedNamingOptions(), new Naming.Logging.NullLogger());
  361. var result = resolver.ResolveDirectories(folderPaths);
  362. if (result.Stacks.Count != 1)
  363. {
  364. return null;
  365. }
  366. return new T
  367. {
  368. Path = folderPaths[0],
  369. AdditionalParts = folderPaths.Skip(1).ToList(),
  370. VideoType = videoTypes[0],
  371. Name = result.Stacks[0].Name
  372. };
  373. }
  374. private bool IsInvalid(Folder parent, string collectionType, IEnumerable<FileSystemInfo> files)
  375. {
  376. if (parent != null)
  377. {
  378. if (parent.IsRoot)
  379. {
  380. return true;
  381. }
  382. }
  383. // Don't do any resolving within a series structure
  384. if (string.IsNullOrEmpty(collectionType))
  385. {
  386. if (parent is Season || parent is Series)
  387. {
  388. return true;
  389. }
  390. // Since the looping is expensive, this is an optimization to help us avoid it
  391. if (files.Select(i => i.Name).Contains("series.xml", StringComparer.OrdinalIgnoreCase))
  392. {
  393. return true;
  394. }
  395. }
  396. var validCollectionTypes = new[]
  397. {
  398. string.Empty,
  399. CollectionType.Movies,
  400. CollectionType.HomeVideos,
  401. CollectionType.MusicVideos,
  402. CollectionType.BoxSets,
  403. CollectionType.Movies
  404. };
  405. return !validCollectionTypes.Contains(collectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase);
  406. }
  407. }
  408. }