Person.cs 4.0 KB

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