Playlist.cs 7.7 KB

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