MovieResolver.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /// <summary>
  25. /// Gets the priority.
  26. /// </summary>
  27. /// <value>The priority.</value>
  28. public override ResolverPriority Priority
  29. {
  30. get
  31. {
  32. // Give plugins a chance to catch iso's first
  33. // Also since we have to loop through child files looking for videos,
  34. // see if we can avoid some of that by letting other resolvers claim folders first
  35. // Also run after series resolver
  36. return ResolverPriority.Third;
  37. }
  38. }
  39. public MultiItemResolverResult ResolveMultiple(Folder parent,
  40. List<FileSystemMetadata> files,
  41. string collectionType,
  42. IDirectoryService directoryService)
  43. {
  44. var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
  45. if (result != null)
  46. {
  47. foreach (var item in result.Items)
  48. {
  49. SetInitialItemValues((Video)item, null);
  50. }
  51. }
  52. return result;
  53. }
  54. private MultiItemResolverResult ResolveMultipleInternal(Folder parent,
  55. List<FileSystemMetadata> files,
  56. string collectionType,
  57. IDirectoryService directoryService)
  58. {
  59. if (IsInvalid(parent, collectionType))
  60. {
  61. return null;
  62. }
  63. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  64. {
  65. return ResolveVideos<MusicVideo>(parent, files, directoryService, false, collectionType);
  66. }
  67. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
  68. string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  69. {
  70. return ResolveVideos<Video>(parent, files, directoryService, false, collectionType);
  71. }
  72. if (string.IsNullOrWhiteSpace(collectionType))
  73. {
  74. // Owned items should just use the plain video type
  75. if (parent == null)
  76. {
  77. return ResolveVideos<Video>(parent, files, directoryService, false, collectionType);
  78. }
  79. if (parent is Series || parent.GetParents().OfType<Series>().Any())
  80. {
  81. return null;
  82. }
  83. return ResolveVideos<Movie>(parent, files, directoryService, false, collectionType);
  84. }
  85. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  86. {
  87. return ResolveVideos<Movie>(parent, files, directoryService, true, collectionType);
  88. }
  89. return null;
  90. }
  91. private MultiItemResolverResult ResolveVideos<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType)
  92. where T : Video, new()
  93. {
  94. var files = new List<FileSystemMetadata>();
  95. var videos = new List<BaseItem>();
  96. var leftOver = new List<FileSystemMetadata>();
  97. // Loop through each child file/folder and see if we find a video
  98. foreach (var child in fileSystemEntries)
  99. {
  100. // This is a hack but currently no better way to resolve a sometimes ambiguous situation
  101. if (string.IsNullOrWhiteSpace(collectionType))
  102. {
  103. if (string.Equals(child.Name, "tvshow.nfo", StringComparison.OrdinalIgnoreCase) ||
  104. string.Equals(child.Name, "season.nfo", StringComparison.OrdinalIgnoreCase))
  105. {
  106. return null;
  107. }
  108. }
  109. if (child.IsDirectory)
  110. {
  111. leftOver.Add(child);
  112. }
  113. else if (IsIgnored(child.Name))
  114. {
  115. }
  116. else
  117. {
  118. files.Add(child);
  119. }
  120. }
  121. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  122. var resolver = new VideoListResolver(namingOptions, new NullLogger());
  123. var resolverResult = resolver.Resolve(files, suppportMultiEditions).ToList();
  124. var result = new MultiItemResolverResult
  125. {
  126. ExtraFiles = leftOver,
  127. Items = videos
  128. };
  129. var isInMixedFolder = resolverResult.Count > 1;
  130. foreach (var video in resolverResult)
  131. {
  132. var firstVideo = video.Files.First();
  133. var videoItem = new T
  134. {
  135. Path = video.Files[0].Path,
  136. IsInMixedFolder = isInMixedFolder,
  137. ProductionYear = video.Year,
  138. Name = video.Name,
  139. AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToList(),
  140. LocalAlternateVersions = video.AlternateVersions.Select(i => i.Path).ToList()
  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. if (IsInvalid(args.Parent, collectionType))
  172. {
  173. return null;
  174. }
  175. // Find movies with their own folders
  176. if (args.IsDirectory)
  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.Path, args.Parent, files, args.DirectoryService, collectionType, true);
  184. }
  185. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  186. {
  187. return FindMovie<Video>(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.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.Path, args.Parent, files, args.DirectoryService, collectionType, true);
  208. }
  209. return null;
  210. }
  211. // Owned items will be caught by the plain video resolver
  212. if (args.Parent == null)
  213. {
  214. return null;
  215. }
  216. Video item = null;
  217. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  218. {
  219. item = ResolveVideo<MusicVideo>(args, false);
  220. }
  221. // To find a movie file, the collection type must be movies or boxsets
  222. else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  223. {
  224. item = ResolveVideo<Movie>(args, true);
  225. }
  226. else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
  227. string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  228. {
  229. item = ResolveVideo<Video>(args, false);
  230. }
  231. else if (string.IsNullOrEmpty(collectionType))
  232. {
  233. if (args.HasParent<Series>())
  234. {
  235. return null;
  236. }
  237. item = ResolveVideo<Video>(args, false);
  238. }
  239. if (item != null)
  240. {
  241. item.IsInMixedFolder = true;
  242. }
  243. return item;
  244. }
  245. private bool IsIgnored(string filename)
  246. {
  247. // Ignore samples
  248. var sampleFilename = " " + filename.Replace(".", " ", StringComparison.OrdinalIgnoreCase)
  249. .Replace("-", " ", StringComparison.OrdinalIgnoreCase)
  250. .Replace("_", " ", StringComparison.OrdinalIgnoreCase)
  251. .Replace("!", " ", StringComparison.OrdinalIgnoreCase);
  252. if (sampleFilename.IndexOf(" sample ", StringComparison.OrdinalIgnoreCase) != -1)
  253. {
  254. return true;
  255. }
  256. return false;
  257. }
  258. /// <summary>
  259. /// Sets the initial item values.
  260. /// </summary>
  261. /// <param name="item">The item.</param>
  262. /// <param name="args">The args.</param>
  263. protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
  264. {
  265. base.SetInitialItemValues(item, args);
  266. SetProviderIdsFromPath(item);
  267. }
  268. /// <summary>
  269. /// Sets the provider id from path.
  270. /// </summary>
  271. /// <param name="item">The item.</param>
  272. private void SetProviderIdsFromPath(Video item)
  273. {
  274. if (item is Movie || item is MusicVideo)
  275. {
  276. //we need to only look at the name of this actual item (not parents)
  277. var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath);
  278. if (!string.IsNullOrWhiteSpace(justName))
  279. {
  280. // check for tmdb id
  281. var tmdbid = justName.GetAttributeValue("tmdbid");
  282. if (!string.IsNullOrWhiteSpace(tmdbid))
  283. {
  284. item.SetProviderId(MetadataProviders.Tmdb, tmdbid);
  285. }
  286. }
  287. if (!string.IsNullOrWhiteSpace(item.Path))
  288. {
  289. // 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)
  290. var imdbid = item.Path.GetAttributeValue("imdbid");
  291. if (!string.IsNullOrWhiteSpace(imdbid))
  292. {
  293. item.SetProviderId(MetadataProviders.Imdb, imdbid);
  294. }
  295. }
  296. }
  297. }
  298. /// <summary>
  299. /// Finds a movie based on a child file system entries
  300. /// </summary>
  301. /// <typeparam name="T"></typeparam>
  302. /// <returns>Movie.</returns>
  303. private T FindMovie<T>(string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool allowFilesAsFolders)
  304. where T : Video, new()
  305. {
  306. var multiDiscFolders = new List<FileSystemMetadata>();
  307. // Search for a folder rip
  308. foreach (var child in fileSystemEntries)
  309. {
  310. var filename = child.Name;
  311. if (child.IsDirectory)
  312. {
  313. if (IsDvdDirectory(child.FullName, filename, directoryService))
  314. {
  315. var movie = new T
  316. {
  317. Path = path,
  318. VideoType = VideoType.Dvd
  319. };
  320. Set3DFormat(movie);
  321. return movie;
  322. }
  323. if (IsBluRayDirectory(child.FullName, filename, directoryService))
  324. {
  325. var movie = new T
  326. {
  327. Path = path,
  328. VideoType = VideoType.BluRay
  329. };
  330. Set3DFormat(movie);
  331. return movie;
  332. }
  333. multiDiscFolders.Add(child);
  334. }
  335. else if (IsDvdFile(filename))
  336. {
  337. var movie = new T
  338. {
  339. Path = path,
  340. VideoType = VideoType.Dvd
  341. };
  342. Set3DFormat(movie);
  343. return movie;
  344. }
  345. }
  346. if (allowFilesAsFolders)
  347. {
  348. // TODO: Allow GetMultiDiscMovie in here
  349. var supportsMultiVersion = !string.Equals(collectionType, CollectionType.HomeVideos) &&
  350. !string.Equals(collectionType, CollectionType.Photos) &&
  351. !string.Equals(collectionType, CollectionType.MusicVideos);
  352. var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType) ??
  353. new MultiItemResolverResult();
  354. if (result.Items.Count == 1)
  355. {
  356. var movie = (T)result.Items[0];
  357. movie.IsInMixedFolder = false;
  358. movie.Name = Path.GetFileName(movie.ContainingFolderPath);
  359. return movie;
  360. }
  361. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  362. {
  363. return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
  364. }
  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, new NullLogger());
  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).ToList(),
  425. VideoType = videoTypes[0],
  426. Name = result.Stacks[0].Name
  427. };
  428. SetIsoType(returnVideo);
  429. return returnVideo;
  430. }
  431. private bool IsInvalid(Folder parent, string collectionType)
  432. {
  433. if (parent != null)
  434. {
  435. if (parent.IsRoot)
  436. {
  437. return true;
  438. }
  439. }
  440. var validCollectionTypes = new[]
  441. {
  442. CollectionType.Movies,
  443. CollectionType.HomeVideos,
  444. CollectionType.MusicVideos,
  445. CollectionType.Movies,
  446. CollectionType.Photos
  447. };
  448. if (string.IsNullOrWhiteSpace(collectionType))
  449. {
  450. return false;
  451. }
  452. return !validCollectionTypes.Contains(collectionType, StringComparer.OrdinalIgnoreCase);
  453. }
  454. public MovieResolver(ILibraryManager libraryManager, IFileSystem fileSystem) : base(libraryManager, fileSystem)
  455. {
  456. }
  457. }
  458. }