@@ -90,11 +90,10 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
90
90
}
91
91
}
92
92
93
- class DownloadTask : TaskBase < bool >
93
+ class DownloadTask : TaskBase
94
94
{
95
95
private readonly IFileSystem fileSystem ;
96
96
private long bytes ;
97
- private WebRequest webRequest ;
98
97
private bool restarted ;
99
98
100
99
public float Progress { get ; set ; }
@@ -110,19 +109,19 @@ public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url,
110
109
Name = "DownloadTask" ;
111
110
}
112
111
113
- protected override bool RunWithReturn ( bool success )
112
+ protected override void Run ( bool success )
114
113
{
115
- base . RunWithReturn ( success ) ;
114
+ base . Run ( success ) ;
116
115
117
116
RaiseOnStart ( ) ;
118
117
119
- var result = false ;
120
118
var attempts = 0 ;
121
119
try
122
120
{
121
+ bool result ;
123
122
do
124
123
{
125
- Logger . Trace ( $ "Download of { Url } to { Destination } Attempt { attempts + 1 } of { RetryCount + 1 } ") ;
124
+ Logger . Trace ( $ "Download of { Url } Attempt { attempts + 1 } of { RetryCount + 1 } ") ;
126
125
result = Download ( ) ;
127
126
if ( result && ValidationHash != null )
128
127
{
@@ -140,20 +139,23 @@ protected override bool RunWithReturn(bool success)
140
139
break ;
141
140
}
142
141
}
143
- } while ( RetryCount < attempts ++ ) ;
142
+ } while ( attempts ++ < RetryCount ) ;
143
+
144
+ if ( ! result )
145
+ {
146
+ throw new DownloadException ( "Error downloading file" ) ;
147
+ }
144
148
}
145
149
catch ( Exception ex )
146
150
{
147
151
Errors = ex . Message ;
148
- if ( ! RaiseFaultHandlers ( ex ) )
152
+ if ( ! RaiseFaultHandlers ( new DownloadException ( "Error downloading file" , ex ) ) )
149
153
throw ;
150
154
}
151
155
finally
152
156
{
153
- RaiseOnEnd ( result ) ;
157
+ RaiseOnEnd ( ) ;
154
158
}
155
-
156
- return result ;
157
159
}
158
160
159
161
protected virtual void UpdateProgress ( float progress )
@@ -178,51 +180,42 @@ public bool Download()
178
180
}
179
181
}
180
182
181
- webRequest = WebRequest . Create ( Url ) ;
182
- var httpWebRequest = webRequest as HttpWebRequest ;
183
- if ( httpWebRequest != null )
183
+ var expectingResume = restarted && bytes > 0 ;
184
+
185
+ var webRequest = ( HttpWebRequest ) WebRequest . Create ( Url ) ;
186
+
187
+ if ( expectingResume )
184
188
{
185
- if ( bytes > 0 )
186
- {
187
- // TODO: fix classlibs to take long overloads
188
- httpWebRequest . AddRange ( ( int ) bytes ) ;
189
- }
189
+ // TODO: fix classlibs to take long overloads
190
+ webRequest . AddRange ( ( int ) bytes ) ;
190
191
}
191
192
192
193
webRequest . Method = "GET" ;
193
194
webRequest . Timeout = 3000 ;
194
195
195
- if ( restarted && bytes > 0 )
196
+ if ( expectingResume )
196
197
Logger . Trace ( $ "Resuming download of { Url } to { Destination } ") ;
197
198
else
198
199
Logger . Trace ( $ "Downloading { Url } to { Destination } ") ;
199
200
200
- using ( var webResponse = webRequest . GetResponseWithoutException ( ) )
201
+ using ( var webResponse = ( HttpWebResponse ) webRequest . GetResponseWithoutException ( ) )
201
202
{
202
- if ( webResponse == null )
203
- return false ;
203
+ var httpStatusCode = webResponse . StatusCode ;
204
+ Logger . Trace ( $ "Downloading { Url } StatusCode: { ( int ) webResponse . StatusCode } " ) ;
204
205
205
- if ( restarted && bytes > 0 )
206
+ if ( expectingResume && httpStatusCode == HttpStatusCode . RequestedRangeNotSatisfiable )
206
207
{
207
- var httpWebResponse = webResponse as HttpWebResponse ;
208
- if ( httpWebResponse != null )
209
- {
210
- var httpStatusCode = httpWebResponse . StatusCode ;
211
- if ( httpStatusCode == HttpStatusCode . RequestedRangeNotSatisfiable )
212
- {
213
- UpdateProgress ( 1 ) ;
214
- return true ;
215
- }
208
+ UpdateProgress ( 1 ) ;
209
+ return true ;
210
+ }
216
211
217
- if ( ! ( httpStatusCode == HttpStatusCode . OK || httpStatusCode == HttpStatusCode . PartialContent ) )
218
- {
219
- return false ;
220
- }
221
- }
212
+ if ( ! ( httpStatusCode == HttpStatusCode . OK || httpStatusCode == HttpStatusCode . PartialContent ) )
213
+ {
214
+ return false ;
222
215
}
223
216
224
217
var responseLength = webResponse . ContentLength ;
225
- if ( restarted && bytes > 0 )
218
+ if ( expectingResume )
226
219
{
227
220
UpdateProgress ( bytes / ( float ) responseLength ) ;
228
221
}
@@ -249,6 +242,15 @@ public bool Download()
249
242
protected int RetryCount { get ; }
250
243
}
251
244
245
+ class DownloadException : Exception
246
+ {
247
+ public DownloadException ( string message ) : base ( message )
248
+ { }
249
+
250
+ public DownloadException ( string message , Exception innerException ) : base ( message , innerException )
251
+ { }
252
+ }
253
+
252
254
class DownloadTextTask : TaskBase < string >
253
255
{
254
256
public float Progress { get ; set ; }
0 commit comments