Person.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.Json.Serialization;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Extensions;
  8. using MediaBrowser.Controller.Providers;
  9. using Microsoft.Extensions.Logging;
  10. namespace MediaBrowser.Controller.Entities
  11. {
  12. /// <summary>
  13. /// This is the full Person object that can be retrieved with all of it's data.
  14. /// </summary>
  15. [Common.RequiresSourceSerialisation]
  16. public class Person : BaseItem, IItemByName, IHasLookupInfo<PersonLookupInfo>
  17. {
  18. /// <summary>
  19. /// Gets the folder containing the item.
  20. /// If the item is a folder, it returns the folder itself.
  21. /// </summary>
  22. /// <value>The containing folder path.</value>
  23. [JsonIgnore]
  24. public override string ContainingFolderPath => Path;
  25. /// <summary>
  26. /// Gets a value indicating whether to enable alpha numeric sorting.
  27. /// </summary>
  28. [JsonIgnore]
  29. public override bool EnableAlphaNumericSorting => false;
  30. [JsonIgnore]
  31. public override bool SupportsPeople => false;
  32. [JsonIgnore]
  33. public override bool SupportsAncestors => false;
  34. public override List<string> GetUserDataKeys()
  35. {
  36. var list = base.GetUserDataKeys();
  37. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  38. return list;
  39. }
  40. public override string CreatePresentationUniqueKey()
  41. {
  42. return GetUserDataKeys()[0];
  43. }
  44. public PersonLookupInfo GetLookupInfo()
  45. {
  46. return GetItemLookupInfo<PersonLookupInfo>();
  47. }
  48. public override double GetDefaultPrimaryImageAspectRatio()
  49. {
  50. double value = 2;
  51. value /= 3;
  52. return value;
  53. }
  54. public IReadOnlyList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  55. {
  56. query.PersonIds = new[] { Id };
  57. return LibraryManager.GetItemList(query);
  58. }
  59. public TaggedItemCounts GetTaggedItemCounts(InternalItemsQuery query)
  60. {
  61. query.PersonIds = [Id];
  62. var counts = new TaggedItemCounts();
  63. // TODO: Remove MusicAlbum and MusicArtist when the relationship between Persons and Music is removed
  64. query.IncludeItemTypes = [BaseItemKind.MusicAlbum];
  65. counts.AlbumCount = LibraryManager.GetCount(query);
  66. query.IncludeItemTypes = [BaseItemKind.MusicArtist];
  67. counts.ArtistCount = LibraryManager.GetCount(query);
  68. query.IncludeItemTypes = [BaseItemKind.Episode];
  69. counts.EpisodeCount = LibraryManager.GetCount(query);
  70. query.IncludeItemTypes = [BaseItemKind.Movie];
  71. counts.MovieCount = LibraryManager.GetCount(query);
  72. query.IncludeItemTypes = [BaseItemKind.MusicVideo];
  73. counts.MusicVideoCount = LibraryManager.GetCount(query);
  74. query.IncludeItemTypes = [BaseItemKind.LiveTvProgram];
  75. counts.ProgramCount = LibraryManager.GetCount(query);
  76. query.IncludeItemTypes = [BaseItemKind.Series];
  77. counts.SeriesCount = LibraryManager.GetCount(query);
  78. query.IncludeItemTypes = [BaseItemKind.Audio];
  79. counts.SongCount = LibraryManager.GetCount(query);
  80. query.IncludeItemTypes = [BaseItemKind.Trailer];
  81. counts.TrailerCount = LibraryManager.GetCount(query);
  82. return counts;
  83. }
  84. public override bool CanDelete()
  85. {
  86. return false;
  87. }
  88. public override bool IsSaveLocalMetadataEnabled()
  89. {
  90. return true;
  91. }
  92. public static string GetPath(string name)
  93. {
  94. return GetPath(name, true);
  95. }
  96. public static string GetPath(string name, bool normalizeName)
  97. {
  98. // Trim the period at the end because windows will have a hard time with that
  99. var validFilename = normalizeName ?
  100. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  101. name;
  102. string subFolderPrefix = null;
  103. foreach (char c in validFilename)
  104. {
  105. if (char.IsLetterOrDigit(c))
  106. {
  107. subFolderPrefix = c.ToString();
  108. break;
  109. }
  110. }
  111. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  112. return string.IsNullOrEmpty(subFolderPrefix) ?
  113. System.IO.Path.Combine(path, validFilename) :
  114. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  115. }
  116. private string GetRebasedPath()
  117. {
  118. return GetPath(System.IO.Path.GetFileName(Path), false);
  119. }
  120. public override bool RequiresRefresh()
  121. {
  122. var newPath = GetRebasedPath();
  123. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  124. {
  125. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  126. return true;
  127. }
  128. return base.RequiresRefresh();
  129. }
  130. /// <summary>
  131. /// This is called before any metadata refresh and returns true or false indicating if changes were made.
  132. /// </summary>
  133. /// <param name="replaceAllMetadata"><c>true</c> to replace all metadata, <c>false</c> to not.</param>
  134. /// <returns><c>true</c> if changes were made, <c>false</c> if not.</returns>
  135. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  136. {
  137. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  138. var newPath = GetRebasedPath();
  139. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  140. {
  141. Path = newPath;
  142. hasChanges = true;
  143. }
  144. return hasChanges;
  145. }
  146. }
  147. }