Playlist.cs 7.6 KB

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