@@ -92,17 +92,19 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
92
92
93
93
class DownloadTask : TaskBase < bool >
94
94
{
95
- private IFileSystem fileSystem ;
95
+ private readonly IFileSystem fileSystem ;
96
96
private long bytes ;
97
97
private WebRequest webRequest ;
98
98
private bool restarted ;
99
99
100
100
public float Progress { get ; set ; }
101
101
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 )
103
103
: base ( token )
104
104
{
105
105
this . fileSystem = fileSystem ;
106
+ ValidationHash = validationHash ;
107
+ RetryCount = retryCount ;
106
108
Url = url ;
107
109
Destination = destination ;
108
110
Name = "DownloadTask" ;
@@ -115,9 +117,30 @@ protected override bool RunWithReturn(bool success)
115
117
RaiseOnStart ( ) ;
116
118
117
119
var result = false ;
120
+ var attempts = 0 ;
118
121
try
119
122
{
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 ++ ) ;
121
144
}
122
145
catch ( Exception ex )
123
146
{
@@ -220,6 +243,10 @@ public bool Download()
220
243
protected string Url { get ; }
221
244
222
245
protected string Destination { get ; }
246
+
247
+ protected string ValidationHash { get ; }
248
+
249
+ protected int RetryCount { get ; }
223
250
}
224
251
225
252
class DownloadTextTask : TaskBase < string >
0 commit comments