2
0

Person.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.Json.Serialization;
  6. using MediaBrowser.Controller.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. /// Returns 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. [JsonIgnore]
  57. public override bool EnableAlphaNumericSorting => false;
  58. [JsonIgnore]
  59. public override bool SupportsPeople => false;
  60. [JsonIgnore]
  61. public override bool SupportsAncestors => false;
  62. public static string GetPath(string name)
  63. {
  64. return GetPath(name, true);
  65. }
  66. public static string GetPath(string name, bool normalizeName)
  67. {
  68. // Trim the period at the end because windows will have a hard time with that
  69. var validFilename = normalizeName ?
  70. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  71. name;
  72. string subFolderPrefix = null;
  73. foreach (char c in validFilename)
  74. {
  75. if (char.IsLetterOrDigit(c))
  76. {
  77. subFolderPrefix = c.ToString();
  78. break;
  79. }
  80. }
  81. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  82. return string.IsNullOrEmpty(subFolderPrefix) ?
  83. System.IO.Path.Combine(path, validFilename) :
  84. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  85. }
  86. private string GetRebasedPath()
  87. {
  88. return GetPath(System.IO.Path.GetFileName(Path), false);
  89. }
  90. public override bool RequiresRefresh()
  91. {
  92. var newPath = GetRebasedPath();
  93. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  94. {
  95. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  96. return true;
  97. }
  98. return base.RequiresRefresh();
  99. }
  100. /// <summary>
  101. /// This is called before any metadata refresh and returns true or false indicating if changes were made.
  102. /// </summary>
  103. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  104. {
  105. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  106. var newPath = GetRebasedPath();
  107. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  108. {
  109. Path = newPath;
  110. hasChanges = true;
  111. }
  112. return hasChanges;
  113. }
  114. }
  115. }