public async Task<PlaylistCreationResult> CreatePlaylist(PlaylistCreationRequest options) { var name = options.Name; var folderName = _fileSystem.GetValidFilename(name) + " [playlist]"; var parentFolder = GetPlaylistsFolder(null); if (parentFolder == null) { throw new ArgumentException(); } if (string.IsNullOrWhiteSpace(options.MediaType)) { foreach (var itemId in options.ItemIdList) { var item = _libraryManager.GetItemById(itemId); if (item == null) { throw new ArgumentException("No item exists with the supplied Id"); } if (!string.IsNullOrWhiteSpace(item.MediaType)) { options.MediaType = item.MediaType; } else if (item is MusicArtist || item is MusicAlbum || item is MusicGenre) { options.MediaType = MediaType.Audio; } else if (item is Genre) { options.MediaType = MediaType.Video; } else { var folder = item as Folder; if (folder != null) { options.MediaType = folder.GetRecursiveChildren() .Where(i => !i.IsFolder && i.SupportsAddingToPlaylist) .Select(i => i.MediaType) .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i)); } } if (!string.IsNullOrWhiteSpace(options.MediaType)) { break; } } } if (string.IsNullOrWhiteSpace(options.MediaType)) { throw new ArgumentException("A playlist media type is required."); } var user = _userManager.GetUserById(new Guid(options.UserId)); var path = Path.Combine(parentFolder.Path, folderName); path = GetTargetPath(path); _iLibraryMonitor.ReportFileSystemChangeBeginning(path); try { Directory.CreateDirectory(path); var playlist = new Playlist { Name = name, Parent = parentFolder, Path = path, OwnerUserId = options.UserId }; playlist.SetMediaType(options.MediaType); await parentFolder.AddChild(playlist, CancellationToken.None).ConfigureAwait(false); await playlist.RefreshMetadata(new MetadataRefreshOptions { ForceSave = true }, CancellationToken.None) .ConfigureAwait(false); if (options.ItemIdList.Count > 0) { await AddToPlaylistInternal(playlist.Id.ToString("N"), options.ItemIdList, user); } return new PlaylistCreationResult { Id = playlist.Id.ToString("N") }; } finally { // Refresh handled internally _iLibraryMonitor.ReportFileSystemChangeComplete(path, false); } }
private async Task CreateNewPlaylist(List<BaseItemDto> items) { var request = new PlaylistCreationRequest { UserId = AuthenticationService.Current.LoggedInUserId, MediaType = items.Any(x => x.IsAudio) ? "Audio" : "Video", ItemIdList = items.Select(x => x.Id).ToList(), Name = PlaylistName }; try { SetProgressBar(AppResources.SysTrayAddingToPlaylist); var result = await ApiClient.CreatePlaylist(request); var playlist = new BaseItemDto { Name = PlaylistName, Id = result.Id, MediaType = request.MediaType }; Playlists.Add(playlist); PlaylistName = string.Empty; if (NavigationService.CanGoBack) { NavigationService.GoBack(); } } catch (HttpException ex) { Utils.HandleHttpException(ex, "CreateNewPlaylist()", NavigationService, Log); App.ShowMessage(AppResources.ErrorAddingToPlaylist); } SetProgressBar(); }