MusicGenre.cs 3.7 KB

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