2
0

Playlist.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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 Playlist()
  31. {
  32. Shares = Array.Empty<Share>();
  33. OpenAccess = false;
  34. }
  35. public Guid OwnerUserId { get; set; }
  36. public bool OpenAccess { get; set; }
  37. public Share[] 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 => string.Equals(MediaType, "Video", StringComparison.OrdinalIgnoreCase);
  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 string PlaylistMediaType { get; set; }
  66. [JsonIgnore]
  67. public override string 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(string value)
  87. {
  88. PlaylistMediaType = value;
  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 new List<BaseItem>();
  106. }
  107. protected override Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  108. {
  109. return Task.CompletedTask;
  110. }
  111. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query)
  112. {
  113. return GetPlayableItems(user, query);
  114. }
  115. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  116. {
  117. return new List<BaseItem>();
  118. }
  119. public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  120. {
  121. return GetPlayableItems(user, query);
  122. }
  123. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
  124. {
  125. return GetLinkedChildrenInfos();
  126. }
  127. private List<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 List<BaseItem> GetPlaylistItems(string playlistMediaType, 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, playlistMediaType, options);
  143. list.AddRange(playlistItems);
  144. }
  145. return list;
  146. }
  147. private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem item, User user, string mediaType, DtoOptions options)
  148. {
  149. if (item is MusicGenre musicGenre)
  150. {
  151. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  152. {
  153. Recursive = true,
  154. IncludeItemTypes = new[] { BaseItemKind.Audio },
  155. GenreIds = new[] { musicGenre.Id },
  156. OrderBy = new[] { (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 = new[] { BaseItemKind.Audio },
  166. ArtistIds = new[] { musicArtist.Id },
  167. OrderBy = new[] { (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. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  178. MediaTypes = new[] { mediaType },
  179. EnableTotalRecordCount = false,
  180. DtoOptions = options
  181. };
  182. return folder.GetItemList(query);
  183. }
  184. return new[] { item };
  185. }
  186. public override bool IsVisible(User user)
  187. {
  188. if (!IsSharedItem)
  189. {
  190. return base.IsVisible(user);
  191. }
  192. if (OpenAccess)
  193. {
  194. return true;
  195. }
  196. var userId = user.Id;
  197. if (userId.Equals(OwnerUserId))
  198. {
  199. return true;
  200. }
  201. var shares = Shares;
  202. if (shares.Length == 0)
  203. {
  204. return false;
  205. }
  206. return shares.Any(share => Guid.TryParse(share.UserId, out var id) && id.Equals(userId));
  207. }
  208. public override bool IsVisibleStandalone(User user)
  209. {
  210. if (!IsSharedItem)
  211. {
  212. return base.IsVisibleStandalone(user);
  213. }
  214. return IsVisible(user);
  215. }
  216. }
  217. }