MovieResolver.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 Emby.Server.Implementations.Logging;
  15. using MediaBrowser.Common.IO;
  16. using MediaBrowser.Controller.IO;
  17. using MediaBrowser.Model.IO;
  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);
  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);
  76. }
  77. if (string.IsNullOrEmpty(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);
  83. }
  84. if (parent is Series || parent.GetParents().OfType<Series>().Any())
  85. {
  86. return null;
  87. }
  88. return ResolveVideos<Movie>(parent, files, directoryService, false);
  89. }
  90. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  91. {
  92. return ResolveVideos<Movie>(parent, files, directoryService, true);
  93. }
  94. return null;
  95. }
  96. private MultiItemResolverResult ResolveVideos<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions)
  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. if (child.IsDirectory)
  106. {
  107. leftOver.Add(child);
  108. }
  109. else if (IsIgnored(child.Name))
  110. {
  111. }
  112. else
  113. {
  114. files.Add(child);
  115. }
  116. }
  117. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  118. var resolver = new VideoListResolver(namingOptions, new PatternsLogger());
  119. var resolverResult = resolver.Resolve(files, suppportMultiEditions).ToList();
  120. var result = new MultiItemResolverResult
  121. {
  122. ExtraFiles = leftOver,
  123. Items = videos
  124. };
  125. var isInMixedFolder = resolverResult.Count > 1;
  126. foreach (var video in resolverResult)
  127. {
  128. var firstVideo = video.Files.First();
  129. var videoItem = new T
  130. {
  131. Path = video.Files[0].Path,
  132. IsInMixedFolder = isInMixedFolder,
  133. ProductionYear = video.Year,
  134. Name = video.Name,
  135. AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToList(),
  136. LocalAlternateVersions = video.AlternateVersions.Select(i => i.Path).ToList()
  137. };
  138. SetVideoType(videoItem, firstVideo);
  139. Set3DFormat(videoItem, firstVideo);
  140. result.Items.Add(videoItem);
  141. }
  142. result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
  143. return result;
  144. }
  145. private bool ContainsFile(List<VideoInfo> result, FileSystemMetadata file)
  146. {
  147. return result.Any(i => ContainsFile(i, file));
  148. }
  149. private bool ContainsFile(VideoInfo result, FileSystemMetadata file)
  150. {
  151. return result.Files.Any(i => ContainsFile(i, file)) ||
  152. result.AlternateVersions.Any(i => ContainsFile(i, file)) ||
  153. result.Extras.Any(i => ContainsFile(i, file));
  154. }
  155. private bool ContainsFile(VideoFileInfo result, FileSystemMetadata file)
  156. {
  157. return string.Equals(result.Path, file.FullName, StringComparison.OrdinalIgnoreCase);
  158. }
  159. /// <summary>
  160. /// Resolves the specified args.
  161. /// </summary>
  162. /// <param name="args">The args.</param>
  163. /// <returns>Video.</returns>
  164. protected override Video Resolve(ItemResolveArgs args)
  165. {
  166. var collectionType = args.GetCollectionType();
  167. if (IsInvalid(args.Parent, collectionType))
  168. {
  169. return null;
  170. }
  171. // Find movies with their own folders
  172. if (args.IsDirectory)
  173. {
  174. var files = args.FileSystemChildren
  175. .Where(i => !LibraryManager.IgnoreFile(i, args.Parent))
  176. .ToList();
  177. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  178. {
  179. return FindMovie<MusicVideo>(args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
  180. }
  181. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  182. {
  183. return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType, false);
  184. }
  185. if (string.IsNullOrEmpty(collectionType))
  186. {
  187. // Owned items will be caught by the plain video resolver
  188. if (args.Parent == null)
  189. {
  190. //return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType);
  191. return null;
  192. }
  193. if (args.HasParent<Series>())
  194. {
  195. return null;
  196. }
  197. {
  198. return FindMovie<Movie>(args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
  199. }
  200. }
  201. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  202. {
  203. return FindMovie<Movie>(args.Path, args.Parent, files, args.DirectoryService, collectionType, true);
  204. }
  205. return null;
  206. }
  207. // Owned items will be caught by the plain video resolver
  208. if (args.Parent == null)
  209. {
  210. return null;
  211. }
  212. Video item = null;
  213. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  214. {
  215. item = ResolveVideo<MusicVideo>(args, false);
  216. }
  217. // To find a movie file, the collection type must be movies or boxsets
  218. else if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  219. {
  220. item = ResolveVideo<Movie>(args, true);
  221. }
  222. else if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
  223. string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  224. {
  225. item = ResolveVideo<Video>(args, false);
  226. }
  227. else if (string.IsNullOrEmpty(collectionType))
  228. {
  229. if (args.HasParent<Series>())
  230. {
  231. return null;
  232. }
  233. item = ResolveVideo<Video>(args, false);
  234. }
  235. if (item != null)
  236. {
  237. item.IsInMixedFolder = true;
  238. }
  239. return item;
  240. }
  241. private bool IsIgnored(string filename)
  242. {
  243. // Ignore samples
  244. var sampleFilename = " " + filename.Replace(".", " ", StringComparison.OrdinalIgnoreCase)
  245. .Replace("-", " ", StringComparison.OrdinalIgnoreCase)
  246. .Replace("_", " ", StringComparison.OrdinalIgnoreCase)
  247. .Replace("!", " ", StringComparison.OrdinalIgnoreCase);
  248. if (sampleFilename.IndexOf(" sample ", StringComparison.OrdinalIgnoreCase) != -1)
  249. {
  250. return true;
  251. }
  252. return false;
  253. }
  254. /// <summary>
  255. /// Sets the initial item values.
  256. /// </summary>
  257. /// <param name="item">The item.</param>
  258. /// <param name="args">The args.</param>
  259. protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
  260. {
  261. base.SetInitialItemValues(item, args);
  262. SetProviderIdsFromPath(item);
  263. }
  264. /// <summary>
  265. /// Sets the provider id from path.
  266. /// </summary>
  267. /// <param name="item">The item.</param>
  268. private void SetProviderIdsFromPath(Video item)
  269. {
  270. if (item is Movie || item is MusicVideo)
  271. {
  272. //we need to only look at the name of this actual item (not parents)
  273. var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath);
  274. if (!string.IsNullOrWhiteSpace(justName))
  275. {
  276. // check for tmdb id
  277. var tmdbid = justName.GetAttributeValue("tmdbid");
  278. if (!string.IsNullOrWhiteSpace(tmdbid))
  279. {
  280. item.SetProviderId(MetadataProviders.Tmdb, tmdbid);
  281. }
  282. }
  283. if (!string.IsNullOrWhiteSpace(item.Path))
  284. {
  285. // 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)
  286. var imdbid = item.Path.GetAttributeValue("imdbid");
  287. if (!string.IsNullOrWhiteSpace(imdbid))
  288. {
  289. item.SetProviderId(MetadataProviders.Imdb, imdbid);
  290. }
  291. }
  292. }
  293. }
  294. /// <summary>
  295. /// Finds a movie based on a child file system entries
  296. /// </summary>
  297. /// <typeparam name="T"></typeparam>
  298. /// <returns>Movie.</returns>
  299. private T FindMovie<T>(string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool allowFilesAsFolders)
  300. where T : Video, new()
  301. {
  302. var multiDiscFolders = new List<FileSystemMetadata>();
  303. // Search for a folder rip
  304. foreach (var child in fileSystemEntries)
  305. {
  306. var filename = child.Name;
  307. if (child.IsDirectory)
  308. {
  309. if (IsDvdDirectory(filename))
  310. {
  311. var movie = new T
  312. {
  313. Path = path,
  314. VideoType = VideoType.Dvd
  315. };
  316. Set3DFormat(movie);
  317. return movie;
  318. }
  319. if (IsBluRayDirectory(filename))
  320. {
  321. var movie = new T
  322. {
  323. Path = path,
  324. VideoType = VideoType.BluRay
  325. };
  326. Set3DFormat(movie);
  327. return movie;
  328. }
  329. multiDiscFolders.Add(child);
  330. }
  331. else if (IsDvdFile(filename))
  332. {
  333. var movie = new T
  334. {
  335. Path = path,
  336. VideoType = VideoType.Dvd
  337. };
  338. Set3DFormat(movie);
  339. return movie;
  340. }
  341. }
  342. if (allowFilesAsFolders)
  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);
  349. if (result.Items.Count == 1)
  350. {
  351. var movie = (T)result.Items[0];
  352. movie.IsInMixedFolder = false;
  353. movie.Name = Path.GetFileName(movie.ContainingFolderPath);
  354. return movie;
  355. }
  356. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  357. {
  358. return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
  359. }
  360. }
  361. return null;
  362. }
  363. /// <summary>
  364. /// Gets the multi disc movie.
  365. /// </summary>
  366. /// <typeparam name="T"></typeparam>
  367. /// <param name="multiDiscFolders">The folders.</param>
  368. /// <param name="directoryService">The directory service.</param>
  369. /// <returns>``0.</returns>
  370. private T GetMultiDiscMovie<T>(List<FileSystemMetadata> multiDiscFolders, IDirectoryService directoryService)
  371. where T : Video, new()
  372. {
  373. var videoTypes = new List<VideoType>();
  374. var folderPaths = multiDiscFolders.Select(i => i.FullName).Where(i =>
  375. {
  376. var subFileEntries = directoryService.GetFileSystemEntries(i)
  377. .ToList();
  378. var subfolders = subFileEntries
  379. .Where(e => e.IsDirectory)
  380. .Select(d => d.Name)
  381. .ToList();
  382. if (subfolders.Any(IsDvdDirectory))
  383. {
  384. videoTypes.Add(VideoType.Dvd);
  385. return true;
  386. }
  387. if (subfolders.Any(IsBluRayDirectory))
  388. {
  389. videoTypes.Add(VideoType.BluRay);
  390. return true;
  391. }
  392. var subFiles = subFileEntries
  393. .Where(e => !e.IsDirectory)
  394. .Select(d => d.Name);
  395. if (subFiles.Any(IsDvdFile))
  396. {
  397. videoTypes.Add(VideoType.Dvd);
  398. return true;
  399. }
  400. return false;
  401. }).OrderBy(i => i).ToList();
  402. // If different video types were found, don't allow this
  403. if (videoTypes.Distinct().Count() > 1)
  404. {
  405. return null;
  406. }
  407. if (folderPaths.Count == 0)
  408. {
  409. return null;
  410. }
  411. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  412. var resolver = new StackResolver(namingOptions, new PatternsLogger());
  413. var result = resolver.ResolveDirectories(folderPaths);
  414. if (result.Stacks.Count != 1)
  415. {
  416. return null;
  417. }
  418. var returnVideo = new T
  419. {
  420. Path = folderPaths[0],
  421. AdditionalParts = folderPaths.Skip(1).ToList(),
  422. VideoType = videoTypes[0],
  423. Name = result.Stacks[0].Name
  424. };
  425. SetIsoType(returnVideo);
  426. return returnVideo;
  427. }
  428. private bool IsInvalid(Folder parent, string collectionType)
  429. {
  430. if (parent != null)
  431. {
  432. if (parent.IsRoot)
  433. {
  434. return true;
  435. }
  436. }
  437. var validCollectionTypes = new[]
  438. {
  439. CollectionType.Movies,
  440. CollectionType.HomeVideos,
  441. CollectionType.MusicVideos,
  442. CollectionType.Movies,
  443. CollectionType.Photos
  444. };
  445. if (string.IsNullOrWhiteSpace(collectionType))
  446. {
  447. return false;
  448. }
  449. return !validCollectionTypes.Contains(collectionType, StringComparer.OrdinalIgnoreCase);
  450. }
  451. }
  452. }