mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Prerequisites - Added DownloadFileAction overload that accepts func
This commit is contained in:
parent
730207ccd0
commit
d6829f5cb5
@ -26,10 +26,29 @@ namespace Artemis.Core
|
|||||||
ShowProgressBar = true;
|
ShowProgressBar = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of a copy folder action
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The name of the action</param>
|
||||||
|
/// <param name="urlFunction">A function returning the URL to download</param>
|
||||||
|
/// <param name="fileName">The target file to save as (will be created if needed)</param>
|
||||||
|
public DownloadFileAction(string name, Func<Task<string>> urlFunction, string fileName) : base(name)
|
||||||
|
{
|
||||||
|
UrlFunction = urlFunction ?? throw new ArgumentNullException(nameof(urlFunction));
|
||||||
|
FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
|
||||||
|
|
||||||
|
ShowProgressBar = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the source URL to download
|
/// Gets the source URL to download
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Url { get; }
|
public string? Url { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the function returning the URL to download
|
||||||
|
/// </summary>
|
||||||
|
public Func<Task<string>>? UrlFunction { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the target file to save as (will be created if needed)
|
/// Gets the target file to save as (will be created if needed)
|
||||||
@ -41,19 +60,20 @@ namespace Artemis.Core
|
|||||||
{
|
{
|
||||||
using HttpClient client = new();
|
using HttpClient client = new();
|
||||||
await using FileStream destinationStream = new(FileName, FileMode.OpenOrCreate);
|
await using FileStream destinationStream = new(FileName, FileMode.OpenOrCreate);
|
||||||
|
string url = Url ?? await UrlFunction();
|
||||||
|
|
||||||
void ProgressOnProgressReported(object? sender, EventArgs e)
|
void ProgressOnProgressReported(object? sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if (Progress.ProgressPerSecond != 0)
|
if (Progress.ProgressPerSecond != 0)
|
||||||
Status = $"Downloading {Url} - {Progress.ProgressPerSecond.Bytes().Humanize("#.##")}/sec";
|
Status = $"Downloading {url} - {Progress.ProgressPerSecond.Bytes().Humanize("#.##")}/sec";
|
||||||
else
|
else
|
||||||
Status = $"Downloading {Url}";
|
Status = $"Downloading {url}";
|
||||||
}
|
}
|
||||||
|
|
||||||
Progress.ProgressReported += ProgressOnProgressReported;
|
Progress.ProgressReported += ProgressOnProgressReported;
|
||||||
|
|
||||||
// Get the http headers first to examine the content length
|
// Get the http headers first to examine the content length
|
||||||
using HttpResponseMessage response = await client.GetAsync(Url, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
using HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||||
await using Stream download = await response.Content.ReadAsStreamAsync(cancellationToken);
|
await using Stream download = await response.Content.ReadAsStreamAsync(cancellationToken);
|
||||||
long? contentLength = response.Content.Headers.ContentLength;
|
long? contentLength = response.Content.Headers.ContentLength;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user