MovieResolver.cs 18 KB

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