Person.cs 4.1 KB

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