Movie.cs 5.6 KB

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