Movie.cs 6.2 KB

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