Esempio n. 1
0
        public async Task CustomStream_Canceled()
        {
            Uri uri = new Uri("http://releases.ubuntu.com/18.04.3/ubuntu-18.04.3-desktop-amd64.iso");
            CancellationTokenSource cts = new CancellationTokenSource(1500);

            Directory.CreateDirectory(TestOutputPath);
            string                      filePath          = Path.Combine(TestOutputPath, "CustomStream.iso");
            DownloadContainer           downloadContainer = new MemoryDownloadContainer();
            Progress <DownloadProgress> progressHandler   = new Progress <DownloadProgress>(p =>
            {
                Console.WriteLine($"IProgress: {p.TotalBytesDownloaded}/{p.TotalDownloadSize} : {p.ProgressPercent}%");
            });

            try
            {
                var response = await Client.GetAsync(uri, cts.Token);

                await downloadContainer.ReceiveDataAsync(response.Content, true, progressHandler, cts.Token).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                Assert.Fail($"Wrong exception thrown: {ex.GetType().Name}: {ex.Message}");
            }
        }
Esempio n. 2
0
        private async Task <AndruzzProtobufContainer?> ParseGzipWebSource(Uri uri)
        {
            AndruzzProtobufContainer?songContainer = null;

            try
            {
                WebUtilities.IWebResponseMessage?downloadResponse = await WebUtils.WebClient.GetAsync(uri).ConfigureAwait(false);

                downloadResponse.EnsureSuccessStatusCode();
                if (downloadResponse.Content != null)
                {
                    using Stream scrapeStream = await downloadResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    string?filePath = FilePath;
                    if (CacheToDisk && !string.IsNullOrWhiteSpace(filePath))
                    {
                        using GZipStream gstream = new GZipStream(scrapeStream, CompressionMode.Decompress);
                        using MemoryDownloadContainer container = new MemoryDownloadContainer();
                        await container.ReceiveDataAsync(gstream).ConfigureAwait(false);

                        using Stream ps = container.GetResultStream();
                        songContainer   = ParseProtobuf(ps);
                        try
                        {
                            using Stream s  = container.GetResultStream();
                            using Stream fs = File.Create(filePath);
                            await s.CopyToAsync(fs).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            Logger?.Warning($"Error caching Andruzz's scraped data to file '{filePath}': {ex.Message}");
                        }
                    }
                    else
                    {
                        using GZipStream gstream = new GZipStream(scrapeStream, CompressionMode.Decompress);
                        songContainer            = ParseProtobuf(gstream);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger?.Warning($"Error loading Andruzz's Scrapped Data from GitHub: {ex.Message}");
            }
            return(songContainer);
        }