Playlist.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  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.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. // The path will sometimes be a directory and "Path.HasExtension" returns true if the name contains a '.' (dot).
  40. return System.IO.Path.HasExtension(path) && !Directory.Exists(path);
  41. }
  42. [JsonIgnore]
  43. public override string ContainingFolderPath
  44. {
  45. get
  46. {
  47. var path = Path;
  48. if (IsPlaylistFile(path))
  49. {
  50. return System.IO.Path.GetDirectoryName(path);
  51. }
  52. return path;
  53. }
  54. }
  55. [JsonIgnore]
  56. protected override bool FilterLinkedChildrenPerUser => true;
  57. [JsonIgnore]
  58. public override bool SupportsInheritedParentImages => false;
  59. [JsonIgnore]
  60. public override bool SupportsPlayedStatus => string.Equals(MediaType, "Video", StringComparison.OrdinalIgnoreCase);
  61. [JsonIgnore]
  62. public override bool AlwaysScanInternalMetadataPath => true;
  63. [JsonIgnore]
  64. public override bool SupportsCumulativeRunTimeTicks => true;
  65. public override double GetDefaultPrimaryImageAspectRatio()
  66. {
  67. return 1;
  68. }
  69. public override bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  70. {
  71. return true;
  72. }
  73. public override bool IsSaveLocalMetadataEnabled()
  74. {
  75. return true;
  76. }
  77. protected override List<BaseItem> LoadChildren()
  78. {
  79. // Save a trip to the database
  80. return new List<BaseItem>();
  81. }
  82. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  83. {
  84. return Task.CompletedTask;
  85. }
  86. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query)
  87. {
  88. return GetPlayableItems(user, query);
  89. }
  90. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  91. {
  92. return new List<BaseItem>();
  93. }
  94. public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  95. {
  96. return GetPlayableItems(user, query);
  97. }
  98. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
  99. {
  100. return GetLinkedChildrenInfos();
  101. }
  102. private List<BaseItem> GetPlayableItems(User user, InternalItemsQuery query)
  103. {
  104. query ??= new InternalItemsQuery(user);
  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[] { nameof(Audio) },
  130. GenreIds = new[] { musicGenre.Id },
  131. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  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[] { nameof(Audio) },
  141. ArtistIds = new[] { musicArtist.Id },
  142. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  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. }