MovieResolver.cs 19 KB

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