Playlist.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text.Json.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  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.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. }
  34. public Guid OwnerUserId { get; set; }
  35. public Share[] Shares { get; set; }
  36. [JsonIgnore]
  37. public bool IsFile => IsPlaylistFile(Path);
  38. [JsonIgnore]
  39. public override string ContainingFolderPath
  40. {
  41. get
  42. {
  43. var path = Path;
  44. if (IsPlaylistFile(path))
  45. {
  46. return System.IO.Path.GetDirectoryName(path);
  47. }
  48. return path;
  49. }
  50. }
  51. [JsonIgnore]
  52. protected override bool FilterLinkedChildrenPerUser => true;
  53. [JsonIgnore]
  54. public override bool SupportsInheritedParentImages => false;
  55. [JsonIgnore]
  56. public override bool SupportsPlayedStatus => string.Equals(MediaType, "Video", StringComparison.OrdinalIgnoreCase);
  57. [JsonIgnore]
  58. public override bool AlwaysScanInternalMetadataPath => true;
  59. [JsonIgnore]
  60. public override bool SupportsCumulativeRunTimeTicks => true;
  61. [JsonIgnore]
  62. public override bool IsPreSorted => true;
  63. public string PlaylistMediaType { get; set; }
  64. [JsonIgnore]
  65. public override string MediaType => PlaylistMediaType;
  66. [JsonIgnore]
  67. private bool IsSharedItem
  68. {
  69. get
  70. {
  71. var path = Path;
  72. if (string.IsNullOrEmpty(path))
  73. {
  74. return false;
  75. }
  76. return FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, path);
  77. }
  78. }
  79. public static bool IsPlaylistFile(string path)
  80. {
  81. // The path will sometimes be a directory and "Path.HasExtension" returns true if the name contains a '.' (dot).
  82. return System.IO.Path.HasExtension(path) && !Directory.Exists(path);
  83. }
  84. public void SetMediaType(string value)
  85. {
  86. PlaylistMediaType = value;
  87. }
  88. public override double GetDefaultPrimaryImageAspectRatio()
  89. {
  90. return 1;
  91. }
  92. public override bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  93. {
  94. return true;
  95. }
  96. public override bool IsSaveLocalMetadataEnabled()
  97. {
  98. return true;
  99. }
  100. protected override List<BaseItem> LoadChildren()
  101. {
  102. // Save a trip to the database
  103. return new List<BaseItem>();
  104. }
  105. protected override Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  106. {
  107. return Task.CompletedTask;
  108. }
  109. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query)
  110. {
  111. return GetPlayableItems(user, query);
  112. }
  113. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  114. {
  115. return new List<BaseItem>();
  116. }
  117. public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  118. {
  119. return GetPlayableItems(user, query);
  120. }
  121. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
  122. {
  123. return GetLinkedChildrenInfos();
  124. }
  125. private List<BaseItem> GetPlayableItems(User user, InternalItemsQuery query)
  126. {
  127. query ??= new InternalItemsQuery(user);
  128. query.IsFolder = false;
  129. return base.GetChildren(user, true, query);
  130. }
  131. public static List<BaseItem> GetPlaylistItems(string playlistMediaType, IEnumerable<BaseItem> inputItems, User user, DtoOptions options)
  132. {
  133. if (user != null)
  134. {
  135. inputItems = inputItems.Where(i => i.IsVisible(user));
  136. }
  137. var list = new List<BaseItem>();
  138. foreach (var item in inputItems)
  139. {
  140. var playlistItems = GetPlaylistItems(item, user, playlistMediaType, options);
  141. list.AddRange(playlistItems);
  142. }
  143. return list;
  144. }
  145. private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem item, User user, string mediaType, DtoOptions options)
  146. {
  147. if (item is MusicGenre musicGenre)
  148. {
  149. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  150. {
  151. Recursive = true,
  152. IncludeItemTypes = new[] { BaseItemKind.Audio },
  153. GenreIds = new[] { musicGenre.Id },
  154. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  155. DtoOptions = options
  156. });
  157. }
  158. if (item is MusicArtist musicArtist)
  159. {
  160. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  161. {
  162. Recursive = true,
  163. IncludeItemTypes = new[] { BaseItemKind.Audio },
  164. ArtistIds = new[] { musicArtist.Id },
  165. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  166. DtoOptions = options
  167. });
  168. }
  169. if (item is Folder folder)
  170. {
  171. var query = new InternalItemsQuery(user)
  172. {
  173. Recursive = true,
  174. IsFolder = false,
  175. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  176. MediaTypes = new[] { mediaType },
  177. EnableTotalRecordCount = false,
  178. DtoOptions = options
  179. };
  180. return folder.GetItemList(query);
  181. }
  182. return new[] { item };
  183. }
  184. public override bool IsVisible(User user)
  185. {
  186. if (!IsSharedItem)
  187. {
  188. return base.IsVisible(user);
  189. }
  190. if (user.Id.Equals(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;
  200. return shares.Any(share => Guid.TryParse(share.UserId, out var id) && id.Equals(userId));
  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. }