Person.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using MediaBrowser.Controller.Providers;
  2. using System;
  3. using System.Collections.Generic;
  4. using MediaBrowser.Controller.Extensions;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Serialization;
  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. [IgnoreDataMember]
  46. public override string ContainingFolderPath
  47. {
  48. get
  49. {
  50. return Path;
  51. }
  52. }
  53. public override bool CanDelete()
  54. {
  55. return false;
  56. }
  57. public override bool IsSaveLocalMetadataEnabled()
  58. {
  59. return true;
  60. }
  61. [IgnoreDataMember]
  62. public override bool EnableAlphaNumericSorting
  63. {
  64. get
  65. {
  66. return false;
  67. }
  68. }
  69. [IgnoreDataMember]
  70. public override bool SupportsPeople
  71. {
  72. get
  73. {
  74. return false;
  75. }
  76. }
  77. [IgnoreDataMember]
  78. public override bool SupportsAncestors
  79. {
  80. get
  81. {
  82. return false;
  83. }
  84. }
  85. public static string GetPath(string name)
  86. {
  87. return GetPath(name, true);
  88. }
  89. public static string GetPath(string name, bool normalizeName)
  90. {
  91. // Trim the period at the end because windows will have a hard time with that
  92. var validFilename = normalizeName ?
  93. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  94. name;
  95. string subFolderPrefix = null;
  96. foreach (char c in validFilename)
  97. {
  98. if (char.IsLetterOrDigit(c))
  99. {
  100. subFolderPrefix = c.ToString();
  101. break;
  102. }
  103. }
  104. var path = ConfigurationManager.ApplicationPaths.PeoplePath;
  105. return string.IsNullOrEmpty(subFolderPrefix) ?
  106. System.IO.Path.Combine(path, validFilename) :
  107. System.IO.Path.Combine(path, subFolderPrefix, validFilename);
  108. }
  109. private string GetRebasedPath()
  110. {
  111. return GetPath(System.IO.Path.GetFileName(Path), false);
  112. }
  113. public override bool RequiresRefresh()
  114. {
  115. var newPath = GetRebasedPath();
  116. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  117. {
  118. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  119. return true;
  120. }
  121. return base.RequiresRefresh();
  122. }
  123. /// <summary>
  124. /// This is called before any metadata refresh and returns true or false indicating if changes were made
  125. /// </summary>
  126. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  127. {
  128. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  129. var newPath = GetRebasedPath();
  130. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  131. {
  132. Path = newPath;
  133. hasChanges = true;
  134. }
  135. return hasChanges;
  136. }
  137. }
  138. /// <summary>
  139. /// This is the small Person stub that is attached to BaseItems
  140. /// </summary>
  141. public class PersonInfo : IHasProviderIds
  142. {
  143. public PersonInfo()
  144. {
  145. ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  146. }
  147. public Guid ItemId { get; set; }
  148. /// <summary>
  149. /// Gets or sets the name.
  150. /// </summary>
  151. /// <value>The name.</value>
  152. public string Name { get; set; }
  153. /// <summary>
  154. /// Gets or sets the role.
  155. /// </summary>
  156. /// <value>The role.</value>
  157. public string Role { get; set; }
  158. /// <summary>
  159. /// Gets or sets the type.
  160. /// </summary>
  161. /// <value>The type.</value>
  162. public string Type { get; set; }
  163. /// <summary>
  164. /// Gets or sets the sort order - ascending
  165. /// </summary>
  166. /// <value>The sort order.</value>
  167. public int? SortOrder { get; set; }
  168. public string ImageUrl { get; set; }
  169. public Dictionary<string, string> ProviderIds { get; set; }
  170. /// <summary>
  171. /// Returns a <see cref="System.String" /> that represents this instance.
  172. /// </summary>
  173. /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
  174. public override string ToString()
  175. {
  176. return Name;
  177. }
  178. public bool IsType(string type)
  179. {
  180. return string.Equals(Type, type, StringComparison.OrdinalIgnoreCase) || string.Equals(Role, type, StringComparison.OrdinalIgnoreCase);
  181. }
  182. }
  183. }