Movie.cs 6.7 KB

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