Person.cs 4.4 KB

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