Playlist.cs 7.9 KB

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