From b0415299ca2f81b9f7d7d430e1c0c5e9a01f0958 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Fri, 30 Aug 2019 23:36:30 +0200 Subject: [PATCH] Fixed: Download mediacover using configured proxy. closes #3283 --- .../Http/HttpClientFixture.cs | 12 ++++++++ .../Http/Dispatchers/ManagedHttpDispatcher.cs | 30 ++++++++++++------- src/NzbDrone.Common/Http/HttpClient.cs | 11 ++++--- src/NzbDrone.Common/Http/HttpRequest.cs | 2 ++ 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index eb918016f..a83756320 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -271,6 +271,18 @@ namespace NzbDrone.Common.Test.Http response.Resource.Headers[header].ToString().Should().Be(value); } + [Test] + public void should_download_file() + { + var file = GetTempFilePath(); + + var url = "https://sonarr.tv/img/slider/seriesdetails.png"; + + Subject.DownloadFile(url, file); + + File.Exists(file).Should().BeTrue(); + } + [Test] public void should_not_download_file_with_error() { diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 9ca1d9e1d..83773b2b2 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -33,7 +33,7 @@ namespace NzbDrone.Common.Http.Dispatchers { var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url); - if (PlatformInfo.IsMono) + if (PlatformInfo.IsMono && request.ResponseStream == null) { // On Mono GZipStream/DeflateStream leaks memory if an exception is thrown, use an intermediate buffer in that case. webRequest.AutomaticDecompression = DecompressionMethods.None; @@ -121,19 +121,27 @@ namespace NzbDrone.Common.Http.Dispatchers { try { - data = responseStream.ToBytes(); - - if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip") + if (request.ResponseStream != null) + { + // A target ResponseStream was specified, write to that instead. + responseStream.CopyTo(request.ResponseStream); + } + else { - using (var compressedStream = new MemoryStream(data)) - using (var gzip = new GZipStream(compressedStream, CompressionMode.Decompress)) - using (var decompressedStream = new MemoryStream()) + data = responseStream.ToBytes(); + + if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip") { - gzip.CopyTo(decompressedStream); - data = decompressedStream.ToArray(); + using (var compressedStream = new MemoryStream(data)) + using (var gzip = new GZipStream(compressedStream, CompressionMode.Decompress)) + using (var decompressedStream = new MemoryStream()) + { + gzip.CopyTo(decompressedStream); + data = decompressedStream.ToArray(); + } + + httpWebResponse.Headers.Remove("Content-Encoding"); } - - httpWebResponse.Headers.Remove("Content-Encoding"); } } catch (Exception ex) diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 58896af48..ccde3a50d 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -131,7 +131,7 @@ namespace NzbDrone.Common.Http response = interceptor.PostResponse(response); } - if (request.LogResponseContent) + if (request.LogResponseContent && response.ResponseData != null) { _logger.Trace("Response content ({0} bytes): {1}", response.ResponseData.Length, response.Content); } @@ -240,9 +240,12 @@ namespace NzbDrone.Common.Http _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); - var webClient = new GZipWebClient(); - webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent()); - webClient.DownloadFile(url, fileName); + using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite)) + { + var request = new HttpRequest(url); + request.ResponseStream = fileStream; + var response = Get(request); + } stopWatch.Stop(); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); } diff --git a/src/NzbDrone.Common/Http/HttpRequest.cs b/src/NzbDrone.Common/Http/HttpRequest.cs index 301890804..2519531ca 100644 --- a/src/NzbDrone.Common/Http/HttpRequest.cs +++ b/src/NzbDrone.Common/Http/HttpRequest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Text; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; @@ -43,6 +44,7 @@ namespace NzbDrone.Common.Http public bool StoreResponseCookie { get; set; } public TimeSpan RequestTimeout { get; set; } public TimeSpan RateLimit { get; set; } + public Stream ResponseStream { get; set; } public override string ToString() {