Movie.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using MediaBrowser.Controller.IO;
  3. using MediaBrowser.Model.Entities;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.Entities.Movies
  11. {
  12. /// <summary>
  13. /// Class Movie
  14. /// </summary>
  15. public class Movie : Video
  16. {
  17. public List<Guid> SpecialFeatureIds { get; set; }
  18. public Movie()
  19. {
  20. SpecialFeatureIds = new List<Guid>();
  21. }
  22. /// <summary>
  23. /// Should be overridden to return the proper folder where metadata lives
  24. /// </summary>
  25. /// <value>The meta location.</value>
  26. [IgnoreDataMember]
  27. public override string MetaLocation
  28. {
  29. get
  30. {
  31. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso ? System.IO.Path.GetDirectoryName(Path) : Path;
  32. }
  33. }
  34. /// <summary>
  35. /// Gets the user data key.
  36. /// </summary>
  37. /// <returns>System.String.</returns>
  38. public override string GetUserDataKey()
  39. {
  40. return this.GetProviderId(MetadataProviders.Tmdb) ?? this.GetProviderId(MetadataProviders.Imdb) ?? base.GetUserDataKey();
  41. }
  42. /// <summary>
  43. /// Needed because the resolver stops at the movie folder and we find the video inside.
  44. /// </summary>
  45. /// <value><c>true</c> if [use parent path to create resolve args]; otherwise, <c>false</c>.</value>
  46. protected override bool UseParentPathToCreateResolveArgs
  47. {
  48. get
  49. {
  50. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso;
  51. }
  52. }
  53. /// <summary>
  54. /// Overrides the base implementation to refresh metadata for special features
  55. /// </summary>
  56. /// <param name="cancellationToken">The cancellation token.</param>
  57. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  58. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  59. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  60. /// <param name="resetResolveArgs">if set to <c>true</c> [reset resolve args].</param>
  61. /// <returns>Task{System.Boolean}.</returns>
  62. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  63. {
  64. // Kick off a task to refresh the main item
  65. var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  66. var specialFeaturesChanged = await RefreshSpecialFeatures(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  67. return specialFeaturesChanged || result;
  68. }
  69. private async Task<bool> RefreshSpecialFeatures(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  70. {
  71. var newItems = LoadSpecialFeatures().ToList();
  72. var newItemIds = newItems.Select(i => i.Id).ToList();
  73. var itemsChanged = !SpecialFeatureIds.SequenceEqual(newItemIds);
  74. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
  75. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  76. SpecialFeatureIds = newItemIds;
  77. return itemsChanged || results.Contains(true);
  78. }
  79. /// <summary>
  80. /// Loads the special features.
  81. /// </summary>
  82. /// <returns>IEnumerable{Video}.</returns>
  83. private IEnumerable<Video> LoadSpecialFeatures()
  84. {
  85. if (LocationType != LocationType.FileSystem)
  86. {
  87. return new List<Video>();
  88. }
  89. FileSystemInfo folder;
  90. try
  91. {
  92. folder = ResolveArgs.GetFileSystemEntryByName("specials");
  93. }
  94. catch (IOException ex)
  95. {
  96. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  97. return new List<Video>();
  98. }
  99. // Path doesn't exist. No biggie
  100. if (folder == null)
  101. {
  102. return new List<Video>();
  103. }
  104. IEnumerable<FileSystemInfo> files;
  105. try
  106. {
  107. files = new DirectoryInfo(folder.FullName).EnumerateFiles();
  108. }
  109. catch (IOException ex)
  110. {
  111. Logger.ErrorException("Error loading trailers for {0}", ex, Name);
  112. return new List<Video>();
  113. }
  114. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  115. {
  116. // Try to retrieve it from the db. If we don't find it, use the resolved version
  117. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  118. if (dbItem != null)
  119. {
  120. dbItem.ResolveArgs = video.ResolveArgs;
  121. video = dbItem;
  122. }
  123. return video;
  124. });
  125. }
  126. }
  127. }