MusicGenre.cs 3.8 KB

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