Playlist.cs 7.6 KB

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