Playlist.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text.Json.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Jellyfin.Data.Entities;
  12. using Jellyfin.Data.Enums;
  13. using MediaBrowser.Controller.Dto;
  14. using MediaBrowser.Controller.Entities;
  15. using MediaBrowser.Controller.Entities.Audio;
  16. using MediaBrowser.Controller.Providers;
  17. using MediaBrowser.Model.Querying;
  18. namespace MediaBrowser.Controller.Playlists
  19. {
  20. public class Playlist : Folder, IHasShares
  21. {
  22. public static readonly IReadOnlyList<string> SupportedExtensions = new[]
  23. {
  24. ".m3u",
  25. ".m3u8",
  26. ".pls",
  27. ".wpl",
  28. ".zpl"
  29. };
  30. public Guid OwnerUserId { get; set; }
  31. public Share[] Shares { get; set; }
  32. public Playlist()
  33. {
  34. Shares = Array.Empty<Share>();
  35. }
  36. [JsonIgnore]
  37. public bool IsFile => IsPlaylistFile(Path);
  38. public static bool IsPlaylistFile(string path)
  39. {
  40. // The path will sometimes be a directory and "Path.HasExtension" returns true if the name contains a '.' (dot).
  41. return System.IO.Path.HasExtension(path) && !Directory.Exists(path);
  42. }
  43. [JsonIgnore]
  44. public override string ContainingFolderPath
  45. {
  46. get
  47. {
  48. var path = Path;
  49. if (IsPlaylistFile(path))
  50. {
  51. return System.IO.Path.GetDirectoryName(path);
  52. }
  53. return path;
  54. }
  55. }
  56. [JsonIgnore]
  57. protected override bool FilterLinkedChildrenPerUser => true;
  58. [JsonIgnore]
  59. public override bool SupportsInheritedParentImages => false;
  60. [JsonIgnore]
  61. public override bool SupportsPlayedStatus => string.Equals(MediaType, "Video", StringComparison.OrdinalIgnoreCase);
  62. [JsonIgnore]
  63. public override bool AlwaysScanInternalMetadataPath => true;
  64. [JsonIgnore]
  65. public override bool SupportsCumulativeRunTimeTicks => true;
  66. public override double GetDefaultPrimaryImageAspectRatio()
  67. {
  68. return 1;
  69. }
  70. public override bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  71. {
  72. return true;
  73. }
  74. public override bool IsSaveLocalMetadataEnabled()
  75. {
  76. return true;
  77. }
  78. protected override List<BaseItem> LoadChildren()
  79. {
  80. // Save a trip to the database
  81. return new List<BaseItem>();
  82. }
  83. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  84. {
  85. return Task.CompletedTask;
  86. }
  87. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query)
  88. {
  89. return GetPlayableItems(user, query);
  90. }
  91. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  92. {
  93. return new List<BaseItem>();
  94. }
  95. public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  96. {
  97. return GetPlayableItems(user, query);
  98. }
  99. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
  100. {
  101. return GetLinkedChildrenInfos();
  102. }
  103. private List<BaseItem> GetPlayableItems(User user, InternalItemsQuery query)
  104. {
  105. query ??= new InternalItemsQuery(user);
  106. query.IsFolder = false;
  107. return base.GetChildren(user, true, query);
  108. }
  109. public static List<BaseItem> GetPlaylistItems(string playlistMediaType, IEnumerable<BaseItem> inputItems, User user, DtoOptions options)
  110. {
  111. if (user != null)
  112. {
  113. inputItems = inputItems.Where(i => i.IsVisible(user));
  114. }
  115. var list = new List<BaseItem>();
  116. foreach (var item in inputItems)
  117. {
  118. var playlistItems = GetPlaylistItems(item, user, playlistMediaType, options);
  119. list.AddRange(playlistItems);
  120. }
  121. return list;
  122. }
  123. private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem item, User user, string mediaType, DtoOptions options)
  124. {
  125. if (item is MusicGenre musicGenre)
  126. {
  127. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  128. {
  129. Recursive = true,
  130. IncludeItemTypes = new[] { nameof(Audio) },
  131. GenreIds = new[] { musicGenre.Id },
  132. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  133. DtoOptions = options
  134. });
  135. }
  136. if (item is MusicArtist musicArtist)
  137. {
  138. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  139. {
  140. Recursive = true,
  141. IncludeItemTypes = new[] { nameof(Audio) },
  142. ArtistIds = new[] { musicArtist.Id },
  143. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  144. DtoOptions = options
  145. });
  146. }
  147. if (item is Folder folder)
  148. {
  149. var query = new InternalItemsQuery(user)
  150. {
  151. Recursive = true,
  152. IsFolder = false,
  153. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  154. MediaTypes = new[] { mediaType },
  155. EnableTotalRecordCount = false,
  156. DtoOptions = options
  157. };
  158. return folder.GetItemList(query);
  159. }
  160. return new[] { item };
  161. }
  162. [JsonIgnore]
  163. public override bool IsPreSorted => true;
  164. public string PlaylistMediaType { get; set; }
  165. [JsonIgnore]
  166. public override string MediaType => PlaylistMediaType;
  167. public void SetMediaType(string value)
  168. {
  169. PlaylistMediaType = value;
  170. }
  171. [JsonIgnore]
  172. private bool IsSharedItem
  173. {
  174. get
  175. {
  176. var path = Path;
  177. if (string.IsNullOrEmpty(path))
  178. {
  179. return false;
  180. }
  181. return FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, path);
  182. }
  183. }
  184. public override bool IsVisible(User user)
  185. {
  186. if (!IsSharedItem)
  187. {
  188. return base.IsVisible(user);
  189. }
  190. if (user.Id == OwnerUserId)
  191. {
  192. return true;
  193. }
  194. var shares = Shares;
  195. if (shares.Length == 0)
  196. {
  197. return base.IsVisible(user);
  198. }
  199. var userId = user.Id.ToString("N", CultureInfo.InvariantCulture);
  200. return shares.Any(share => string.Equals(share.UserId, userId, StringComparison.OrdinalIgnoreCase));
  201. }
  202. public override bool IsVisibleStandalone(User user)
  203. {
  204. if (!IsSharedItem)
  205. {
  206. return base.IsVisibleStandalone(user);
  207. }
  208. return IsVisible(user);
  209. }
  210. }
  211. }