Esempio n. 1
0
        private async Task <IList <TrackViewModel> > GetStaticPlaylistTracksAsync(PlaylistViewModel playlist)
        {
            var tracks  = new List <TrackViewModel>();
            var decoder = new PlaylistDecoder();

            await Task.Run(async() =>
            {
                DecodePlaylistResult decodeResult = null;
                decodeResult = decoder.DecodePlaylist(playlist.Path);

                if (decodeResult.DecodeResult.Result)
                {
                    foreach (string path in decodeResult.Paths)
                    {
                        try
                        {
                            tracks.Add(await this.fileService.CreateTrackAsync(path));
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not get track information from file. Exception: {0}", ex.Message);
                        }
                    }
                }
            });

            return(tracks);
        }
Esempio n. 2
0
        private async Task<IList<TrackViewModel>> GetStaticPlaylistTracksAsync(PlaylistViewModel playlist)
        {
            var tracks = new List<TrackViewModel>();
            var decoder = new PlaylistDecoder();

            await Task.Run(async () =>
            {
                DecodePlaylistResult decodeResult = null;
                decodeResult = decoder.DecodePlaylist(playlist.Path);

                if (decodeResult.DecodeResult.Result)
                {
                    foreach (PlaylistEntry entry in decodeResult.PlaylistEntries)
                    {
                        try
                        {
                            TrackViewModel track = await this.fileService.CreateTrackAsync(entry.DecodedPath);
                            track.PlaylistEntry = entry.ReferencePath; // This will allow deleting the correct entry in a playlist file
                            tracks.Add(track);
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not get track information from file. Exception: {0}", ex.Message);
                        }
                    }
                }
            });

            return tracks;
        }
Esempio n. 3
0
        public override async Task <IList <TrackViewModel> > GetTracksAsync(PlaylistViewModel playlist)
        {
            // If no playlist was selected, return no tracks.
            if (playlist == null)
            {
                LogClient.Error($"{nameof(playlist)} is null. Returning empty list of tracks.");
                return(new List <TrackViewModel>());
            }

            var tracks  = new List <TrackViewModel>();
            var decoder = new PlaylistDecoder();

            await Task.Run(async() =>
            {
                DecodePlaylistResult decodeResult = null;
                decodeResult = decoder.DecodePlaylist(playlist.Path);

                if (decodeResult.DecodeResult.Result)
                {
                    foreach (string path in decodeResult.Paths)
                    {
                        try
                        {
                            tracks.Add(await this.fileService.CreateTrackAsync(path));
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not get track information from file. Exception: {0}", ex.Message);
                        }
                    }
                }
            });

            return(tracks);
        }
Esempio n. 4
0
        private List <string> ProcessPlaylistFile(string playlistPath)
        {
            var decoder = new PlaylistDecoder();
            DecodePlaylistResult decodeResult = decoder.DecodePlaylist(playlistPath);

            if (!decodeResult.DecodeResult.Result)
            {
                LogClient.Error("Error while decoding playlist file. Exception: {0}", decodeResult.DecodeResult.GetMessages());
            }

            return(decodeResult.Paths);
        }
Esempio n. 5
0
        public async Task <OpenPlaylistResult> OpenPlaylistAsync(string fileName)
        {
            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.Instance.Logger.Error("Error while decoding playlist file. Exception: {0}", decodeResult.DecodeResult.GetMessages());
                return(OpenPlaylistResult.Error);
            }

            // Set the paths
            // -------------
            paths = decodeResult.Paths;


            // Get a unique name for the playlist
            // ----------------------------------
            playlistName = await this.playlistRepository.GetUniquePlaylistNameAsync(decodeResult.PlaylistName);

            // Add the Playlist to the database
            // --------------------------------
            AddPlaylistResult addPlaylistResult = await this.playlistRepository.AddPlaylistAsync(playlistName);

            if (addPlaylistResult != AddPlaylistResult.Success)
            {
                return(OpenPlaylistResult.Error);
            }

            // Add TrackInfo's to the Playlist
            // -------------------------------
            List <TrackInfo> tracks = await this.trackRepository.GetTracksAsync(paths);

            AddToPlaylistResult result = await this.playlistRepository.AddTracksToPlaylistAsync(tracks, playlistName);

            if (!result.IsSuccess)
            {
                return(OpenPlaylistResult.Error);
            }

            // If we arrive at this point, OpenPlaylistResult = OpenPlaylistResult.Success,
            // so we can always raise the PlaylistsChanged Event.
            this.PlaylistsChanged(this, new EventArgs());

            return(OpenPlaylistResult.Success);
        }
