MovieResolver.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Movies;
  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 MediaBrowser.Model.Extensions;
  9. using Emby.Naming.Video;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using MediaBrowser.Controller.Drawing;
  15. using MediaBrowser.Controller.IO;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Logging;
  18. namespace Emby.Server.Implementations.Library.Resolvers.Movies
  19. {
  20. /// <summary>
  21. /// Class MovieResolver
  22. /// </summary>
  23. public class MovieResolver : BaseVideoResolver<Video>, IMultiItemResolver
  24. {
  25. /// <summary>
  26. /// Gets the priority.
  27. /// </summary>
  28. /// <value>The priority.</value>
  29. public override ResolverPriority Priority
  30. {
  31. get
  32. {
  33. // Give plugins a chance to catch iso's first
  34. // Also since we have to loop through child files looking for videos,
  35. // see if we can avoid some of that by letting other resolvers claim folders first
  36. // Also run after series resolver
  37. return ResolverPriority.Third;
  38. }
  39. }
  40. public MultiItemResolverResult ResolveMultiple(Folder parent,
  41. List<FileSystemMetadata> files,
  42. string collectionType,
  43. IDirectoryService directoryService)
  44. {
  45. var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
  46. if (result != null)
  47. {
  48. foreach (var item in result.Items)
  49. {
  50. SetInitialItemValues((Video)item, null);
  51. }
  52. }
  53. return result;
  54. }
  55. private MultiItemResolverResult ResolveMultipleInternal(Folder parent,
  56. List<FileSystemMetadata> files,
  57. string collectionType,
  58. IDirectoryService directoryService)
  59. {
  60. if (IsInvalid(parent, collectionType))
  61. {
  62. return null;
  63. }
  64. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  65. {
  66. return ResolveVideos<MusicVideo>(parent, files, directoryService, true, collectionType, false);
  67. }
  68. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
  69. string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  70. {
  71. return ResolveVideos<Video>(parent, files, directoryService, false, collectionType, false);
  72. }
  73. if (string.IsNullOrWhiteSpace(collectionType))
  74. {
  75. // Owned items should just use the plain video type
  76. if (parent == null)
  77. {
  78. return ResolveVideos<Video>(parent, files, directoryService, false, collectionType, false);
  79. }
  80. if (parent is Series || parent.GetParents().OfType<Series>().Any())
  81. {
  82. return null;
  83. }
  84. return ResolveVideos<Movie>(parent, files, directoryService, false, collectionType, true);
  85. }
  86. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  87. {
  88. return ResolveVideos<Movie>(parent, files, directoryService, true, collectionType, true);
  89. }
  90. return null;
  91. }
  92. private MultiItemResolverResult ResolveVideos<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType, bool parseName)
  93. where T : Video, new()
  94. {
  95. var files = new List<FileSystemMetadata>();
  96. var videos = new List<BaseItem>();
  97. var leftOver = new List<FileSystemMetadata>();
  98. // Loop through each child file/folder and see if we find a video
  99. foreach (var child in fileSystemEntries)
  100. {
  101. // This is a hack but currently no better way to resolve a sometimes ambiguous situation
  102. if (string.IsNullOrWhiteSpace(collectionType))
  103. {
  104. if (string.Equals(child.Name, "tvshow.nfo", StringComparison.OrdinalIgnoreCase) ||
  105. string.Equals(child.Name, "season.nfo", StringComparison.OrdinalIgnoreCase))
  106. {
  107. return null;
  108. }
  109. }
  110. if (child.IsDirectory)
  111. {
  112. leftOver.Add(child);
  113. }
  114. else
  115. {
  116. files.Add(child);
  117. }
  118. }
  119. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  120. var resolver = new VideoListResolver(namingOptions);
  121. var resolverResult = resolver.Resolve(files, suppportMultiEditions).ToList();
  122. var result = new MultiItemResolverResult
  123. {
  124. ExtraFiles = leftOver,
  125. Items = videos
  126. };
  127. var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
  128. foreach (var video in resolverResult)
  129. {
  130. var firstVideo = video.Files.First();
  131. var videoItem = new T
  132. {
  133. Path = video.Files[0].Path,
  134. IsInMixedFolder = isInMixedFolder,
  135. ProductionYear = video.Year,
  136. Name = parseName ?
  137. video.Name :
  138. Path.GetFileNameWithoutExtension(video.Files[0].Path),
  139. AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToArray(),
  140. LocalAlternateVersions = video.AlternateVersions.Select(i => i.Path).ToArray()
  141. };
  142. SetVideoType(videoItem, firstVideo);
  143. Set3DFormat(videoItem, firstVideo);
  144. result.Items.Add(videoItem);
  145. }
  146. result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
  147. return result;
  148. }
  149. private bool ContainsFile(List<VideoInfo> result, FileSystemMetadata file)
  150. {
  151. return result.Any(i => ContainsFile(i, file));
  152. }
  153. private bool ContainsFile(VideoInfo result, FileSystemMetadata file)
  154. {
  155. return result.Files.Any(i => ContainsFile(i, file)) ||
  156. result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
  157. result.Extras.Any(i => ContainsFile(i, file));
  158. }
  159. private bool ContainsFile(VideoFileInfo result, FileSystemMetadata file)
  160. {
  161. return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
  162. }
  163. /// <summary>
  164. /// Resolves the specified args.
  165. /// </summary>
  166. /// <param name="args">The args.</param>
  167. /// <returns>Video.</returns>
  168. protected override Video Resolve(ItemResolveArgs args)
  169. {
  170. var collectionType = args.GetCollectionType();
  171. // Find movies with their own folders
  172. if (args.IsDirectory)
  173. {
  174. if (IsInvalid(args.Parent, collectionType))
  175. {
  176. return null;
  177. }
  178. var files = args.FileSystemChildren
  179. .Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
  180. .ToList();
  181. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  182. {
  183. return FindMovie<MusicVideo>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
  184. }
  185. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  186. {
  187. return FindMovie<Video>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
  188. }
  189. if (string.IsNullOrEmpty(collectionType))
  190. {
  191. // Owned items will be caught by the plain video resolver
  192. if (args.Parent == null)
  193. {
  194. //return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType);
  195. return null;
  196. }
  197. if (args.HasParent<Series>())
  198. {
  199. return null;
  200. }
  201. {
  202. return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
  203. }
  204. }
  205. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  206. {
  207. return FindMovie<Movie>(args, args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
  208. }
  209. return null;
  210. }
  211. // Handle owned items
  212. if (args.Parent == null)
  213. {
  214. return base.Resolve(args);
  215. }
  216. if (IsInvalid(args.Parent, collectionType))
  217. {
  218. return null;
  219. }
  220. Video item = null;
  221. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  222. {
  223. item = ResolveVideo<MusicVideo>(args, false);
  224. }
  225. // To find a movie file, the collection type must be movies or boxsets
  226. else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  227. {
  228. item = ResolveVideo<Movie>(args, true);
  229. }
  230. else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
  231. string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  232. {
  233. item = ResolveVideo<Video>(args, false);
  234. }
  235. else if (string.IsNullOrEmpty(collectionType))
  236. {
  237. if (args.HasParent<Series>())
  238. {
  239. return null;
  240. }
  241. item = ResolveVideo<Video>(args, false);
  242. }
  243. if (item != null)
  244. {
  245. item.IsInMixedFolder = true;
  246. }
  247. return item;
  248. }
  249. /// <summary>
  250. /// Sets the initial item values.
  251. /// </summary>
  252. /// <param name="item">The item.</param>
  253. /// <param name="args">The args.</param>
  254. protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
  255. {
  256. base.SetInitialItemValues(item, args);
  257. SetProviderIdsFromPath(item);
  258. }
  259. /// <summary>
  260. /// Sets the provider id from path.
  261. /// </summary>
  262. /// <param name="item">The item.</param>
  263. private void SetProviderIdsFromPath(Video item)
  264. {
  265. if (item is Movie || item is MusicVideo)
  266. {
  267. //we need to only look at the name of this actual item (not parents)
  268. var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath);
  269. if (!string.IsNullOrWhiteSpace(justName))
  270. {
  271. // check for tmdb id
  272. var tmdbid = justName.GetAttributeValue("tmdbid");
  273. if (!string.IsNullOrWhiteSpace(tmdbid))
  274. {
  275. item.SetProviderId(MetadataProviders.Tmdb, tmdbid);
  276. }
  277. }
  278. if (!string.IsNullOrWhiteSpace(item.Path))
  279. {
  280. // check for imdb id - we use full media path, as we can assume, that this will match in any use case (wither id in parent dir or in file name)
  281. var imdbid = item.Path.GetAttributeValue("imdbid");
  282. if (!string.IsNullOrWhiteSpace(imdbid))
  283. {
  284. item.SetProviderId(MetadataProviders.Imdb, imdbid);
  285. }
  286. }
  287. }
  288. }
  289. /// <summary>
  290. /// Finds a movie based on a child file system entries
  291. /// </summary>
  292. /// <typeparam name="T"></typeparam>
  293. /// <returns>Movie.</returns>
  294. private T FindMovie<T>(ItemResolveArgs args, string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool parseName)
  295. where T : Video, new()
  296. {
  297. var multiDiscFolders = new List<FileSystemMetadata>();
  298. var libraryOptions = args.GetLibraryOptions();
  299. var supportPhotos = string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) && libraryOptions.EnablePhotos;
  300. var photos = new List<FileSystemMetadata>();
  301. // Search for a folder rip
  302. foreach (var child in fileSystemEntries)
  303. {
  304. var filename = child.Name;
  305. if (child.IsDirectory)
  306. {
  307. if (IsDvdDirectory(child.FullName, filename, directoryService))
  308. {
  309. var movie = new T
  310. {
  311. Path = path,
  312. VideoType = VideoType.Dvd
  313. };
  314. Set3DFormat(movie);
  315. return movie;
  316. }
  317. if (IsBluRayDirectory(child.FullName, filename, directoryService))
  318. {
  319. var movie = new T
  320. {
  321. Path = path,
  322. VideoType = VideoType.BluRay
  323. };
  324. Set3DFormat(movie);
  325. return movie;
  326. }
  327. multiDiscFolders.Add(child);
  328. }
  329. else if (IsDvdFile(filename))
  330. {
  331. var movie = new T
  332. {
  333. Path = path,
  334. VideoType = VideoType.Dvd
  335. };
  336. Set3DFormat(movie);
  337. return movie;
  338. }
  339. else if (supportPhotos && !child.IsHidden && PhotoResolver.IsImageFile(child.FullName, _imageProcessor))
  340. {
  341. photos.Add(child);
  342. }
  343. }
  344. // TODO: Allow GetMultiDiscMovie in here
  345. var supportsMultiVersion = !string.Equals(collectionType, CollectionType.HomeVideos) &&
  346. !string.Equals(collectionType, CollectionType.Photos) &&
  347. !string.Equals(collectionType, CollectionType.MusicVideos);
  348. var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType, parseName) ??
  349. new MultiItemResolverResult();
  350. if (result.Items.Count == 1)
  351. {
  352. var videoPath = result.Items[0].Path;
  353. var hasPhotos = photos.Any(i => !PhotoResolver.IsOwnedByResolvedMedia(LibraryManager, libraryOptions, videoPath, i.Name));
  354. if (!hasPhotos)
  355. {
  356. var movie = (T)result.Items[0];
  357. movie.IsInMixedFolder = false;
  358. movie.Name = Path.GetFileName(movie.ContainingFolderPath);
  359. return movie;
  360. }
  361. }
  362. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  363. {
  364. return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
  365. }
  366. return null;
  367. }
  368. /// <summary>
  369. /// Gets the multi disc movie.
  370. /// </summary>
  371. /// <typeparam name="T"></typeparam>
  372. /// <param name="multiDiscFolders">The folders.</param>
  373. /// <param name="directoryService">The directory service.</param>
  374. /// <returns>``0.</returns>
  375. private T GetMultiDiscMovie<T>(List<FileSystemMetadata> multiDiscFolders, IDirectoryService directoryService)
  376. where T : Video, new()
  377. {
  378. var videoTypes = new List<VideoType>();
  379. var folderPaths = multiDiscFolders.Select(i => i.FullName).Where(i =>
  380. {
  381. var subFileEntries = directoryService.GetFileSystemEntries(i);
  382. var subfolders = subFileEntries
  383. .Where(e => e.IsDirectory)
  384. .ToList();
  385. if (subfolders.Any(s => IsDvdDirectory(s.FullName, s.Name, directoryService)))
  386. {
  387. videoTypes.Add(VideoType.Dvd);
  388. return true;
  389. }
  390. if (subfolders.Any(s => IsBluRayDirectory(s.FullName, s.Name, directoryService)))
  391. {
  392. videoTypes.Add(VideoType.BluRay);
  393. return true;
  394. }
  395. var subFiles = subFileEntries
  396. .Where(e => !e.IsDirectory)
  397. .Select(d => d.Name);
  398. if (subFiles.Any(IsDvdFile))
  399. {
  400. videoTypes.Add(VideoType.Dvd);
  401. return true;
  402. }
  403. return false;
  404. }).OrderBy(i => i).ToList();
  405. // If different video types were found, don't allow this
  406. if (videoTypes.Distinct().Count() > 1)
  407. {
  408. return null;
  409. }
  410. if (folderPaths.Count == 0)
  411. {
  412. return null;
  413. }
  414. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  415. var resolver = new StackResolver(namingOptions);
  416. var result = resolver.ResolveDirectories(folderPaths);
  417. if (result.Stacks.Count != 1)
  418. {
  419. return null;
  420. }
  421. var returnVideo = new T
  422. {
  423. Path = folderPaths[0],
  424. AdditionalParts = folderPaths.Skip(1).ToArray(),
  425. VideoType = videoTypes[0],
  426. Name = result.Stacks[0].Name
  427. };
  428. SetIsoType(returnVideo);
  429. return returnVideo;
  430. }
  431. private string[] ValidCollectionTypes = new[]
  432. {
  433. CollectionType.Movies,
  434. CollectionType.HomeVideos,
  435. CollectionType.MusicVideos,
  436. CollectionType.Movies,
  437. CollectionType.Photos
  438. };
  439. private bool IsInvalid(Folder parent, string collectionType)
  440. {
  441. if (parent != null)
  442. {
  443. if (parent.IsRoot)
  444. {
  445. return true;
  446. }
  447. }
  448. if (string.IsNullOrWhiteSpace(collectionType))
  449. {
  450. return false;
  451. }
  452. return !ValidCollectionTypes.Contains(collectionType, StringComparer.OrdinalIgnoreCase);
  453. }
  454. private IImageProcessor _imageProcessor;
  455. public MovieResolver(ILibraryManager libraryManager, IFileSystem fileSystem, IImageProcessor imageProcessor) : base(libraryManager, fileSystem)
  456. {
  457. _imageProcessor = imageProcessor;
  458. }
  459. }
  460. }