diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index a83756320..8ccb60bfc 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -281,6 +281,11 @@ namespace NzbDrone.Common.Test.Http Subject.DownloadFile(url, file); File.Exists(file).Should().BeTrue(); + File.Exists(file + ".part").Should().BeFalse(); + + var fileInfo = new FileInfo(file); + + fileInfo.Length.Should().Be(307054); } [Test] @@ -288,9 +293,10 @@ namespace NzbDrone.Common.Test.Http { var file = GetTempFilePath(); - Assert.Throws(() => Subject.DownloadFile("http://download.sonarr.tv/wrongpath", file)); + Assert.Throws(() => Subject.DownloadFile("http://download.sonarr.tv/wrongpath", file)); File.Exists(file).Should().BeFalse(); + File.Exists(file + ".part").Should().BeFalse(); ExceptionVerification.ExpectedWarns(1); } diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index ccde3a50d..5ddfa9f60 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -229,6 +229,8 @@ namespace NzbDrone.Common.Http public void DownloadFile(string url, string fileName) { + var fileNamePart = fileName + ".part"; + try { var fileInfo = new FileInfo(fileName); @@ -240,24 +242,22 @@ namespace NzbDrone.Common.Http _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); - using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite)) + using (var fileStream = new FileStream(fileNamePart, FileMode.Create, FileAccess.ReadWrite)) { var request = new HttpRequest(url); request.ResponseStream = fileStream; var response = Get(request); } stopWatch.Stop(); + File.Move(fileNamePart, fileName); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); } - catch (WebException e) - { - _logger.Warn("Failed to get response from: {0} {1}", url, e.Message); - throw; - } - catch (Exception e) + finally { - _logger.Warn(e, "Failed to get response from: " + url); - throw; + if (File.Exists(fileNamePart)) + { + File.Delete(fileNamePart); + } } } diff --git a/src/NzbDrone.Common/Http/HttpException.cs b/src/NzbDrone.Common/Http/HttpException.cs index 005fab57a..d3f5c2da4 100644 --- a/src/NzbDrone.Common/Http/HttpException.cs +++ b/src/NzbDrone.Common/Http/HttpException.cs @@ -28,7 +28,7 @@ namespace NzbDrone.Common.Http public override string ToString() { - if (Response != null) + if (Response != null && Response.ResponseData != null) { return base.ToString() + Environment.NewLine + Response.Content; }