Genre.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.Json.Serialization;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Extensions;
  8. using Microsoft.Extensions.Logging;
  9. namespace MediaBrowser.Controller.Entities
  10. {
  11. /// <summary>
  12. /// Class Genre.
  13. /// </summary>
  14. public class Genre : BaseItem, IItemByName
  15. {
  16. /// <summary>
  17. /// Gets the folder containing the item.
  18. /// If the item is a folder, it returns the folder itself.
  19. /// </summary>
  20. /// <value>The containing folder path.</value>
  21. [JsonIgnore]
  22. public override string ContainingFolderPath => Path;
  23. [JsonIgnore]
  24. public override bool IsDisplayedAsFolder => true;
  25. [JsonIgnore]
  26. public override bool SupportsAncestors => false;
  27. [JsonIgnore]
  28. public override bool SupportsPeople => false;
  29. public override List<string> GetUserDataKeys()
  30. {
  31. var list = base.GetUserDataKeys();
  32. list.Insert(0, GetType().Name + "-" + (Name ?? string.Empty).RemoveDiacritics());
  33. return list;
  34. }
  35. public override string CreatePresentationUniqueKey()
  36. {
  37. return GetUserDataKeys()[0];
  38. }
  39. public override double GetDefaultPrimaryImageAspectRatio()
  40. {
  41. return 1;
  42. }
  43. public override bool IsSaveLocalMetadataEnabled()
  44. {
  45. return true;
  46. }
  47. public override bool CanDelete()
  48. {
  49. return false;
  50. }
  51. public IList<BaseItem> GetTaggedItems(InternalItemsQuery query)
  52. {
  53. query.GenreIds = new[] { Id };
  54. query.ExcludeItemTypes = new[]
  55. {
  56. BaseItemKind.MusicVideo,
  57. BaseItemKind.Audio,
  58. BaseItemKind.MusicAlbum,
  59. BaseItemKind.MusicArtist
  60. };
  61. return LibraryManager.GetItemList(query);
  62. }
  63. public static string GetPath(string name)
  64. {
  65. return GetPath(name, true);
  66. }
  67. public static string GetPath(string name, bool normalizeName)
  68. {
  69. // Trim the period at the end because windows will have a hard time with that
  70. var validName = normalizeName ?
  71. FileSystem.GetValidFilename(name).Trim().TrimEnd('.') :
  72. name;
  73. return System.IO.Path.Combine(ConfigurationManager.ApplicationPaths.GenrePath, validName);
  74. }
  75. private string GetRebasedPath()
  76. {
  77. return GetPath(System.IO.Path.GetFileName(Path), false);
  78. }
  79. public override bool RequiresRefresh()
  80. {
  81. var newPath = GetRebasedPath();
  82. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  83. {
  84. Logger.LogDebug("{0} path has changed from {1} to {2}", GetType().Name, Path, newPath);
  85. return true;
  86. }
  87. return base.RequiresRefresh();
  88. }
  89. /// <summary>
  90. /// This is called before any metadata refresh and returns true if changes were made.
  91. /// </summary>
  92. /// <param name="replaceAllMetadata">Whether to replace all metadata.</param>
  93. /// <returns>true if the item has change, else false.</returns>
  94. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  95. {
  96. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  97. var newPath = GetRebasedPath();
  98. if (!string.Equals(Path, newPath, StringComparison.Ordinal))
  99. {
  100. Path = newPath;
  101. hasChanges = true;
  102. }
  103. return hasChanges;
  104. }
  105. }
  106. }