Movie.cs 5.3 KB

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