MovieResolver.cs 18 KB

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