Playlist.cs 7.8 KB

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