Person.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.Json.Serialization;
  6. using Diacritics.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. public override List<string> GetUserDataKeys()
  17. {
  18. var list = base.GetUserDataKeys();
  19. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  20. return list;
  21. }
  22. public override string CreatePresentationUniqueKey()
  23. {
  24. return GetUserDataKeys()[0];
  25. }
  26. public PersonLookupInfo GetLookupInfo()
  27. {
  28. return GetItemLookupInfo<PersonLookupInfo>();
  29. }
  30. public override double GetDefaultPrimaryImageAspectRatio()
  31. {
  32. double value = 2;
  33. value /= 3;
  34. return value;
  35. }
  36. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  37. {
  38. query.PersonIds = new[] { Id };
  39. return LibraryManager.GetItemList(query);
  40. }
  41. /// <summary>
  42. /// Gets the folder containing the item.
  43. /// If the item is a folder, it returns the folder itself.
  44. /// </summary>
  45. /// <value>The containing folder path.</value>
  46. [JsonIgnore]
  47. public override string ContainingFolderPath => Path;
  48. public override bool CanDelete()
  49. {
  50. return false;
  51. }
  52. public override bool IsSaveLocalMetadataEnabled()
  53. {
  54. return true;
  55. }
  56. /// <summary>
  57. /// Gets a value indicating whether to enable alpha numeric sorting.
  58. /// </summary>
  59. [JsonIgnore]
  60. public override bool EnableAlphaNumericSorting => false;
  61. [JsonIgnore]
  62. public override bool SupportsPeople => false;
  63. [JsonIgnore]
  64. public override bool SupportsAncestors => false;
  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. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  107. {
  108. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  109. var newPath = GetRebasedPath();
  110. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  111. {
  112. Path = newPath;
  113. hasChanges = true;
  114. }
  115. return hasChanges;
  116. }
  117. }
  118. }