From d6829f5cb538aa5fe8c6e2b463ed605fe3058f8f Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Tue, 8 Jun 2021 22:27:47 +0100 Subject: [PATCH] Prerequisites - Added DownloadFileAction overload that accepts func --- .../PrerequisiteAction/DownloadFileAction.cs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/DownloadFileAction.cs b/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/DownloadFileAction.cs index 0707abfd6..db0dbd693 100644 --- a/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/DownloadFileAction.cs +++ b/src/Artemis.Core/Plugins/Prerequisites/PrerequisiteAction/DownloadFileAction.cs @@ -26,10 +26,29 @@ namespace Artemis.Core ShowProgressBar = true; } + /// + /// Creates a new instance of a copy folder action + /// + /// The name of the action + /// A function returning the URL to download + /// The target file to save as (will be created if needed) + public DownloadFileAction(string name, Func> urlFunction, string fileName) : base(name) + { + UrlFunction = urlFunction ?? throw new ArgumentNullException(nameof(urlFunction)); + FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); + + ShowProgressBar = true; + } + /// /// Gets the source URL to download /// - public string Url { get; } + public string? Url { get; } + + /// + /// Gets the function returning the URL to download + /// + public Func>? UrlFunction { get; } /// /// Gets the target file to save as (will be created if needed) @@ -41,19 +60,20 @@ namespace Artemis.Core { using HttpClient client = new(); await using FileStream destinationStream = new(FileName, FileMode.OpenOrCreate); + string url = Url ?? await UrlFunction(); void ProgressOnProgressReported(object? sender, EventArgs e) { if (Progress.ProgressPerSecond != 0) - Status = $"Downloading {Url} - {Progress.ProgressPerSecond.Bytes().Humanize("#.##")}/sec"; + Status = $"Downloading {url} - {Progress.ProgressPerSecond.Bytes().Humanize("#.##")}/sec"; else - Status = $"Downloading {Url}"; + Status = $"Downloading {url}"; } Progress.ProgressReported += ProgressOnProgressReported; // 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); long? contentLength = response.Content.Headers.ContentLength;