| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CS1591 |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.IO; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Jellyfin.Data.Enums; |
| | 13 | | using Jellyfin.Database.Implementations.Entities; |
| | 14 | | using Jellyfin.Extensions; |
| | 15 | | using MediaBrowser.Controller.Dto; |
| | 16 | | using MediaBrowser.Controller.Entities; |
| | 17 | | using MediaBrowser.Controller.Entities.Audio; |
| | 18 | | using MediaBrowser.Controller.Extensions; |
| | 19 | | using MediaBrowser.Controller.Library; |
| | 20 | | using MediaBrowser.Controller.Playlists; |
| | 21 | | using MediaBrowser.Controller.Providers; |
| | 22 | | using MediaBrowser.Model.Entities; |
| | 23 | | using MediaBrowser.Model.IO; |
| | 24 | | using MediaBrowser.Model.Playlists; |
| | 25 | | using Microsoft.Extensions.Configuration; |
| | 26 | | using Microsoft.Extensions.Logging; |
| | 27 | | using PlaylistsNET.Content; |
| | 28 | | using PlaylistsNET.Models; |
| | 29 | | using Genre = MediaBrowser.Controller.Entities.Genre; |
| | 30 | | using MusicAlbum = MediaBrowser.Controller.Entities.Audio.MusicAlbum; |
| | 31 | |
|
| | 32 | | namespace Emby.Server.Implementations.Playlists |
| | 33 | | { |
| | 34 | | public class PlaylistManager : IPlaylistManager |
| | 35 | | { |
| | 36 | | private readonly ILibraryManager _libraryManager; |
| | 37 | | private readonly IFileSystem _fileSystem; |
| | 38 | | private readonly ILibraryMonitor _iLibraryMonitor; |
| | 39 | | private readonly ILogger<PlaylistManager> _logger; |
| | 40 | | private readonly IUserManager _userManager; |
| | 41 | | private readonly IProviderManager _providerManager; |
| | 42 | | private readonly IConfiguration _appConfig; |
| | 43 | |
|
| | 44 | | public PlaylistManager( |
| | 45 | | ILibraryManager libraryManager, |
| | 46 | | IFileSystem fileSystem, |
| | 47 | | ILibraryMonitor iLibraryMonitor, |
| | 48 | | ILogger<PlaylistManager> logger, |
| | 49 | | IUserManager userManager, |
| | 50 | | IProviderManager providerManager, |
| | 51 | | IConfiguration appConfig) |
| | 52 | | { |
| 21 | 53 | | _libraryManager = libraryManager; |
| 21 | 54 | | _fileSystem = fileSystem; |
| 21 | 55 | | _iLibraryMonitor = iLibraryMonitor; |
| 21 | 56 | | _logger = logger; |
| 21 | 57 | | _userManager = userManager; |
| 21 | 58 | | _providerManager = providerManager; |
| 21 | 59 | | _appConfig = appConfig; |
| 21 | 60 | | } |
| | 61 | |
|
| | 62 | | public Playlist GetPlaylistForUser(Guid playlistId, Guid userId) |
| | 63 | | { |
| 0 | 64 | | return GetPlaylists(userId).Where(p => p.Id.Equals(playlistId)).FirstOrDefault(); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public IEnumerable<Playlist> GetPlaylists(Guid userId) |
| | 68 | | { |
| 0 | 69 | | var user = _userManager.GetUserById(userId); |
| 0 | 70 | | return _libraryManager.GetItemList(new InternalItemsQuery |
| 0 | 71 | | { |
| 0 | 72 | | IncludeItemTypes = [BaseItemKind.Playlist], |
| 0 | 73 | | Recursive = true, |
| 0 | 74 | | DtoOptions = new DtoOptions(false) |
| 0 | 75 | | }) |
| 0 | 76 | | .Cast<Playlist>() |
| 0 | 77 | | .Where(p => p.IsVisible(user)); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public async Task<PlaylistCreationResult> CreatePlaylist(PlaylistCreationRequest request) |
| | 81 | | { |
| | 82 | | var name = request.Name; |
| | 83 | | var folderName = _fileSystem.GetValidFilename(name); |
| | 84 | | var parentFolder = GetPlaylistsFolder(request.UserId); |
| | 85 | | if (parentFolder is null) |
| | 86 | | { |
| | 87 | | throw new ArgumentException(nameof(parentFolder)); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | if (request.MediaType is null || request.MediaType == MediaType.Unknown) |
| | 91 | | { |
| | 92 | | foreach (var itemId in request.ItemIdList) |
| | 93 | | { |
| | 94 | | var item = _libraryManager.GetItemById(itemId) ?? throw new ArgumentException("No item exists with t |
| | 95 | | if (item.MediaType != MediaType.Unknown) |
| | 96 | | { |
| | 97 | | request.MediaType = item.MediaType; |
| | 98 | | } |
| | 99 | | else if (item is MusicArtist || item is MusicAlbum || item is MusicGenre) |
| | 100 | | { |
| | 101 | | request.MediaType = MediaType.Audio; |
| | 102 | | } |
| | 103 | | else if (item is Genre) |
| | 104 | | { |
| | 105 | | request.MediaType = MediaType.Video; |
| | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| | 109 | | if (item is Folder folder) |
| | 110 | | { |
| | 111 | | request.MediaType = folder.GetRecursiveChildren(i => !i.IsFolder && i.SupportsAddingToPlayli |
| | 112 | | .Select(i => i.MediaType) |
| | 113 | | .FirstOrDefault(i => i != MediaType.Unknown); |
| | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| | 117 | | if (request.MediaType is not null && request.MediaType != MediaType.Unknown) |
| | 118 | | { |
| | 119 | | break; |
| | 120 | | } |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| | 124 | | if (request.MediaType is null || request.MediaType == MediaType.Unknown) |
| | 125 | | { |
| | 126 | | request.MediaType = MediaType.Audio; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | var user = _userManager.GetUserById(request.UserId); |
| | 130 | | var path = Path.Combine(parentFolder.Path, folderName); |
| | 131 | | path = GetTargetPath(path); |
| | 132 | |
|
| | 133 | | _iLibraryMonitor.ReportFileSystemChangeBeginning(path); |
| | 134 | |
|
| | 135 | | try |
| | 136 | | { |
| | 137 | | var info = Directory.CreateDirectory(path); |
| | 138 | | var playlist = new Playlist |
| | 139 | | { |
| | 140 | | Name = name, |
| | 141 | | Path = path, |
| | 142 | | OwnerUserId = request.UserId, |
| | 143 | | Shares = request.Users ?? [], |
| | 144 | | OpenAccess = request.Public ?? false, |
| | 145 | | DateCreated = info.CreationTimeUtc, |
| | 146 | | DateModified = info.LastWriteTimeUtc |
| | 147 | | }; |
| | 148 | |
|
| | 149 | | playlist.SetMediaType(request.MediaType); |
| | 150 | | parentFolder.AddChild(playlist); |
| | 151 | |
|
| | 152 | | await playlist.RefreshMetadata(new MetadataRefreshOptions(new DirectoryService(_fileSystem)) { ForceSave |
| | 153 | | .ConfigureAwait(false); |
| | 154 | |
|
| | 155 | | if (request.ItemIdList.Count > 0) |
| | 156 | | { |
| | 157 | | await AddToPlaylistInternal(playlist.Id, request.ItemIdList, user, new DtoOptions(false) |
| | 158 | | { |
| | 159 | | EnableImages = true |
| | 160 | | }).ConfigureAwait(false); |
| | 161 | | } |
| | 162 | |
|
| | 163 | | return new PlaylistCreationResult(playlist.Id.ToString("N", CultureInfo.InvariantCulture)); |
| | 164 | | } |
| | 165 | | finally |
| | 166 | | { |
| | 167 | | // Refresh handled internally |
| | 168 | | _iLibraryMonitor.ReportFileSystemChangeComplete(path, false); |
| | 169 | | } |
| | 170 | | } |
| | 171 | |
|
| | 172 | | private List<Playlist> GetUserPlaylists(Guid userId) |
| | 173 | | { |
| 0 | 174 | | var user = _userManager.GetUserById(userId); |
| 0 | 175 | | var playlistsFolder = GetPlaylistsFolder(userId); |
| 0 | 176 | | if (playlistsFolder is null) |
| | 177 | | { |
| 0 | 178 | | return []; |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | return playlistsFolder.GetChildren(user, true).OfType<Playlist>().ToList(); |
| | 182 | | } |
| | 183 | |
|
| | 184 | | private static string GetTargetPath(string path) |
| | 185 | | { |
| 0 | 186 | | while (Directory.Exists(path)) |
| | 187 | | { |
| 0 | 188 | | path += "1"; |
| | 189 | | } |
| | 190 | |
|
| 0 | 191 | | return path; |
| | 192 | | } |
| | 193 | |
|
| | 194 | | private IReadOnlyList<BaseItem> GetPlaylistItems(IEnumerable<Guid> itemIds, User user, DtoOptions options) |
| | 195 | | { |
| 0 | 196 | | var items = itemIds.Select(_libraryManager.GetItemById).Where(i => i is not null); |
| | 197 | |
|
| 0 | 198 | | return Playlist.GetPlaylistItems(items, user, options); |
| | 199 | | } |
| | 200 | |
|
| | 201 | | public Task AddItemToPlaylistAsync(Guid playlistId, IReadOnlyCollection<Guid> itemIds, Guid userId) |
| | 202 | | { |
| 0 | 203 | | var user = userId.IsEmpty() ? null : _userManager.GetUserById(userId); |
| | 204 | |
|
| 0 | 205 | | return AddToPlaylistInternal(playlistId, itemIds, user, new DtoOptions(false) |
| 0 | 206 | | { |
| 0 | 207 | | EnableImages = true |
| 0 | 208 | | }); |
| | 209 | | } |
| | 210 | |
|
| | 211 | | private async Task AddToPlaylistInternal(Guid playlistId, IReadOnlyCollection<Guid> newItemIds, User user, DtoOp |
| | 212 | | { |
| | 213 | | // Retrieve the existing playlist |
| | 214 | | var playlist = _libraryManager.GetItemById(playlistId) as Playlist |
| | 215 | | ?? throw new ArgumentException("No Playlist exists with Id " + playlistId); |
| | 216 | |
|
| | 217 | | // Retrieve all the items to be added to the playlist |
| | 218 | | var newItems = GetPlaylistItems(newItemIds, user, options) |
| | 219 | | .Where(i => i.SupportsAddingToPlaylist); |
| | 220 | |
|
| | 221 | | // Filter out duplicate items |
| | 222 | | var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet(); |
| | 223 | | newItems = newItems |
| | 224 | | .Where(i => !existingIds.Contains(i.Id)) |
| | 225 | | .Distinct(); |
| | 226 | |
|
| | 227 | | // Create a list of the new linked children to add to the playlist |
| | 228 | | var childrenToAdd = newItems |
| | 229 | | .Select(LinkedChild.Create) |
| | 230 | | .ToList(); |
| | 231 | |
|
| | 232 | | // Log duplicates that have been ignored, if any |
| | 233 | | int numDuplicates = newItemIds.Count - childrenToAdd.Count; |
| | 234 | | if (numDuplicates > 0) |
| | 235 | | { |
| | 236 | | _logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDup |
| | 237 | | } |
| | 238 | |
|
| | 239 | | // Do nothing else if there are no items to add to the playlist |
| | 240 | | if (childrenToAdd.Count == 0) |
| | 241 | | { |
| | 242 | | return; |
| | 243 | | } |
| | 244 | |
|
| | 245 | | // Update the playlist in the repository |
| | 246 | | playlist.LinkedChildren = [.. playlist.LinkedChildren, .. childrenToAdd]; |
| | 247 | |
|
| | 248 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 249 | |
|
| | 250 | | // Refresh playlist metadata |
| | 251 | | _providerManager.QueueRefresh( |
| | 252 | | playlist.Id, |
| | 253 | | new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | 254 | | { |
| | 255 | | ForceSave = true |
| | 256 | | }, |
| | 257 | | RefreshPriority.High); |
| | 258 | | } |
| | 259 | |
|
| | 260 | | public async Task RemoveItemFromPlaylistAsync(string playlistId, IEnumerable<string> entryIds) |
| | 261 | | { |
| | 262 | | if (_libraryManager.GetItemById(playlistId) is not Playlist playlist) |
| | 263 | | { |
| | 264 | | throw new ArgumentException("No Playlist exists with the supplied Id"); |
| | 265 | | } |
| | 266 | |
|
| | 267 | | var children = playlist.GetManageableItems().ToList(); |
| | 268 | |
|
| | 269 | | var idList = entryIds.ToList(); |
| | 270 | |
|
| | 271 | | var removals = children.Where(i => idList.Contains(i.Item1.ItemId?.ToString("N", CultureInfo.InvariantCultur |
| | 272 | |
|
| | 273 | | playlist.LinkedChildren = children.Except(removals) |
| | 274 | | .Select(i => i.Item1) |
| | 275 | | .ToArray(); |
| | 276 | |
|
| | 277 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 278 | |
|
| | 279 | | _providerManager.QueueRefresh( |
| | 280 | | playlist.Id, |
| | 281 | | new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
| | 282 | | { |
| | 283 | | ForceSave = true |
| | 284 | | }, |
| | 285 | | RefreshPriority.High); |
| | 286 | | } |
| | 287 | |
|
| | 288 | | internal static int DetermineAdjustedIndex(int newPriorIndexAllChildren, int newIndex) |
| | 289 | | { |
| 3 | 290 | | if (newIndex == 0) |
| | 291 | | { |
| 2 | 292 | | return newPriorIndexAllChildren > 0 ? newPriorIndexAllChildren - 1 : 0; |
| | 293 | | } |
| | 294 | |
|
| 1 | 295 | | return newPriorIndexAllChildren + 1; |
| | 296 | | } |
| | 297 | |
|
| | 298 | | public async Task MoveItemAsync(string playlistId, string entryId, int newIndex, Guid callingUserId) |
| | 299 | | { |
| | 300 | | if (_libraryManager.GetItemById(playlistId) is not Playlist playlist) |
| | 301 | | { |
| | 302 | | throw new ArgumentException("No Playlist exists with the supplied Id"); |
| | 303 | | } |
| | 304 | |
|
| | 305 | | var user = _userManager.GetUserById(callingUserId); |
| | 306 | | var children = playlist.GetManageableItems().ToList(); |
| | 307 | | var accessibleChildren = children.Where(c => c.Item2.IsVisible(user)).ToArray(); |
| | 308 | |
|
| | 309 | | var oldIndexAll = children.FindIndex(i => string.Equals(entryId, i.Item1.ItemId?.ToString("N", CultureInfo.I |
| | 310 | | var oldIndexAccessible = accessibleChildren.FindIndex(i => string.Equals(entryId, i.Item1.ItemId?.ToString(" |
| | 311 | |
|
| | 312 | | if (oldIndexAccessible == newIndex) |
| | 313 | | { |
| | 314 | | return; |
| | 315 | | } |
| | 316 | |
|
| | 317 | | var newPriorItemIndex = newIndex > oldIndexAccessible ? newIndex : newIndex - 1 < 0 ? 0 : newIndex - 1; |
| | 318 | | var newPriorItemId = accessibleChildren[newPriorItemIndex].Item1.ItemId; |
| | 319 | | var newPriorItemIndexOnAllChildren = children.FindIndex(c => c.Item1.ItemId.Equals(newPriorItemId)); |
| | 320 | | var adjustedNewIndex = DetermineAdjustedIndex(newPriorItemIndexOnAllChildren, newIndex); |
| | 321 | |
|
| | 322 | | var item = playlist.LinkedChildren.FirstOrDefault(i => string.Equals(entryId, i.ItemId?.ToString("N", Cultur |
| | 323 | | if (item is null) |
| | 324 | | { |
| | 325 | | _logger.LogWarning("Modified item not found in playlist. ItemId: {ItemId}, PlaylistId: {PlaylistId}", en |
| | 326 | |
|
| | 327 | | return; |
| | 328 | | } |
| | 329 | |
|
| | 330 | | var newList = playlist.LinkedChildren.ToList(); |
| | 331 | | newList.Remove(item); |
| | 332 | |
|
| | 333 | | if (newIndex >= newList.Count) |
| | 334 | | { |
| | 335 | | newList.Add(item); |
| | 336 | | } |
| | 337 | | else |
| | 338 | | { |
| | 339 | | newList.Insert(adjustedNewIndex, item); |
| | 340 | | } |
| | 341 | |
|
| | 342 | | playlist.LinkedChildren = [.. newList]; |
| | 343 | |
|
| | 344 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 345 | | } |
| | 346 | |
|
| | 347 | | /// <inheritdoc /> |
| | 348 | | public void SavePlaylistFile(Playlist item) |
| | 349 | | { |
| | 350 | | // this is probably best done as a metadata provider |
| | 351 | | // saving a file over itself will require some work to prevent this from happening when not needed |
| 0 | 352 | | var playlistPath = item.Path; |
| 0 | 353 | | var extension = Path.GetExtension(playlistPath.AsSpan()); |
| | 354 | |
|
| 0 | 355 | | if (extension.Equals(".wpl", StringComparison.OrdinalIgnoreCase)) |
| | 356 | | { |
| 0 | 357 | | var playlist = new WplPlaylist(); |
| 0 | 358 | | foreach (var child in item.GetLinkedChildren()) |
| | 359 | | { |
| 0 | 360 | | var entry = new WplPlaylistEntry() |
| 0 | 361 | | { |
| 0 | 362 | | Path = NormalizeItemPath(playlistPath, child.Path), |
| 0 | 363 | | TrackTitle = child.Name, |
| 0 | 364 | | AlbumTitle = child.Album |
| 0 | 365 | | }; |
| | 366 | |
|
| 0 | 367 | | if (child is IHasAlbumArtist hasAlbumArtist) |
| | 368 | | { |
| 0 | 369 | | entry.AlbumArtist = hasAlbumArtist.AlbumArtists.Count > 0 ? hasAlbumArtist.AlbumArtists[0] : nul |
| | 370 | | } |
| | 371 | |
|
| 0 | 372 | | if (child is IHasArtist hasArtist) |
| | 373 | | { |
| 0 | 374 | | entry.TrackArtist = hasArtist.Artists.Count > 0 ? hasArtist.Artists[0] : null; |
| | 375 | | } |
| | 376 | |
|
| 0 | 377 | | if (child.RunTimeTicks.HasValue) |
| | 378 | | { |
| 0 | 379 | | entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value); |
| | 380 | | } |
| | 381 | |
|
| 0 | 382 | | playlist.PlaylistEntries.Add(entry); |
| | 383 | | } |
| | 384 | |
|
| 0 | 385 | | string text = new WplContent().ToText(playlist); |
| 0 | 386 | | File.WriteAllText(playlistPath, text); |
| | 387 | | } |
| 0 | 388 | | else if (extension.Equals(".zpl", StringComparison.OrdinalIgnoreCase)) |
| | 389 | | { |
| 0 | 390 | | var playlist = new ZplPlaylist(); |
| 0 | 391 | | foreach (var child in item.GetLinkedChildren()) |
| | 392 | | { |
| 0 | 393 | | var entry = new ZplPlaylistEntry() |
| 0 | 394 | | { |
| 0 | 395 | | Path = NormalizeItemPath(playlistPath, child.Path), |
| 0 | 396 | | TrackTitle = child.Name, |
| 0 | 397 | | AlbumTitle = child.Album |
| 0 | 398 | | }; |
| | 399 | |
|
| 0 | 400 | | if (child is IHasAlbumArtist hasAlbumArtist) |
| | 401 | | { |
| 0 | 402 | | entry.AlbumArtist = hasAlbumArtist.AlbumArtists.Count > 0 ? hasAlbumArtist.AlbumArtists[0] : nul |
| | 403 | | } |
| | 404 | |
|
| 0 | 405 | | if (child is IHasArtist hasArtist) |
| | 406 | | { |
| 0 | 407 | | entry.TrackArtist = hasArtist.Artists.Count > 0 ? hasArtist.Artists[0] : null; |
| | 408 | | } |
| | 409 | |
|
| 0 | 410 | | if (child.RunTimeTicks.HasValue) |
| | 411 | | { |
| 0 | 412 | | entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value); |
| | 413 | | } |
| | 414 | |
|
| 0 | 415 | | playlist.PlaylistEntries.Add(entry); |
| | 416 | | } |
| | 417 | |
|
| 0 | 418 | | string text = new ZplContent().ToText(playlist); |
| 0 | 419 | | File.WriteAllText(playlistPath, text); |
| | 420 | | } |
| 0 | 421 | | else if (extension.Equals(".m3u", StringComparison.OrdinalIgnoreCase)) |
| | 422 | | { |
| 0 | 423 | | var playlist = new M3uPlaylist |
| 0 | 424 | | { |
| 0 | 425 | | IsExtended = true |
| 0 | 426 | | }; |
| 0 | 427 | | foreach (var child in item.GetLinkedChildren()) |
| | 428 | | { |
| 0 | 429 | | var entry = new M3uPlaylistEntry() |
| 0 | 430 | | { |
| 0 | 431 | | Path = NormalizeItemPath(playlistPath, child.Path), |
| 0 | 432 | | Title = child.Name, |
| 0 | 433 | | Album = child.Album |
| 0 | 434 | | }; |
| | 435 | |
|
| 0 | 436 | | if (child is IHasAlbumArtist hasAlbumArtist) |
| | 437 | | { |
| 0 | 438 | | entry.AlbumArtist = hasAlbumArtist.AlbumArtists.Count > 0 ? hasAlbumArtist.AlbumArtists[0] : nul |
| | 439 | | } |
| | 440 | |
|
| 0 | 441 | | if (child.RunTimeTicks.HasValue) |
| | 442 | | { |
| 0 | 443 | | entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value); |
| | 444 | | } |
| | 445 | |
|
| 0 | 446 | | playlist.PlaylistEntries.Add(entry); |
| | 447 | | } |
| | 448 | |
|
| 0 | 449 | | string text = new M3uContent().ToText(playlist); |
| 0 | 450 | | File.WriteAllText(playlistPath, text); |
| | 451 | | } |
| 0 | 452 | | else if (extension.Equals(".m3u8", StringComparison.OrdinalIgnoreCase)) |
| | 453 | | { |
| 0 | 454 | | var playlist = new M3uPlaylist |
| 0 | 455 | | { |
| 0 | 456 | | IsExtended = true |
| 0 | 457 | | }; |
| | 458 | |
|
| 0 | 459 | | foreach (var child in item.GetLinkedChildren()) |
| | 460 | | { |
| 0 | 461 | | var entry = new M3uPlaylistEntry() |
| 0 | 462 | | { |
| 0 | 463 | | Path = NormalizeItemPath(playlistPath, child.Path), |
| 0 | 464 | | Title = child.Name, |
| 0 | 465 | | Album = child.Album |
| 0 | 466 | | }; |
| | 467 | |
|
| 0 | 468 | | if (child is IHasAlbumArtist hasAlbumArtist) |
| | 469 | | { |
| 0 | 470 | | entry.AlbumArtist = hasAlbumArtist.AlbumArtists.Count > 0 ? hasAlbumArtist.AlbumArtists[0] : nul |
| | 471 | | } |
| | 472 | |
|
| 0 | 473 | | if (child.RunTimeTicks.HasValue) |
| | 474 | | { |
| 0 | 475 | | entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value); |
| | 476 | | } |
| | 477 | |
|
| 0 | 478 | | playlist.PlaylistEntries.Add(entry); |
| | 479 | | } |
| | 480 | |
|
| 0 | 481 | | string text = new M3uContent().ToText(playlist); |
| 0 | 482 | | File.WriteAllText(playlistPath, text); |
| | 483 | | } |
| 0 | 484 | | else if (extension.Equals(".pls", StringComparison.OrdinalIgnoreCase)) |
| | 485 | | { |
| 0 | 486 | | var playlist = new PlsPlaylist(); |
| 0 | 487 | | foreach (var child in item.GetLinkedChildren()) |
| | 488 | | { |
| 0 | 489 | | var entry = new PlsPlaylistEntry() |
| 0 | 490 | | { |
| 0 | 491 | | Path = NormalizeItemPath(playlistPath, child.Path), |
| 0 | 492 | | Title = child.Name |
| 0 | 493 | | }; |
| | 494 | |
|
| 0 | 495 | | if (child.RunTimeTicks.HasValue) |
| | 496 | | { |
| 0 | 497 | | entry.Length = TimeSpan.FromTicks(child.RunTimeTicks.Value); |
| | 498 | | } |
| | 499 | |
|
| 0 | 500 | | playlist.PlaylistEntries.Add(entry); |
| | 501 | | } |
| | 502 | |
|
| 0 | 503 | | string text = new PlsContent().ToText(playlist); |
| 0 | 504 | | File.WriteAllText(playlistPath, text); |
| | 505 | | } |
| 0 | 506 | | } |
| | 507 | |
|
| | 508 | | private static string NormalizeItemPath(string playlistPath, string itemPath) |
| | 509 | | { |
| 0 | 510 | | return MakeRelativePath(Path.GetDirectoryName(playlistPath), itemPath); |
| | 511 | | } |
| | 512 | |
|
| | 513 | | private static string MakeRelativePath(string folderPath, string fileAbsolutePath) |
| | 514 | | { |
| 0 | 515 | | ArgumentException.ThrowIfNullOrEmpty(folderPath); |
| 0 | 516 | | ArgumentException.ThrowIfNullOrEmpty(fileAbsolutePath); |
| | 517 | |
|
| 0 | 518 | | if (!folderPath.EndsWith(Path.DirectorySeparatorChar)) |
| | 519 | | { |
| 0 | 520 | | folderPath += Path.DirectorySeparatorChar; |
| | 521 | | } |
| | 522 | |
|
| 0 | 523 | | var folderUri = new Uri(folderPath); |
| 0 | 524 | | var fileAbsoluteUri = new Uri(fileAbsolutePath); |
| | 525 | |
|
| | 526 | | // path can't be made relative |
| 0 | 527 | | if (folderUri.Scheme != fileAbsoluteUri.Scheme) |
| | 528 | | { |
| 0 | 529 | | return fileAbsolutePath; |
| | 530 | | } |
| | 531 | |
|
| 0 | 532 | | var relativeUri = folderUri.MakeRelativeUri(fileAbsoluteUri); |
| 0 | 533 | | string relativePath = Uri.UnescapeDataString(relativeUri.ToString()); |
| | 534 | |
|
| 0 | 535 | | if (fileAbsoluteUri.Scheme.Equals("file", StringComparison.OrdinalIgnoreCase)) |
| | 536 | | { |
| 0 | 537 | | relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); |
| | 538 | | } |
| | 539 | |
|
| 0 | 540 | | return relativePath; |
| | 541 | | } |
| | 542 | |
|
| | 543 | | /// <inheritdoc /> |
| | 544 | | public Folder GetPlaylistsFolder() |
| | 545 | | { |
| 2 | 546 | | return GetPlaylistsFolder(Guid.Empty); |
| | 547 | | } |
| | 548 | |
|
| | 549 | | /// <inheritdoc /> |
| | 550 | | public Folder GetPlaylistsFolder(Guid userId) |
| | 551 | | { |
| | 552 | | const string TypeName = "PlaylistsFolder"; |
| | 553 | |
|
| 2 | 554 | | return _libraryManager.RootFolder.Children.OfType<Folder>().FirstOrDefault(i => string.Equals(i.GetType().Na |
| 2 | 555 | | _libraryManager.GetUserRootFolder().Children.OfType<Folder>().FirstOrDefault(i => string.Equals(i.GetTyp |
| | 556 | | } |
| | 557 | |
|
| | 558 | | /// <inheritdoc /> |
| | 559 | | public async Task RemovePlaylistsAsync(Guid userId) |
| | 560 | | { |
| | 561 | | var playlists = GetUserPlaylists(userId); |
| | 562 | | foreach (var playlist in playlists) |
| | 563 | | { |
| | 564 | | // Update owner if shared |
| | 565 | | var rankedShares = playlist.Shares.OrderByDescending(x => x.CanEdit).ToList(); |
| | 566 | | if (rankedShares.Count > 0) |
| | 567 | | { |
| | 568 | | playlist.OwnerUserId = rankedShares[0].UserId; |
| | 569 | | playlist.Shares = rankedShares.Skip(1).ToArray(); |
| | 570 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 571 | | } |
| | 572 | | else if (!playlist.OpenAccess) |
| | 573 | | { |
| | 574 | | // Remove playlist if not shared |
| | 575 | | _libraryManager.DeleteItem( |
| | 576 | | playlist, |
| | 577 | | new DeleteOptions |
| | 578 | | { |
| | 579 | | DeleteFileLocation = false, |
| | 580 | | DeleteFromExternalProvider = false |
| | 581 | | }, |
| | 582 | | playlist.GetParent(), |
| | 583 | | false); |
| | 584 | | } |
| | 585 | | } |
| | 586 | | } |
| | 587 | |
|
| | 588 | | public async Task UpdatePlaylist(PlaylistUpdateRequest request) |
| | 589 | | { |
| | 590 | | var playlist = GetPlaylistForUser(request.Id, request.UserId); |
| | 591 | |
|
| | 592 | | if (request.Ids is not null) |
| | 593 | | { |
| | 594 | | playlist.LinkedChildren = []; |
| | 595 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 596 | |
|
| | 597 | | var user = _userManager.GetUserById(request.UserId); |
| | 598 | | await AddToPlaylistInternal(request.Id, request.Ids, user, new DtoOptions(false) |
| | 599 | | { |
| | 600 | | EnableImages = true |
| | 601 | | }).ConfigureAwait(false); |
| | 602 | |
|
| | 603 | | playlist = GetPlaylistForUser(request.Id, request.UserId); |
| | 604 | | } |
| | 605 | |
|
| | 606 | | if (request.Name is not null) |
| | 607 | | { |
| | 608 | | playlist.Name = request.Name; |
| | 609 | | } |
| | 610 | |
|
| | 611 | | if (request.Users is not null) |
| | 612 | | { |
| | 613 | | playlist.Shares = request.Users; |
| | 614 | | } |
| | 615 | |
|
| | 616 | | if (request.Public is not null) |
| | 617 | | { |
| | 618 | | playlist.OpenAccess = request.Public.Value; |
| | 619 | | } |
| | 620 | |
|
| | 621 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 622 | | } |
| | 623 | |
|
| | 624 | | public async Task AddUserToShares(PlaylistUserUpdateRequest request) |
| | 625 | | { |
| | 626 | | var userId = request.UserId; |
| | 627 | | var playlist = GetPlaylistForUser(request.Id, userId); |
| | 628 | | var shares = playlist.Shares.ToList(); |
| | 629 | | var existingUserShare = shares.FirstOrDefault(s => s.UserId.Equals(userId)); |
| | 630 | | if (existingUserShare is not null) |
| | 631 | | { |
| | 632 | | shares.Remove(existingUserShare); |
| | 633 | | } |
| | 634 | |
|
| | 635 | | shares.Add(new PlaylistUserPermissions(userId, request.CanEdit ?? false)); |
| | 636 | | playlist.Shares = shares; |
| | 637 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 638 | | } |
| | 639 | |
|
| | 640 | | public async Task RemoveUserFromShares(Guid playlistId, Guid userId, PlaylistUserPermissions share) |
| | 641 | | { |
| | 642 | | var playlist = GetPlaylistForUser(playlistId, userId); |
| | 643 | | var shares = playlist.Shares.ToList(); |
| | 644 | | shares.Remove(share); |
| | 645 | | playlist.Shares = shares; |
| | 646 | | await UpdatePlaylistInternal(playlist).ConfigureAwait(false); |
| | 647 | | } |
| | 648 | |
|
| | 649 | | private async Task UpdatePlaylistInternal(Playlist playlist) |
| | 650 | | { |
| | 651 | | await playlist.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(f |
| | 652 | |
|
| | 653 | | if (playlist.IsFile) |
| | 654 | | { |
| | 655 | | SavePlaylistFile(playlist); |
| | 656 | | } |
| | 657 | | } |
| | 658 | | } |
| | 659 | | } |