Playlist.cs 7.7 KB

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