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