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

Commit 668661d

Browse files
Inline some methods
1 parent 3b3debf commit 668661d

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
9797
class DownloadTask: TaskBase<DownloadResult>
9898
{
9999
private long bytes;
100-
private WebRequest request;
100+
private WebRequest webRequest;
101101
private bool restarted;
102102

103103
public float Progress { get; set; }
@@ -119,8 +119,6 @@ protected override DownloadResult RunWithReturn(bool success)
119119
try
120120
{
121121
Logger.Trace("Downloading");
122-
123-
InitializeDownload();
124122
RunDownload();
125123

126124
Logger.Trace("Downloaded");
@@ -146,38 +144,70 @@ protected virtual void UpdateProgress(float progress)
146144

147145
public bool RunDownload()
148146
{
147+
var fileInfo = new FileInfo(Destination);
148+
if (fileInfo.Exists)
149+
{
150+
if (fileInfo.Length > 0)
151+
{
152+
bytes = fileInfo.Length;
153+
restarted = true;
154+
}
155+
else if (fileInfo.Length == 0)
156+
{
157+
fileInfo.Delete();
158+
}
159+
}
160+
161+
webRequest = WebRequest.Create(Url);
162+
var httpWebRequest = webRequest as HttpWebRequest;
163+
if (httpWebRequest != null)
164+
{
165+
if (bytes > 0)
166+
{
167+
// TODO: fix classlibs to take long overloads
168+
httpWebRequest.AddRange((int)bytes);
169+
}
170+
}
171+
172+
webRequest.Method = "GET";
173+
webRequest.Timeout = 3000;
174+
149175
if (restarted && bytes > 0)
150176
Logger.Trace($"Resuming download of {Url} to {Destination}");
151177
else
152178
Logger.Trace($"Downloading {Url} to {Destination}");
153179

154-
using (WebResponse response = request.GetResponseWithoutException())
180+
using (var webResponse = webRequest.GetResponseWithoutException())
155181
{
156-
if (response == null)
182+
if (webResponse == null)
157183
return false;
158184

159-
if (restarted && bytes > 0 && response is HttpWebResponse)
185+
if (restarted && bytes > 0)
160186
{
161-
var httpStatusCode = ((HttpWebResponse)response).StatusCode;
162-
if (httpStatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
187+
var httpWebResponse = webResponse as HttpWebResponse;
188+
if (httpWebResponse != null)
163189
{
164-
UpdateProgress(1);
165-
return true;
166-
}
190+
var httpStatusCode = httpWebResponse.StatusCode;
191+
if (httpStatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
192+
{
193+
UpdateProgress(1);
194+
return true;
195+
}
167196

168-
if (!(httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.PartialContent))
169-
{
170-
return false;
197+
if (!(httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.PartialContent))
198+
{
199+
return false;
200+
}
171201
}
172202
}
173203

174-
var responseLength = response.ContentLength;
204+
var responseLength = webResponse.ContentLength;
175205
if (restarted && bytes > 0)
176206
{
177-
UpdateProgress(bytes / responseLength);
207+
UpdateProgress(bytes / (float) responseLength);
178208
}
179209

180-
using (var responseStream = response.GetResponseStream())
210+
using (var responseStream = webResponse.GetResponseStream())
181211
{
182212
using (Stream destinationStream = new FileStream(Destination, FileMode.Append))
183213
{
@@ -193,34 +223,5 @@ public bool RunDownload()
193223
protected string Url { get; }
194224

195225
protected string Destination { get; }
196-
197-
private void InitializeDownload()
198-
{
199-
var fi = new FileInfo(Destination);
200-
if (fi.Exists)
201-
{
202-
if (fi.Length > 0)
203-
{
204-
bytes = fi.Length;
205-
restarted = true;
206-
}
207-
else if (fi.Length == 0)
208-
{
209-
fi.Delete();
210-
}
211-
}
212-
213-
request = WebRequest.Create(Url);
214-
if (request is HttpWebRequest)
215-
{
216-
//((HttpWebRequest)request).UserAgent = "Unity PackageManager v" + PackageManager.Instance.Version;
217-
218-
if (bytes > 0)
219-
((HttpWebRequest)request).AddRange((int)bytes); // TODO: fix classlibs to take long overloads
220-
}
221-
222-
request.Method = "GET";
223-
request.Timeout = 3000;
224-
}
225226
}
226227
}

0 commit comments

Comments
 (0)