Person.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Extensions;
  7. using MediaBrowser.Controller.Providers;
  8. using Microsoft.Extensions.Logging;
  9. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// This is the full Person object that can be retrieved with all of it's data.
  13. /// </summary>
  14. public class Person : BaseItem, IItemByName, IHasLookupInfo<PersonLookupInfo>
  15. {
  16. /// <summary>
  17. /// Gets the folder containing the item.
  18. /// If the item is a folder, it returns the folder itself.
  19. /// </summary>
  20. /// <value>The containing folder path.</value>
  21. [JsonIgnore]
  22. public override string ContainingFolderPath => Path;
  23. /// <summary>
  24. /// Gets a value indicating whether to enable alpha numeric sorting.
  25. /// </summary>
  26. [JsonIgnore]
  27. public override bool EnableAlphaNumericSorting => false;
  28. [JsonIgnore]
  29. public override bool SupportsPeople => false;
  30. [JsonIgnore]
  31. public override bool SupportsAncestors => false;
  32. public override List<string> GetUserDataKeys()
  33. {
  34. var list = base.GetUserDataKeys();
  35. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  36. return list;
  37. }
  38. public override string CreatePresentationUniqueKey()
  39. {
  40. return GetUserDataKeys()[0];
  41. }
  42. public PersonLookupInfo GetLookupInfo()
  43. {
  44. return GetItemLookupInfo<PersonLookupInfo>();
  45. }
  46. public override double GetDefaultPrimaryImageAspectRatio()
  47. {
  48. double value = 2;
  49. value /= 3;
  50. return value;
  51. }
  52. public IReadOnlyList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  53. {
  54. query.PersonIds = new[] { Id };
  55. return LibraryManager.GetItemList(query);
  56. }
  57. public override bool CanDelete()
  58. {
  59. return false;
  60. }
  61. public override bool IsSaveLocalMetadataEnabled()
  62. {
  63. return true;
  64. }
  65. public static string GetPath(string name)
  66. {
  67. return GetPath(name, true);
  68. }
  69. public static string GetPath(string name, bool normalizeName)
  70. {
  71. // Trim the period at the end because windows will have a hard time with that
  72. var validFilename = normalizeName ?
  73. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  74. name;
  75. string subFolderPrefix = null;
  76. foreach (char c in validFilename)
  77. {
  78. if (char.IsLetterOrDigit(c))
  79. {
  80. subFolderPrefix = c.ToString();
  81. break;
  82. }
  83. }
  84. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  85. return string.IsNullOrEmpty(subFolderPrefix) ?
  86. System.IO.Path.Combine(path, validFilename) :
  87. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  88. }
  89. private string GetRebasedPath()
  90. {
  91. return GetPath(System.IO.Path.GetFileName(Path), false);
  92. }
  93. public override bool RequiresRefresh()
  94. {
  95. var newPath = GetRebasedPath();
  96. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  97. {
  98. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  99. return true;
  100. }
  101. return base.RequiresRefresh();
  102. }
  103. /// <summary>
  104. /// This is called before any metadata refresh and returns true or false indicating if changes were made.
  105. /// </summary>
  106. /// <param name="replaceAllMetadata"><c>true</c> to replace all metadata, <c>false</c> to not.</param>
  107. /// <returns><c>true</c> if changes were made, <c>false</c> if not.</returns>
  108. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  109. {
  110. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  111. var newPath = GetRebasedPath();
  112. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  113. {
  114. Path = newPath;
  115. hasChanges = true;
  116. }
  117. return hasChanges;
  118. }
  119. }
  120. }