Playlist.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. //When a directory contains a `.`, calling "HasExtension" will return true, even if that location is a directory that exists.
  41. //This kills the PlaylistXmlSaver, because instead of saving in "config/data/playlists/MyList2.0/playlist.xml",
  42. //It saves the information in "config/data/playlists/MyList2.xml". So we just need to see if it's actually a real directory first.
  43. //Lucky for us, when a new playlist is created, the directory on the drive is created first, then the Playlist object is created.
  44. //And if there's not a directory there, then we can just check if it has an extension.
  45. if (new System.IO.DirectoryInfo(path).Exists)
  46. { //This is a directory, and therefore definitely not a playlist file.
  47. return false;
  48. }
  49. //Well, there's no directory there, so if it /looks/ like a file path, then it probably is.
  50. return System.IO.Path.HasExtension(path);
  51. }
  52. [JsonIgnore]
  53. public override string ContainingFolderPath
  54. {
  55. get
  56. {
  57. var path = Path;
  58. if (IsPlaylistFile(path))
  59. {
  60. return System.IO.Path.GetDirectoryName(path);
  61. }
  62. return path;
  63. }
  64. }
  65. [JsonIgnore]
  66. protected override bool FilterLinkedChildrenPerUser => true;
  67. [JsonIgnore]
  68. public override bool SupportsInheritedParentImages => false;
  69. [JsonIgnore]
  70. public override bool SupportsPlayedStatus => string.Equals(MediaType, "Video", StringComparison.OrdinalIgnoreCase);
  71. [JsonIgnore]
  72. public override bool AlwaysScanInternalMetadataPath => true;
  73. [JsonIgnore]
  74. public override bool SupportsCumulativeRunTimeTicks => true;
  75. public override double GetDefaultPrimaryImageAspectRatio()
  76. {
  77. return 1;
  78. }
  79. public override bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders)
  80. {
  81. return true;
  82. }
  83. public override bool IsSaveLocalMetadataEnabled()
  84. {
  85. return true;
  86. }
  87. protected override List<BaseItem> LoadChildren()
  88. {
  89. // Save a trip to the database
  90. return new List<BaseItem>();
  91. }
  92. protected override Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  93. {
  94. return Task.CompletedTask;
  95. }
  96. public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query)
  97. {
  98. return GetPlayableItems(user, query);
  99. }
  100. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  101. {
  102. return new List<BaseItem>();
  103. }
  104. public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  105. {
  106. return GetPlayableItems(user, query);
  107. }
  108. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
  109. {
  110. return GetLinkedChildrenInfos();
  111. }
  112. private List<BaseItem> GetPlayableItems(User user, InternalItemsQuery query)
  113. {
  114. if (query == null)
  115. {
  116. query = new InternalItemsQuery(user);
  117. }
  118. query.IsFolder = false;
  119. return base.GetChildren(user, true, query);
  120. }
  121. public static List<BaseItem> GetPlaylistItems(string playlistMediaType, IEnumerable<BaseItem> inputItems, User user, DtoOptions options)
  122. {
  123. if (user != null)
  124. {
  125. inputItems = inputItems.Where(i => i.IsVisible(user));
  126. }
  127. var list = new List<BaseItem>();
  128. foreach (var item in inputItems)
  129. {
  130. var playlistItems = GetPlaylistItems(item, user, playlistMediaType, options);
  131. list.AddRange(playlistItems);
  132. }
  133. return list;
  134. }
  135. private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem item, User user, string mediaType, DtoOptions options)
  136. {
  137. if (item is MusicGenre musicGenre)
  138. {
  139. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  140. {
  141. Recursive = true,
  142. IncludeItemTypes = new[] { nameof(Audio) },
  143. GenreIds = new[] { musicGenre.Id },
  144. OrderBy = new[] { ItemSortBy.AlbumArtist, ItemSortBy.Album, ItemSortBy.SortName }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
  145. DtoOptions = options
  146. });
  147. }
  148. if (item is MusicArtist musicArtist)
  149. {
  150. return LibraryManager.GetItemList(new InternalItemsQuery(user)
  151. {
  152. Recursive = true,
  153. IncludeItemTypes = new[] { nameof(Audio) },
  154. ArtistIds = new[] { musicArtist.Id },
  155. OrderBy = new[] { ItemSortBy.AlbumArtist, ItemSortBy.Album, ItemSortBy.SortName }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
  156. DtoOptions = options
  157. });
  158. }
  159. if (item is Folder folder)
  160. {
  161. var query = new InternalItemsQuery(user)
  162. {
  163. Recursive = true,
  164. IsFolder = false,
  165. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  166. MediaTypes = new[] { mediaType },
  167. EnableTotalRecordCount = false,
  168. DtoOptions = options
  169. };
  170. return folder.GetItemList(query);
  171. }
  172. return new[] { item };
  173. }
  174. [JsonIgnore]
  175. public override bool IsPreSorted => true;
  176. public string PlaylistMediaType { get; set; }
  177. [JsonIgnore]
  178. public override string MediaType => PlaylistMediaType;
  179. public void SetMediaType(string value)
  180. {
  181. PlaylistMediaType = value;
  182. }
  183. [JsonIgnore]
  184. private bool IsSharedItem
  185. {
  186. get
  187. {
  188. var path = Path;
  189. if (string.IsNullOrEmpty(path))
  190. {
  191. return false;
  192. }
  193. return FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, path);
  194. }
  195. }
  196. public override bool IsVisible(User user)
  197. {
  198. if (!IsSharedItem)
  199. {
  200. return base.IsVisible(user);
  201. }
  202. if (user.Id == OwnerUserId)
  203. {
  204. return true;
  205. }
  206. var shares = Shares;
  207. if (shares.Length == 0)
  208. {
  209. return base.IsVisible(user);
  210. }
  211. var userId = user.Id.ToString("N", CultureInfo.InvariantCulture);
  212. return shares.Any(share => string.Equals(share.UserId, userId, StringComparison.OrdinalIgnoreCase));
  213. }
  214. public override bool IsVisibleStandalone(User user)
  215. {
  216. if (!IsSharedItem)
  217. {
  218. return base.IsVisibleStandalone(user);
  219. }
  220. return IsVisible(user);
  221. }
  222. }
  223. }