MovieResolver.cs 19 KB

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