Playlist.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if (query == null)
  105. {
  106. query = new InternalItemsQuery(user);
  107. }
  108. query.IsFolder = false;
  109. return base.GetChildren(user, true, query);
  110. }
  111. public static List<BaseItem> GetPlaylistItems(string playlistMediaType, IEnumerable<BaseItem> inputItems, User user, DtoOptions options)
  112. {
  113. if (user != null)
  114. {
  115. inputItems = inputItems.Where(i => i.IsVisible(user));
  116. }
  117. var list = new List<BaseItem>();
  118. foreach (var item in inputItems)
  119. {
  120. var playlistItems = GetPlaylistItems(item, user, playlistMediaType, options);
  121. list.AddRange(playlistItems);
  122. }
  123. return list;
  124. }
  125. private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem item, User user, string mediaType, DtoOptions options)
  126. {
  127. if (item is MusicGenre musicGenre)
  128. {
  129. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  130. {
  131. Recursive = true,
  132. IncludeItemTypes = new[] { nameof(Audio) },
  133. GenreIds = new[] { musicGenre.Id },
  134. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  135. DtoOptions = options
  136. });
  137. }
  138. if (item is MusicArtist musicArtist)
  139. {
  140. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  141. {
  142. Recursive = true,
  143. IncludeItemTypes = new[] { nameof(Audio) },
  144. ArtistIds = new[] { musicArtist.Id },
  145. OrderBy = new[] { (ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), (ItemSortBy.SortName, SortOrder.Ascending) },
  146. DtoOptions = options
  147. });
  148. }
  149. if (item is Folder folder)
  150. {
  151. var query = new InternalItemsQuery(user)
  152. {
  153. Recursive = true,
  154. IsFolder = false,
  155. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  156. MediaTypes = new[] { mediaType },
  157. EnableTotalRecordCount = false,
  158. DtoOptions = options
  159. };
  160. return folder.GetItemList(query);
  161. }
  162. return new[] { item };
  163. }
  164. [JsonIgnore]
  165. public override bool IsPreSorted => true;
  166. public string PlaylistMediaType { get; set; }
  167. [JsonIgnore]
  168. public override string MediaType => PlaylistMediaType;
  169. public void SetMediaType(string value)
  170. {
  171. PlaylistMediaType = value;
  172. }
  173. [JsonIgnore]
  174. private bool IsSharedItem
  175. {
  176. get
  177. {
  178. var path = Path;
  179. if (string.IsNullOrEmpty(path))
  180. {
  181. return false;
  182. }
  183. return FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, path);
  184. }
  185. }
  186. public override bool IsVisible(User user)
  187. {
  188. if (!IsSharedItem)
  189. {
  190. return base.IsVisible(user);
  191. }
  192. if (user.Id == OwnerUserId)
  193. {
  194. return true;
  195. }
  196. var shares = Shares;
  197. if (shares.Length == 0)
  198. {
  199. return base.IsVisible(user);
  200. }
  201. var userId = user.Id.ToString("N", CultureInfo.InvariantCulture);
  202. return shares.Any(share => string.Equals(share.UserId, userId, StringComparison.OrdinalIgnoreCase));
  203. }
  204. public override bool IsVisibleStandalone(User user)
  205. {
  206. if (!IsSharedItem)
  207. {
  208. return base.IsVisibleStandalone(user);
  209. }
  210. return IsVisible(user);
  211. }
  212. }
  213. }