public async Task<ImportPlaylistResult> ImportPlaylistsAsync(IList<string> fileNames) { ImportPlaylistResult finalResult = ImportPlaylistResult.Success; this.watcher.Suspend(); // Stop watching the playlist folder IList<ImportPlaylistResult> results = new List<ImportPlaylistResult>(); foreach (string fileName in fileNames) { if (FileFormats.IsSupportedStaticPlaylistFile(fileName)) { results.Add(await this.ImportStaticPlaylistAsync(fileName)); } else if (FileFormats.IsSupportedSmartPlaylistFile(fileName)) { results.Add(await this.ImportSmartPlaylistAsync(fileName)); } } this.watcher.Resume(); // Start watching the playlist folder if (results.Any(x => x.Equals(ImportPlaylistResult.Success))) { this.PlaylistFolderChanged(this, new EventArgs()); } return finalResult; }
protected override async Task <ImportPlaylistResult> ImportPlaylistAsync(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { LogClient.Error($"{nameof(fileName)} is empty"); return(ImportPlaylistResult.Error); } IList <PlaylistViewModel> existingPlaylists = await this.GetPlaylistsAsync(); string newPlaylistName = string.Empty; string newFileNameWithoutExtension = string.Empty; string newPlaylistFileName = string.Empty; this.Watcher.Suspend(); // Stop watching the playlist folder ImportPlaylistResult result = ImportPlaylistResult.Success; await Task.Run(() => { try { IList <string> existingPlaylistNames = existingPlaylists.Select(x => x.Name).ToList(); IList <string> existingFileNamesWithoutExtension = existingPlaylists.Select(x => Path.GetFileNameWithoutExtension(x.Path)).ToList(); string originalPlaylistName = this.GetPlaylistName(fileName); newPlaylistName = originalPlaylistName.MakeUnique(existingPlaylistNames); string originalFileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); newFileNameWithoutExtension = originalFileNameWithoutExtension.MakeUnique(existingFileNamesWithoutExtension); // Generate a new filename for the playlist newPlaylistFileName = this.CreatePlaylistFilename(newFileNameWithoutExtension); // Copy the playlist file to the playlists folder, using the new filename. System.IO.File.Copy(fileName, newPlaylistFileName); // Change the playlist name to the unique name (if changed) this.SetPlaylistNameIfDifferent(newPlaylistFileName, newPlaylistName); } catch (Exception ex) { LogClient.Error($"Error while importing smart playlist. Exception: {ex.Message}"); result = ImportPlaylistResult.Error; } }); if (result.Equals(ImportPlaylistResult.Success)) { this.OnPlaylistAdded(new PlaylistViewModel(newPlaylistName, newPlaylistFileName)); } this.Watcher.Resume(); // Start watching the playlist folder return(result); }
public async Task <ImportPlaylistResult> ImportPlaylistsAsync(IList <string> fileNames) { ImportPlaylistResult finalResult = ImportPlaylistResult.Success; foreach (string fileName in fileNames) { ImportPlaylistResult result = await this.ImportPlaylistAsync(fileName); if (!result.Equals(ImportPlaylistResult.Success)) { finalResult = result; } } return(finalResult); }
protected async Task ImportPlaylistsAsync(IList <string> playlistPaths = null) { if (playlistPaths == null) { // Set up the file dialog box Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Title = Application.Current.FindResource("Language_Import_Playlists").ToString(); dlg.DefaultExt = FileFormats.M3U; // Default file extension dlg.Multiselect = true; // Filter files by extension dlg.Filter = $"{ResourceUtils.GetString("Language_Playlists")} {this.playlistService.DialogFileFilter}"; // Show the file dialog box bool?dialogResult = dlg.ShowDialog(); // Process the file dialog box result if (!(bool)dialogResult) { return; } playlistPaths = dlg.FileNames; } ImportPlaylistResult result = await this.playlistService.ImportPlaylistsAsync(playlistPaths); if (result == ImportPlaylistResult.Error) { this.dialogService.ShowNotification( 0xe711, 16, ResourceUtils.GetString("Language_Error"), ResourceUtils.GetString("Language_Error_Importing_Playlists"), ResourceUtils.GetString("Language_Ok"), true, ResourceUtils.GetString("Language_Log_File")); } }
private async Task<ImportPlaylistResult> ImportStaticPlaylistAsync(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { LogClient.Error("FileName is empty"); return ImportPlaylistResult.Error; } string playlistName = String.Empty; IList<string> paths = new List<string>(); // Decode the playlist file // ------------------------ var decoder = new PlaylistDecoder(); DecodePlaylistResult decodeResult = null; await Task.Run(() => decodeResult = decoder.DecodePlaylist(fileName)); if (!decodeResult.DecodeResult.Result) { LogClient.Error("Error while decoding playlist file. Exception: {0}", decodeResult.DecodeResult.GetMessages()); return ImportPlaylistResult.Error; } // Set the paths // ------------- paths = decodeResult.Paths; // Get a unique name for the playlist // ---------------------------------- try { playlistName = await this.GetUniquePlaylistNameAsync(System.IO.Path.GetFileNameWithoutExtension(fileName)); } catch (Exception ex) { LogClient.Error("Error while getting unique playlist filename. Exception: {0}", ex.Message); return ImportPlaylistResult.Error; } // Create the Playlist in the playlists folder // ------------------------------------------- string sanitizedPlaylistName = this.SanitizePlaylistFilename(playlistName); string filename = this.CreatePlaylistFilePath(sanitizedPlaylistName, PlaylistType.Static); ImportPlaylistResult result = ImportPlaylistResult.Success; try { using (FileStream fs = System.IO.File.Create(filename)) { using (var writer = new StreamWriter(fs)) { foreach (string path in paths) { try { writer.WriteLine(path); } catch (Exception ex) { LogClient.Error("Could not write path '{0}' to playlist '{1}' with filename '{2}'. Exception: {3}", path, playlistName, filename, ex.Message); } } } } } catch (Exception ex) { LogClient.Error("Could not create playlist '{0}' with filename '{1}'. Exception: {2}", playlistName, filename, ex.Message); result = ImportPlaylistResult.Error; } return result; }
protected override async Task <ImportPlaylistResult> ImportPlaylistAsync(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { LogClient.Error("FileName is empty"); return(ImportPlaylistResult.Error); } this.Watcher.Suspend(); // Stop watching the playlist folder string playlistName = String.Empty; var paths = new List <String>(); // Decode the playlist file // ------------------------ var decoder = new PlaylistDecoder(); DecodePlaylistResult decodeResult = null; await Task.Run(() => decodeResult = decoder.DecodePlaylist(fileName)); if (!decodeResult.DecodeResult.Result) { LogClient.Error("Error while decoding playlist file. Exception: {0}", decodeResult.DecodeResult.GetMessages()); return(ImportPlaylistResult.Error); } // Set the paths // ------------- paths = decodeResult.Paths; // Get a unique name for the playlist // ---------------------------------- try { playlistName = await this.GetUniquePlaylistNameAsync(System.IO.Path.GetFileNameWithoutExtension(fileName)); } catch (Exception ex) { LogClient.Error("Error while getting unique playlist filename. Exception: {0}", ex.Message); return(ImportPlaylistResult.Error); } // Create the Playlist in the playlists folder // ------------------------------------------- string sanitizedPlaylistName = FileUtils.SanitizeFilename(playlistName); string filename = this.CreatePlaylistFilename(sanitizedPlaylistName); ImportPlaylistResult result = ImportPlaylistResult.Success; try { using (FileStream fs = System.IO.File.Create(filename)) { using (var writer = new StreamWriter(fs)) { foreach (string path in paths) { try { writer.WriteLine(path); } catch (Exception ex) { LogClient.Error("Could not write path '{0}' to playlist '{1}' with filename '{2}'. Exception: {3}", path, playlistName, filename, ex.Message); } } } } } catch (Exception ex) { LogClient.Error("Could not create playlist '{0}' with filename '{1}'. Exception: {2}", playlistName, filename, ex.Message); result = ImportPlaylistResult.Error; } if (result.Equals(ImportPlaylistResult.Success)) { this.OnPlaylistAdded(new PlaylistViewModel(sanitizedPlaylistName, filename)); } this.Watcher.Resume(); // Start watching the playlist folder return(result); }