Esempio n. 6
0
        public async Task <List <PlayableTrack> > GetTracks(IList <string> playlists)
        {
            var tracks  = new List <PlayableTrack>();
            var decoder = new PlaylistDecoder();

            var allPlaylists = playlists;

            // If no playlists were selected, get all playlists.
            if (playlists == null || playlists.Count == 0)
            {
                allPlaylists = await this.GetPlaylistsAsync();
            }

            await Task.Run(async() =>
            {
                foreach (string playlist in allPlaylists)
                {
                    string filename = this.CreatePlaylistFilename(playlist);
                    DecodePlaylistResult decodeResult = null;
                    decodeResult = decoder.DecodePlaylist(filename);

                    if (decodeResult.DecodeResult.Result)
                    {
                        foreach (string path in decodeResult.Paths)
                        {
                            try
                            {
                                tracks.Add(await this.fileService.CreateTrackAsync(path));
                            }
                            catch (Exception ex)
                            {
                                LogClient.Error("Could not get track information from file. Exception: {0}", ex.Message);
                            }
                        }
                    }
                }
            });

            return(tracks);
        }
Esempio n. 7
0
        public void DecodeM3uPlaylist()
        {
            // Arrange
            string playlistPath = System.IO.Path.GetFullPath(@"Files\PlaylistDecoder\Test.m3u");

            // Act
            var decoder = new PlaylistDecoder();
            DecodePlaylistResult result = decoder.DecodePlaylist(playlistPath);

            // Assert
            if (result.DecodeResult.Result && !string.IsNullOrWhiteSpace(result.PlaylistName))
            {
                Assert.IsTrue(result.Paths.Count == 3);
                Assert.AreEqual(result.Paths[0], @"C:\Music\File1.mp3");
                Assert.AreEqual(result.Paths[1], @"C:\Music\File2.mp3");
                Assert.AreEqual(result.Paths[2], @"C:\Music\File3.mp3");
                Assert.AreEqual(result.PlaylistName, "Test");
            }
            else
            {
                Assert.Fail();
            }
        }
Esempio n. 8
0
        public async Task <List <TrackViewModel> > GetTracks(string playlistName)
        {
            // If no playlist was selected, return no tracks.
            if (string.IsNullOrEmpty(playlistName))
            {
                LogClient.Error("PlaylistName is empty. Returning empty list of tracks.");
                return(new List <TrackViewModel>());
            }

            var tracks  = new List <TrackViewModel>();
            var decoder = new PlaylistDecoder();

            await Task.Run(async() =>
            {
                string filename = this.CreatePlaylistFilename(playlistName);
                DecodePlaylistResult decodeResult = null;
                decodeResult = decoder.DecodePlaylist(filename);

                if (decodeResult.DecodeResult.Result)
                {
                    foreach (string path in decodeResult.Paths)
                    {
                        try
                        {
                            tracks.Add(await this.fileService.CreateTrackAsync(path));
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not get track information from file. Exception: {0}", ex.Message);
                        }
                    }
                }
            });

            return(tracks);
        }
Esempio n. 9
0
        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;
        }
Esempio n. 10
0
        public async Task <OpenPlaylistResult> OpenPlaylistAsync(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                LogClient.Error("FileName is empty");
                return(OpenPlaylistResult.Error);
            }

            watcher.EnableRaisingEvents = false; // 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(OpenPlaylistResult.Error);
            }

            // Set the paths
            // -------------
            paths = decodeResult.Paths;

            // Get a unique name for the playlist
            // ----------------------------------
            try
            {
                playlistName = await this.GetUniquePlaylistAsync(System.IO.Path.GetFileNameWithoutExtension(fileName));
            }
            catch (Exception ex)
            {
                LogClient.Error("Error while getting unique playlist filename. Exception: {0}", ex.Message);
                return(OpenPlaylistResult.Error);
            }

            // Create the Playlist in the playlists folder
            // -------------------------------------------
            string sanitizedPlaylist = FileUtils.SanitizeFilename(playlistName);
            string filename          = this.CreatePlaylistFilename(sanitizedPlaylist);

            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);
                return(OpenPlaylistResult.Error);
            }

            // If we arrive at this point, OpenPlaylistResult = OpenPlaylistResult.Success, so we can always raise the PlaylistAdded Event.
            this.PlaylistAdded(playlistName);

            watcher.EnableRaisingEvents = true; // Start watching the playlist folder

            return(OpenPlaylistResult.Success);
        }
Esempio n. 11
0
        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);
        }