Genre.cs 3.7 KB

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