MovieResolver.cs 19 KB

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