MovieResolver.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Movies;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Controller.Resolvers;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Extensions;
  9. using Emby.Naming.Video;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using MediaBrowser.Controller.IO;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Logging;
  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
  29. {
  30. get
  31. {
  32. // Give plugins a chance to catch iso's first
  33. // Also since we have to loop through child files looking for videos,
  34. // see if we can avoid some of that by letting other resolvers claim folders first
  35. // Also run after series resolver
  36. return ResolverPriority.Third;
  37. }
  38. }
  39. public MultiItemResolverResult ResolveMultiple(Folder parent,
  40. List<FileSystemMetadata> files,
  41. string collectionType,
  42. IDirectoryService directoryService)
  43. {
  44. var result = ResolveMultipleInternal(parent, files, collectionType, directoryService);
  45. if (result != null)
  46. {
  47. foreach (var item in result.Items)
  48. {
  49. SetInitialItemValues((Video)item, null);
  50. }
  51. }
  52. return result;
  53. }
  54. private MultiItemResolverResult ResolveMultipleInternal(Folder parent,
  55. List<FileSystemMetadata> files,
  56. string collectionType,
  57. IDirectoryService directoryService)
  58. {
  59. if (IsInvalid(parent, collectionType))
  60. {
  61. return null;
  62. }
  63. if (string.Equals(collectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase))
  64. {
  65. return ResolveVideos<MusicVideo>(parent, files, directoryService, true, collectionType, false);
  66. }
  67. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase) ||
  68. string.Equals(collectionType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  69. {
  70. return ResolveVideos<Video>(parent, files, directoryService, false, collectionType, false);
  71. }
  72. if (string.IsNullOrWhiteSpace(collectionType))
  73. {
  74. // Owned items should just use the plain video type
  75. if (parent == null)
  76. {
  77. return ResolveVideos<Video>(parent, files, directoryService, false, collectionType, false);
  78. }
  79. if (parent is Series || parent.GetParents().OfType<Series>().Any())
  80. {
  81. return null;
  82. }
  83. return ResolveVideos<Movie>(parent, files, directoryService, false, collectionType, true);
  84. }
  85. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  86. {
  87. return ResolveVideos<Movie>(parent, files, directoryService, true, collectionType, true);
  88. }
  89. return null;
  90. }
  91. private MultiItemResolverResult ResolveVideos<T>(Folder parent, IEnumerable<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, bool suppportMultiEditions, string collectionType, bool parseName)
  92. where T : Video, new()
  93. {
  94. var files = new List<FileSystemMetadata>();
  95. var videos = new List<BaseItem>();
  96. var leftOver = new List<FileSystemMetadata>();
  97. // Loop through each child file/folder and see if we find a video
  98. foreach (var child in fileSystemEntries)
  99. {
  100. // This is a hack but currently no better way to resolve a sometimes ambiguous situation
  101. if (string.IsNullOrWhiteSpace(collectionType))
  102. {
  103. if (string.Equals(child.Name, "tvshow.nfo", StringComparison.OrdinalIgnoreCase) ||
  104. string.Equals(child.Name, "season.nfo", StringComparison.OrdinalIgnoreCase))
  105. {
  106. return null;
  107. }
  108. }
  109. if (child.IsDirectory)
  110. {
  111. leftOver.Add(child);
  112. }
  113. else if (IsIgnored(child.Name))
  114. {
  115. }
  116. else
  117. {
  118. files.Add(child);
  119. }
  120. }
  121. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  122. var resolver = new VideoListResolver(namingOptions);
  123. var resolverResult = resolver.Resolve(files, suppportMultiEditions).ToList();
  124. var result = new MultiItemResolverResult
  125. {
  126. ExtraFiles = leftOver,
  127. Items = videos
  128. };
  129. var isInMixedFolder = resolverResult.Count > 1 || (parent != null && parent.IsTopParent);
  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 = parseName ?
  139. video.Name :
  140. Path.GetFileName(video.Files[0].Path),
  141. AdditionalParts = video.Files.Skip(1).Select(i => i.Path).ToArray(),
  142. LocalAlternateVersions = video.AlternateVersions.Select(i => i.Path).ToArray()
  143. };
  144. SetVideoType(videoItem, firstVideo);
  145. Set3DFormat(videoItem, firstVideo);
  146. result.Items.Add(videoItem);
  147. }
  148. result.ExtraFiles.AddRange(files.Where(i => !ContainsFile(resolverResult, i)));
  149. return result;
  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 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. if (IsInvalid(args.Parent, collectionType))
  174. {
  175. return null;
  176. }
  177. // Find movies with their own folders
  178. if (args.IsDirectory)
  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.Path, args.Parent, files, args.DirectoryService, collectionType, true, false);
  186. }
  187. if (string.Equals(collectionType, CollectionType.HomeVideos, StringComparison.OrdinalIgnoreCase))
  188. {
  189. return FindMovie<Video>(args.Path, args.Parent, files, args.DirectoryService, collectionType, false, 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.Path, args.Parent, files, args.DirectoryService, collectionType, true, true);
  205. }
  206. }
  207. if (string.Equals(collectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  208. {
  209. return FindMovie<Movie>(args.Path, args.Parent, files, args.DirectoryService, collectionType, true, true);
  210. }
  211. return null;
  212. }
  213. // Owned items will be caught by the plain video resolver
  214. if (args.Parent == null)
  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. private bool IsIgnored(string filename)
  248. {
  249. // Ignore samples
  250. var sampleFilename = " " + filename.Replace(".", " ", StringComparison.OrdinalIgnoreCase)
  251. .Replace("-", " ", StringComparison.OrdinalIgnoreCase)
  252. .Replace("_", " ", StringComparison.OrdinalIgnoreCase)
  253. .Replace("!", " ", StringComparison.OrdinalIgnoreCase);
  254. if (sampleFilename.IndexOf(" sample ", StringComparison.OrdinalIgnoreCase) != -1)
  255. {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /// <summary>
  261. /// Sets the initial item values.
  262. /// </summary>
  263. /// <param name="item">The item.</param>
  264. /// <param name="args">The args.</param>
  265. protected override void SetInitialItemValues(Video item, ItemResolveArgs args)
  266. {
  267. base.SetInitialItemValues(item, args);
  268. SetProviderIdsFromPath(item);
  269. }
  270. /// <summary>
  271. /// Sets the provider id from path.
  272. /// </summary>
  273. /// <param name="item">The item.</param>
  274. private void SetProviderIdsFromPath(Video item)
  275. {
  276. if (item is Movie || item is MusicVideo)
  277. {
  278. //we need to only look at the name of this actual item (not parents)
  279. var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath);
  280. if (!string.IsNullOrWhiteSpace(justName))
  281. {
  282. // check for tmdb id
  283. var tmdbid = justName.GetAttributeValue("tmdbid");
  284. if (!string.IsNullOrWhiteSpace(tmdbid))
  285. {
  286. item.SetProviderId(MetadataProviders.Tmdb, tmdbid);
  287. }
  288. }
  289. if (!string.IsNullOrWhiteSpace(item.Path))
  290. {
  291. // 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)
  292. var imdbid = item.Path.GetAttributeValue("imdbid");
  293. if (!string.IsNullOrWhiteSpace(imdbid))
  294. {
  295. item.SetProviderId(MetadataProviders.Imdb, imdbid);
  296. }
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// Finds a movie based on a child file system entries
  302. /// </summary>
  303. /// <typeparam name="T"></typeparam>
  304. /// <returns>Movie.</returns>
  305. private T FindMovie<T>(string path, Folder parent, List<FileSystemMetadata> fileSystemEntries, IDirectoryService directoryService, string collectionType, bool allowFilesAsFolders, bool parseName)
  306. where T : Video, new()
  307. {
  308. var multiDiscFolders = new List<FileSystemMetadata>();
  309. // Search for a folder rip
  310. foreach (var child in fileSystemEntries)
  311. {
  312. var filename = child.Name;
  313. if (child.IsDirectory)
  314. {
  315. if (IsDvdDirectory(child.FullName, filename, directoryService))
  316. {
  317. var movie = new T
  318. {
  319. Path = path,
  320. VideoType = VideoType.Dvd
  321. };
  322. Set3DFormat(movie);
  323. return movie;
  324. }
  325. if (IsBluRayDirectory(child.FullName, filename, directoryService))
  326. {
  327. var movie = new T
  328. {
  329. Path = path,
  330. VideoType = VideoType.BluRay
  331. };
  332. Set3DFormat(movie);
  333. return movie;
  334. }
  335. multiDiscFolders.Add(child);
  336. }
  337. else if (IsDvdFile(filename))
  338. {
  339. var movie = new T
  340. {
  341. Path = path,
  342. VideoType = VideoType.Dvd
  343. };
  344. Set3DFormat(movie);
  345. return movie;
  346. }
  347. }
  348. if (allowFilesAsFolders)
  349. {
  350. // TODO: Allow GetMultiDiscMovie in here
  351. var supportsMultiVersion = !string.Equals(collectionType, CollectionType.HomeVideos) &&
  352. !string.Equals(collectionType, CollectionType.Photos) &&
  353. !string.Equals(collectionType, CollectionType.MusicVideos);
  354. var result = ResolveVideos<T>(parent, fileSystemEntries, directoryService, supportsMultiVersion, collectionType, parseName) ??
  355. new MultiItemResolverResult();
  356. if (result.Items.Count == 1)
  357. {
  358. var movie = (T)result.Items[0];
  359. movie.IsInMixedFolder = false;
  360. movie.Name = Path.GetFileName(movie.ContainingFolderPath);
  361. return movie;
  362. }
  363. if (result.Items.Count == 0 && multiDiscFolders.Count > 0)
  364. {
  365. return GetMultiDiscMovie<T>(multiDiscFolders, directoryService);
  366. }
  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 bool IsInvalid(Folder parent, string collectionType)
  434. {
  435. if (parent != null)
  436. {
  437. if (parent.IsRoot)
  438. {
  439. return true;
  440. }
  441. }
  442. var validCollectionTypes = new[]
  443. {
  444. CollectionType.Movies,
  445. CollectionType.HomeVideos,
  446. CollectionType.MusicVideos,
  447. CollectionType.Movies,
  448. CollectionType.Photos
  449. };
  450. if (string.IsNullOrWhiteSpace(collectionType))
  451. {
  452. return false;
  453. }
  454. return !validCollectionTypes.Contains(collectionType, StringComparer.OrdinalIgnoreCase);
  455. }
  456. public MovieResolver(ILibraryManager libraryManager, IFileSystem fileSystem) : base(libraryManager, fileSystem)
  457. {
  458. }
  459. }
  460. }