MovieResolver.cs 18 KB

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