MovieResolver.cs 19 KB

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