MovieResolver.cs 20 KB

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