Playlist.cs 7.8 KB

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