-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor DownloadTarget and DownloadPart
- Loading branch information
Showing
8 changed files
with
223 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using System.IO; | ||
using System.ComponentModel; | ||
|
||
using Autofac; | ||
|
||
using CKAN.Configuration; | ||
|
||
namespace CKAN | ||
{ | ||
public partial class NetAsyncDownloader | ||
{ | ||
// Private utility class for tracking downloads | ||
private class DownloadPart | ||
{ | ||
public readonly DownloadTarget target; | ||
public readonly string path; | ||
|
||
public DateTime lastProgressUpdateTime; | ||
public long lastProgressUpdateSize; | ||
public long bytesLeft; | ||
public long size; | ||
public long bytesPerSecond; | ||
public Exception error; | ||
|
||
// Number of target URLs already tried and failed | ||
private int triedDownloads; | ||
|
||
/// <summary> | ||
/// Percentage, bytes received, total bytes to receive | ||
/// </summary> | ||
public event Action<int, long, long> Progress; | ||
public event Action<object, AsyncCompletedEventArgs, string> Done; | ||
|
||
private string mimeType => target.mimeType; | ||
private ResumingWebClient agent; | ||
|
||
public DownloadPart(DownloadTarget target) | ||
{ | ||
this.target = target; | ||
path = target.filename ?? Path.GetTempFileName(); | ||
size = bytesLeft = target.size; | ||
lastProgressUpdateTime = DateTime.Now; | ||
triedDownloads = 0; | ||
} | ||
|
||
public void Download(Uri url, string path) | ||
{ | ||
ResetAgent(); | ||
// Check whether to use an auth token for this host | ||
if (url.IsAbsoluteUri | ||
&& ServiceLocator.Container.Resolve<IConfiguration>().TryGetAuthToken(url.Host, out string token) | ||
&& !string.IsNullOrEmpty(token)) | ||
{ | ||
log.InfoFormat("Using auth token for {0}", url.Host); | ||
// Send our auth token to the GitHub API (or whoever else needs one) | ||
agent.Headers.Add("Authorization", $"token {token}"); | ||
} | ||
agent.DownloadFileAsyncWithResume(url, path); | ||
} | ||
|
||
public Uri CurrentUri => target.urls[triedDownloads]; | ||
|
||
public bool HaveMoreUris => triedDownloads + 1 < target.urls.Count; | ||
|
||
public void NextUri() | ||
{ | ||
if (HaveMoreUris) | ||
{ | ||
++triedDownloads; | ||
} | ||
} | ||
|
||
public void Abort() | ||
{ | ||
agent?.CancelAsyncOverridden(); | ||
} | ||
|
||
private void ResetAgent() | ||
{ | ||
// This WebClient child class does some complicated stuff, let's keep using it for now | ||
#pragma warning disable SYSLIB0014 | ||
agent = new ResumingWebClient(); | ||
#pragma warning restore SYSLIB0014 | ||
|
||
agent.Headers.Add("User-Agent", Net.UserAgentString); | ||
|
||
// Tell the server what kind of files we want | ||
if (!string.IsNullOrEmpty(mimeType)) | ||
{ | ||
log.InfoFormat("Setting MIME type {0}", mimeType); | ||
agent.Headers.Add("Accept", mimeType); | ||
} | ||
|
||
// Forward progress and completion events to our listeners | ||
agent.DownloadProgressChanged += (sender, args) => | ||
{ | ||
Progress?.Invoke(args.ProgressPercentage, args.BytesReceived, args.TotalBytesToReceive); | ||
}; | ||
agent.DownloadProgress += (percent, bytesReceived, totalBytesToReceive) => | ||
{ | ||
Progress?.Invoke(percent, bytesReceived, totalBytesToReceive); | ||
}; | ||
agent.DownloadFileCompleted += (sender, args) => | ||
{ | ||
Done?.Invoke(sender, args, | ||
args.Cancelled || args.Error != null | ||
? null | ||
: agent.ResponseHeaders?.Get("ETag")?.Replace("\"", "")); | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using ChinhDo.Transactions.FileManager; | ||
|
||
namespace CKAN | ||
{ | ||
public partial class NetAsyncDownloader | ||
{ | ||
public class DownloadTarget | ||
{ | ||
public List<Uri> urls { get; private set; } | ||
public string filename { get; private set; } | ||
public long size { get; set; } | ||
public string mimeType { get; private set; } | ||
|
||
public DownloadTarget(List<Uri> urls, | ||
string filename = null, | ||
long size = 0, | ||
string mimeType = "") | ||
{ | ||
var FileTransaction = new TxFileManager(); | ||
|
||
this.urls = urls; | ||
this.filename = string.IsNullOrEmpty(filename) | ||
? FileTransaction.GetTempFileName() | ||
: filename; | ||
this.size = size; | ||
this.mimeType = mimeType; | ||
} | ||
|
||
public DownloadTarget(Uri url, | ||
string filename = null, | ||
long size = 0, | ||
string mimeType = "") | ||
: this(new List<Uri> { url }, | ||
filename, size, mimeType) | ||
{ | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.