Movie.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text.Json.Serialization;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Data.Enums;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Providers;
  14. namespace MediaBrowser.Controller.Entities.Movies
  15. {
  16. /// <summary>
  17. /// Class Movie.
  18. /// </summary>
  19. public class Movie : Video, IHasSpecialFeatures, IHasTrailers, IHasLookupInfo<MovieInfo>, ISupportsBoxSetGrouping
  20. {
  21. public Movie()
  22. {
  23. SpecialFeatureIds = Array.Empty<Guid>();
  24. RemoteTrailers = Array.Empty<MediaUrl>();
  25. LocalTrailerIds = Array.Empty<Guid>();
  26. RemoteTrailerIds = Array.Empty<Guid>();
  27. }
  28. /// <inheritdoc />
  29. public IReadOnlyList<Guid> SpecialFeatureIds { get; set; }
  30. /// <inheritdoc />
  31. public IReadOnlyList<Guid> LocalTrailerIds { get; set; }
  32. /// <inheritdoc />
  33. public IReadOnlyList<Guid> RemoteTrailerIds { get; set; }
  34. /// <summary>
  35. /// Gets or sets the name of the TMDB collection.
  36. /// </summary>
  37. /// <value>The name of the TMDB collection.</value>
  38. public string TmdbCollectionName { get; set; }
  39. [JsonIgnore]
  40. public string CollectionName
  41. {
  42. get => TmdbCollectionName;
  43. set => TmdbCollectionName = value;
  44. }
  45. [JsonIgnore]
  46. public override bool StopRefreshIfLocalMetadataFound => false;
  47. public override double GetDefaultPrimaryImageAspectRatio()
  48. {
  49. // hack for tv plugins
  50. if (SourceType == SourceType.Channel)
  51. {
  52. return 0;
  53. }
  54. return 2.0 / 3;
  55. }
  56. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  57. {
  58. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(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 (IsFileProtocol && SupportsOwnedItems && !IsInMixedFolder)
  62. {
  63. var specialFeaturesChanged = await RefreshSpecialFeatures(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  64. if (specialFeaturesChanged)
  65. {
  66. hasChanges = true;
  67. }
  68. }
  69. return hasChanges;
  70. }
  71. private async Task<bool> RefreshSpecialFeatures(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  72. {
  73. var newItems = LibraryManager.FindExtras(this, fileSystemChildren, options.DirectoryService).ToList();
  74. var newItemIds = newItems.Select(i => i.Id).ToArray();
  75. var itemsChanged = !SpecialFeatureIds.SequenceEqual(newItemIds);
  76. var ownerId = Id;
  77. var tasks = newItems.Select(i =>
  78. {
  79. var subOptions = new MetadataRefreshOptions(options);
  80. if (i.OwnerId != ownerId)
  81. {
  82. i.OwnerId = ownerId;
  83. subOptions.ForceSave = true;
  84. }
  85. return RefreshMetadataForOwnedItem(i, false, subOptions, cancellationToken);
  86. });
  87. await Task.WhenAll(tasks).ConfigureAwait(false);
  88. SpecialFeatureIds = newItemIds;
  89. return itemsChanged;
  90. }
  91. /// <inheritdoc />
  92. public override UnratedItem GetBlockUnratedType()
  93. {
  94. return UnratedItem.Movie;
  95. }
  96. public MovieInfo GetLookupInfo()
  97. {
  98. var info = GetItemLookupInfo<MovieInfo>();
  99. if (!IsInMixedFolder)
  100. {
  101. var name = System.IO.Path.GetFileName(ContainingFolderPath);
  102. if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
  103. {
  104. if (string.Equals(name, System.IO.Path.GetFileName(Path), StringComparison.OrdinalIgnoreCase))
  105. {
  106. // if the folder has the file extension, strip it
  107. name = System.IO.Path.GetFileNameWithoutExtension(name);
  108. }
  109. }
  110. info.Name = name;
  111. }
  112. return info;
  113. }
  114. /// <inheritdoc />
  115. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  116. {
  117. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  118. if (!ProductionYear.HasValue)
  119. {
  120. var info = LibraryManager.ParseName(Name);
  121. var yearInName = info.Year;
  122. if (yearInName.HasValue)
  123. {
  124. ProductionYear = yearInName;
  125. hasChanges = true;
  126. }
  127. else
  128. {
  129. // Try to get the year from the folder name
  130. if (!IsInMixedFolder)
  131. {
  132. info = LibraryManager.ParseName(System.IO.Path.GetFileName(ContainingFolderPath));
  133. yearInName = info.Year;
  134. if (yearInName.HasValue)
  135. {
  136. ProductionYear = yearInName;
  137. hasChanges = true;
  138. }
  139. }
  140. }
  141. }
  142. return hasChanges;
  143. }
  144. /// <inheritdoc />
  145. public override List<ExternalUrl> GetRelatedUrls()
  146. {
  147. var list = base.GetRelatedUrls();
  148. var imdbId = this.GetProviderId(MetadataProvider.Imdb);
  149. if (!string.IsNullOrEmpty(imdbId))
  150. {
  151. list.Add(new ExternalUrl
  152. {
  153. Name = "Trakt",
  154. Url = string.Format(CultureInfo.InvariantCulture, "https://trakt.tv/movies/{0}", imdbId)
  155. });
  156. }
  157. return list;
  158. }
  159. }
  160. }