Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 2800d7f

Browse files
Adding functionality to validate the downloaded files md5 and retrying
1 parent 3c1f6a7 commit 2800d7f

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,19 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
9292

9393
class DownloadTask : TaskBase<bool>
9494
{
95-
private IFileSystem fileSystem;
95+
private readonly IFileSystem fileSystem;
9696
private long bytes;
9797
private WebRequest webRequest;
9898
private bool restarted;
9999

100100
public float Progress { get; set; }
101101

102-
public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url, string destination)
102+
public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url, string destination, string validationHash = null, int retryCount = 0)
103103
: base(token)
104104
{
105105
this.fileSystem = fileSystem;
106+
ValidationHash = validationHash;
107+
RetryCount = retryCount;
106108
Url = url;
107109
Destination = destination;
108110
Name = "DownloadTask";
@@ -115,9 +117,30 @@ protected override bool RunWithReturn(bool success)
115117
RaiseOnStart();
116118

117119
var result = false;
120+
var attempts = 0;
118121
try
119122
{
120-
result = Download();
123+
do
124+
{
125+
Logger.Trace($"Download of {Url} to {Destination} Attempt {attempts + 1} of {RetryCount + 1}");
126+
result = Download();
127+
if (result && ValidationHash != null)
128+
{
129+
var md5 = fileSystem.CalculateMD5(Destination);
130+
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
131+
132+
if (!result)
133+
{
134+
Logger.Warning($"Downloaded MD5 {md5} does not match expected. Deleting {Destination}.");
135+
fileSystem.FileDelete(Destination);
136+
}
137+
else
138+
{
139+
Logger.Trace($"Download confirmed {md5}");
140+
break;
141+
}
142+
}
143+
} while (RetryCount < attempts++);
121144
}
122145
catch (Exception ex)
123146
{
@@ -220,6 +243,10 @@ public bool Download()
220243
protected string Url { get; }
221244

222245
protected string Destination { get; }
246+
247+
protected string ValidationHash { get; }
248+
249+
protected int RetryCount { get; }
223250
}
224251

225252
class DownloadTextTask : TaskBase<string>

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task TestDownloadTask()
4444
var cutDownloadPathBytes = downloadPathBytes.Take(takeCount).ToArray();
4545
fileSystem.WriteAllBytes(downloadHalfPath, cutDownloadPathBytes);
4646

47-
downloadTask = new DownloadTask(CancellationToken.None, fileSystem, TestDownload, downloadHalfPath);
47+
downloadTask = new DownloadTask(CancellationToken.None, fileSystem, TestDownload, downloadHalfPath, TestDownloadMD5, 1);
4848
downloadResult = await downloadTask.StartAwait();
4949

5050
downloadResult.Should().BeTrue();

0 commit comments

Comments
 (0)