MovieResolver.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. // check for tmdb id
  262. var tmdbid = justName.GetAttributeValue("tmdbid");
  263. if (!string.IsNullOrEmpty(tmdbid))
  264. {
  265. item.SetProviderId(MetadataProviders.Tmdb, tmdbid);
  266. }
  267. // 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)
  268. var imdbid = item.Path.GetAttributeValue("imdbid");
  269. if (!string.IsNullOrEmpty(imdbid))
  270. {
  271. item.SetProviderId(MetadataProviders.Imdb, imdbid);
  272. }
  273. }
  274. }
  275. /// <summary>
  276. /// Finds a movie based on a child file system entries
  277. /// </summary>
  278. /// <typeparam name="T"></typeparam>
  279. /// <param name="path">The path.</param>
  280. /// <param name="parent">The parent.</param>
  281. /// <param name="fileSystemEntries">The file system entries.</param>
  282. /// <param name="directoryService">The directory service.</param>
  283. /// <param name="collectionType">Type of the collection.</param>
  284. /// <returns>Movie.</returns>
  285. private T FindMovie<T>(string path, Folder parent, List<FileSystemInfo> fileSystemEntries, IDirectoryService directoryService, string collectionType)
  286. where T : Video, new()
  287. {
  288. var multiDiscFolders = new List<FileSystemInfo>();
  289. // Search for a folder rip
  290. foreach (var child in fileSystemEntries)
  291. {
  292. var filename = child.Name;
  293. if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  294. {
  295. if (IsDvdDirectory(filename))
  296. {
  297. var movie = new T
  298. {
  299. Path = path,
  300. VideoType = VideoType.Dvd
  301. };
  302. Set3DFormat(movie);
  303. return movie;
  304. }
  305. if (IsBluRayDirectory(filename))
  306. {
  307. var movie = new T
  308. {
  309. Path = path,
  310. VideoType = VideoType.BluRay
  311. };
  312. Set3DFormat(movie);
  313. return movie;
  314. }
  315. multiDiscFolders.Add(child);
  316. }
  317. else if (IsDvdFile(filename))
  318. {
  319. var movie = new T
  320. {
  321. Path = path,
  322. VideoType = VideoType.Dvd
  323. };
  324. Set3DFormat(movie);
  325. return movie;
  326. }
  327. }
  328. var supportsMultiVersion = !string.Equals(collectionType, CollectionType.HomeVideos) &&
  329. !string.Equals(collectionType, CollectionType.Photos) &&
  330. !string.Equals(collectionType, CollectionType.MusicVideos);
  331. var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, collectionType, supportsMultiVersion);
  332. if (result.Items.Count == 1)
  333. {
  334. var movie = (T)result.Items[0];
  335. movie.IsInMixedFolder = false;
  336. movie.Name = Path.GetFileName(movie.ContainingFolderPath);
  337. return movie;
  338. }
  339. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  340. {
  341. return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
  342. }
  343. return null;
  344. }
  345. /// <summary>
  346. /// Gets the multi disc movie.
  347. /// </summary>
  348. /// <typeparam name="T"></typeparam>
  349. /// <param name="multiDiscFolders">The folders.</param>
  350. /// <param name="directoryService">The directory service.</param>
  351. /// <returns>``0.</returns>
  352. private T GetMultiDiscMovie<T>(List<FileSystemInfo> multiDiscFolders, IDirectoryService directoryService)
  353. where T : Video, new()
  354. {
  355. var videoTypes = new List<VideoType>();
  356. var folderPaths = multiDiscFolders.Select(i => i.FullName).Where(i =>
  357. {
  358. var subFileEntries = directoryService.GetFileSystemEntries(i)
  359. .ToList();
  360. var subfolders = subFileEntries
  361. .Where(e => (e.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  362. .Select(d => d.Name)
  363. .ToList();
  364. if (subfolders.Any(IsDvdDirectory))
  365. {
  366. videoTypes.Add(VideoType.Dvd);
  367. return true;
  368. }
  369. if (subfolders.Any(IsBluRayDirectory))
  370. {
  371. videoTypes.Add(VideoType.BluRay);
  372. return true;
  373. }
  374. var subFiles = subFileEntries
  375. .Where(e => (e.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
  376. .Select(d => d.Name);
  377. if (subFiles.Any(IsDvdFile))
  378. {
  379. videoTypes.Add(VideoType.Dvd);
  380. return true;
  381. }
  382. return false;
  383. }).OrderBy(i => i).ToList();
  384. // If different video types were found, don't allow this
  385. if (videoTypes.Distinct().Count() > 1)
  386. {
  387. return null;
  388. }
  389. if (folderPaths.Count == 0)
  390. {
  391. return null;
  392. }
  393. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  394. var resolver = new StackResolver(namingOptions, new PatternsLogger());
  395. var result = resolver.ResolveDirectories(folderPaths);
  396. if (result.Stacks.Count != 1)
  397. {
  398. return null;
  399. }
  400. return new T
  401. {
  402. Path = folderPaths[0],
  403. AdditionalParts = folderPaths.Skip(1).ToList(),
  404. VideoType = videoTypes[0],
  405. Name = result.Stacks[0].Name
  406. };
  407. }
  408. private bool IsInvalid(Folder parent, string collectionType, IEnumerable<FileSystemInfo> files)
  409. {
  410. if (parent != null)
  411. {
  412. if (parent.IsRoot)
  413. {
  414. return true;
  415. }
  416. }
  417. var validCollectionTypes = new[]
  418. {
  419. string.Empty,
  420. CollectionType.Movies,
  421. CollectionType.HomeVideos,
  422. CollectionType.MusicVideos,
  423. CollectionType.Movies,
  424. CollectionType.Photos
  425. };
  426. return !validCollectionTypes.Contains(collectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase);
  427. }
  428. }
  429. }