Movie.cs 6.0 KB

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