mirror of https://github.com/Sonarr/Sonarr
parent
e96d05149c
commit
b0415299ca
|
@ -271,6 +271,18 @@ namespace NzbDrone.Common.Test.Http
|
||||||
response.Resource.Headers[header].ToString().Should().Be(value);
|
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]
|
[Test]
|
||||||
public void should_not_download_file_with_error()
|
public void should_not_download_file_with_error()
|
||||||
{
|
{
|
||||||
|
|
|
@ -33,7 +33,7 @@ namespace NzbDrone.Common.Http.Dispatchers
|
||||||
{
|
{
|
||||||
var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url);
|
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.
|
// On Mono GZipStream/DeflateStream leaks memory if an exception is thrown, use an intermediate buffer in that case.
|
||||||
webRequest.AutomaticDecompression = DecompressionMethods.None;
|
webRequest.AutomaticDecompression = DecompressionMethods.None;
|
||||||
|
@ -121,19 +121,27 @@ namespace NzbDrone.Common.Http.Dispatchers
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
data = responseStream.ToBytes();
|
if (request.ResponseStream != null)
|
||||||
|
|
||||||
if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip")
|
|
||||||
{
|
{
|
||||||
using (var compressedStream = new MemoryStream(data))
|
// A target ResponseStream was specified, write to that instead.
|
||||||
using (var gzip = new GZipStream(compressedStream, CompressionMode.Decompress))
|
responseStream.CopyTo(request.ResponseStream);
|
||||||
using (var decompressedStream = new MemoryStream())
|
}
|
||||||
{
|
else
|
||||||
gzip.CopyTo(decompressedStream);
|
{
|
||||||
data = decompressedStream.ToArray();
|
data = responseStream.ToBytes();
|
||||||
}
|
|
||||||
|
|
||||||
httpWebResponse.Headers.Remove("Content-Encoding");
|
if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip")
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
@ -131,7 +131,7 @@ namespace NzbDrone.Common.Http
|
||||||
response = interceptor.PostResponse(response);
|
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);
|
_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);
|
_logger.Debug("Downloading [{0}] to [{1}]", url, fileName);
|
||||||
|
|
||||||
var stopWatch = Stopwatch.StartNew();
|
var stopWatch = Stopwatch.StartNew();
|
||||||
var webClient = new GZipWebClient();
|
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
|
||||||
webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent());
|
{
|
||||||
webClient.DownloadFile(url, fileName);
|
var request = new HttpRequest(url);
|
||||||
|
request.ResponseStream = fileStream;
|
||||||
|
var response = Get(request);
|
||||||
|
}
|
||||||
stopWatch.Stop();
|
stopWatch.Stop();
|
||||||
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
|
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
@ -43,6 +44,7 @@ namespace NzbDrone.Common.Http
|
||||||
public bool StoreResponseCookie { get; set; }
|
public bool StoreResponseCookie { get; set; }
|
||||||
public TimeSpan RequestTimeout { get; set; }
|
public TimeSpan RequestTimeout { get; set; }
|
||||||
public TimeSpan RateLimit { get; set; }
|
public TimeSpan RateLimit { get; set; }
|
||||||
|
public Stream ResponseStream { get; set; }
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue