Playlist.cs 7.7 KB

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