| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CS1591 |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.IO; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Text.Json.Serialization; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Jellyfin.Data.Entities; |
| | 13 | | using Jellyfin.Data.Enums; |
| | 14 | | using MediaBrowser.Controller.Dto; |
| | 15 | | using MediaBrowser.Controller.Entities; |
| | 16 | | using MediaBrowser.Controller.Entities.Audio; |
| | 17 | | using MediaBrowser.Controller.Providers; |
| | 18 | | using MediaBrowser.Model.Entities; |
| | 19 | |
|
| | 20 | | namespace MediaBrowser.Controller.Playlists |
| | 21 | | { |
| | 22 | | public class Playlist : Folder, IHasShares |
| | 23 | | { |
| 1 | 24 | | public static readonly IReadOnlyList<string> SupportedExtensions = |
| 1 | 25 | | [ |
| 1 | 26 | | ".m3u", |
| 1 | 27 | | ".m3u8", |
| 1 | 28 | | ".pls", |
| 1 | 29 | | ".wpl", |
| 1 | 30 | | ".zpl" |
| 1 | 31 | | ]; |
| | 32 | |
|
| 1 | 33 | | public Playlist() |
| | 34 | | { |
| 1 | 35 | | Shares = []; |
| 1 | 36 | | OpenAccess = false; |
| 1 | 37 | | } |
| | 38 | |
|
| | 39 | | public Guid OwnerUserId { get; set; } |
| | 40 | |
|
| | 41 | | public bool OpenAccess { get; set; } |
| | 42 | |
|
| | 43 | | public IReadOnlyList<PlaylistUserPermissions> Shares { get; set; } |
| | 44 | |
|
| | 45 | | [JsonIgnore] |
| 0 | 46 | | public bool IsFile => IsPlaylistFile(Path); |
| | 47 | |
|
| | 48 | | [JsonIgnore] |
| | 49 | | public override string ContainingFolderPath |
| | 50 | | { |
| | 51 | | get |
| | 52 | | { |
| 0 | 53 | | var path = Path; |
| | 54 | |
|
| 0 | 55 | | if (IsPlaylistFile(path)) |
| | 56 | | { |
| 0 | 57 | | return System.IO.Path.GetDirectoryName(path); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | return path; |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | [JsonIgnore] |
| 0 | 65 | | protected override bool FilterLinkedChildrenPerUser => true; |
| | 66 | |
|
| | 67 | | [JsonIgnore] |
| 0 | 68 | | public override bool SupportsInheritedParentImages => false; |
| | 69 | |
|
| | 70 | | [JsonIgnore] |
| 0 | 71 | | public override bool SupportsPlayedStatus => MediaType == Jellyfin.Data.Enums.MediaType.Video; |
| | 72 | |
|
| | 73 | | [JsonIgnore] |
| 0 | 74 | | public override bool AlwaysScanInternalMetadataPath => true; |
| | 75 | |
|
| | 76 | | [JsonIgnore] |
| 0 | 77 | | public override bool SupportsCumulativeRunTimeTicks => true; |
| | 78 | |
|
| | 79 | | [JsonIgnore] |
| 0 | 80 | | public override bool IsPreSorted => true; |
| | 81 | |
|
| | 82 | | public MediaType PlaylistMediaType { get; set; } |
| | 83 | |
|
| | 84 | | [JsonIgnore] |
| 0 | 85 | | public override MediaType MediaType => PlaylistMediaType; |
| | 86 | |
|
| | 87 | | [JsonIgnore] |
| | 88 | | private bool IsSharedItem |
| | 89 | | { |
| | 90 | | get |
| | 91 | | { |
| 0 | 92 | | var path = Path; |
| | 93 | |
|
| 0 | 94 | | if (string.IsNullOrEmpty(path)) |
| | 95 | | { |
| 0 | 96 | | return false; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | return FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.DataPath, path); |
| | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| | 103 | | public static bool IsPlaylistFile(string path) |
| | 104 | | { |
| | 105 | | // The path will sometimes be a directory and "Path.HasExtension" returns true if the name contains a '.' (d |
| 0 | 106 | | return System.IO.Path.HasExtension(path) && !Directory.Exists(path); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetMediaType(MediaType? value) |
| | 110 | | { |
| 0 | 111 | | PlaylistMediaType = value ?? MediaType.Unknown; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public override double GetDefaultPrimaryImageAspectRatio() |
| | 115 | | { |
| 0 | 116 | | return 1; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public override bool IsAuthorizedToDelete(User user, List<Folder> allCollectionFolders) |
| | 120 | | { |
| 0 | 121 | | return true; |
| | 122 | | } |
| | 123 | |
|
| | 124 | | public override bool IsSaveLocalMetadataEnabled() |
| | 125 | | { |
| 0 | 126 | | return true; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | protected override List<BaseItem> LoadChildren() |
| | 130 | | { |
| | 131 | | // Save a trip to the database |
| 0 | 132 | | return []; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | protected override Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMe |
| | 136 | | { |
| 0 | 137 | | return Task.CompletedTask; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | public override List<BaseItem> GetChildren(User user, bool includeLinkedChildren, InternalItemsQuery query) |
| | 141 | | { |
| 0 | 142 | | return GetPlayableItems(user, query); |
| | 143 | | } |
| | 144 | |
|
| | 145 | | protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService) |
| | 146 | | { |
| 0 | 147 | | return []; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query) |
| | 151 | | { |
| 0 | 152 | | return GetPlayableItems(user, query); |
| | 153 | | } |
| | 154 | |
|
| | 155 | | public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems() |
| | 156 | | { |
| 0 | 157 | | return GetLinkedChildrenInfos(); |
| | 158 | | } |
| | 159 | |
|
| | 160 | | private List<BaseItem> GetPlayableItems(User user, InternalItemsQuery query) |
| | 161 | | { |
| 0 | 162 | | query ??= new InternalItemsQuery(user); |
| | 163 | |
|
| 0 | 164 | | query.IsFolder = false; |
| | 165 | |
|
| 0 | 166 | | return base.GetChildren(user, true, query); |
| | 167 | | } |
| | 168 | |
|
| | 169 | | public static IReadOnlyList<BaseItem> GetPlaylistItems(IEnumerable<BaseItem> inputItems, User user, DtoOptions o |
| | 170 | | { |
| 0 | 171 | | if (user is not null) |
| | 172 | | { |
| 0 | 173 | | inputItems = inputItems.Where(i => i.IsVisible(user)); |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | var list = new List<BaseItem>(); |
| | 177 | |
|
| 0 | 178 | | foreach (var item in inputItems) |
| | 179 | | { |
| 0 | 180 | | var playlistItems = GetPlaylistItems(item, user, options); |
| 0 | 181 | | list.AddRange(playlistItems); |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | return list; |
| | 185 | | } |
| | 186 | |
|
| | 187 | | private static IEnumerable<BaseItem> GetPlaylistItems(BaseItem item, User user, DtoOptions options) |
| | 188 | | { |
| 0 | 189 | | if (item is MusicGenre musicGenre) |
| | 190 | | { |
| 0 | 191 | | return LibraryManager.GetItemList(new InternalItemsQuery(user) |
| 0 | 192 | | { |
| 0 | 193 | | Recursive = true, |
| 0 | 194 | | IncludeItemTypes = [BaseItemKind.Audio], |
| 0 | 195 | | GenreIds = [musicGenre.Id], |
| 0 | 196 | | OrderBy = [(ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), ( |
| 0 | 197 | | DtoOptions = options |
| 0 | 198 | | }); |
| | 199 | | } |
| | 200 | |
|
| 0 | 201 | | if (item is MusicArtist musicArtist) |
| | 202 | | { |
| 0 | 203 | | return LibraryManager.GetItemList(new InternalItemsQuery(user) |
| 0 | 204 | | { |
| 0 | 205 | | Recursive = true, |
| 0 | 206 | | IncludeItemTypes = [BaseItemKind.Audio], |
| 0 | 207 | | ArtistIds = [musicArtist.Id], |
| 0 | 208 | | OrderBy = [(ItemSortBy.AlbumArtist, SortOrder.Ascending), (ItemSortBy.Album, SortOrder.Ascending), ( |
| 0 | 209 | | DtoOptions = options |
| 0 | 210 | | }); |
| | 211 | | } |
| | 212 | |
|
| 0 | 213 | | if (item is Folder folder) |
| | 214 | | { |
| 0 | 215 | | var query = new InternalItemsQuery(user) |
| 0 | 216 | | { |
| 0 | 217 | | Recursive = true, |
| 0 | 218 | | IsFolder = false, |
| 0 | 219 | | MediaTypes = [MediaType.Audio, MediaType.Video], |
| 0 | 220 | | EnableTotalRecordCount = false, |
| 0 | 221 | | DtoOptions = options |
| 0 | 222 | | }; |
| | 223 | |
|
| 0 | 224 | | return folder.GetItemList(query); |
| | 225 | | } |
| | 226 | |
|
| 0 | 227 | | return [item]; |
| | 228 | | } |
| | 229 | |
|
| | 230 | | public override bool IsVisible(User user) |
| | 231 | | { |
| 0 | 232 | | if (!IsSharedItem) |
| | 233 | | { |
| 0 | 234 | | return base.IsVisible(user); |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | if (OpenAccess) |
| | 238 | | { |
| 0 | 239 | | return true; |
| | 240 | | } |
| | 241 | |
|
| 0 | 242 | | var userId = user.Id; |
| 0 | 243 | | if (userId.Equals(OwnerUserId)) |
| | 244 | | { |
| 0 | 245 | | return true; |
| | 246 | | } |
| | 247 | |
|
| 0 | 248 | | var shares = Shares; |
| 0 | 249 | | if (shares.Count == 0) |
| | 250 | | { |
| 0 | 251 | | return false; |
| | 252 | | } |
| | 253 | |
|
| 0 | 254 | | return shares.Any(s => s.UserId.Equals(userId)); |
| | 255 | | } |
| | 256 | |
|
| | 257 | | public override bool CanDelete(User user) |
| | 258 | | { |
| 0 | 259 | | return user.HasPermission(PermissionKind.IsAdministrator) || user.Id.Equals(OwnerUserId); |
| | 260 | | } |
| | 261 | |
|
| | 262 | | public override bool IsVisibleStandalone(User user) |
| | 263 | | { |
| 0 | 264 | | if (!IsSharedItem) |
| | 265 | | { |
| 0 | 266 | | return base.IsVisibleStandalone(user); |
| | 267 | | } |
| | 268 | |
|
| 0 | 269 | | return IsVisible(user); |
| | 270 | | } |
| | 271 | | } |
| | 272 | | } |