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 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); }