Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace GeneralUpdate.Core.Download.Abstractions;
public interface IDownloadExecutor
{
Task<DownloadResult> ExecuteAsync(
string url, string destPath,
DownloadAsset asset, string destPath,
IProgress<DownloadProgress>? progress = null,
CancellationToken token = default);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public HttpDownloadExecutor(HttpClient client, TimeSpan? timeout = null, bool en
}

public async Task<DownloadResult> ExecuteAsync(
string url, string destPath,
DownloadAsset asset, string destPath,
IProgress<DownloadProgress>? progress = null,
CancellationToken token = default)
{
Expand All @@ -42,7 +42,7 @@ public async Task<DownloadResult> ExecuteAsync(

try
{
using var request = new HttpRequestMessage(HttpMethod.Get, url);
using var request = new HttpRequestMessage(HttpMethod.Get, asset.Url);
if (_enableResume && existingBytes > 0)
request.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(existingBytes, null);

Expand Down Expand Up @@ -74,12 +74,12 @@ public async Task<DownloadResult> ExecuteAsync(
totalBytes > 0 ? totalBytes + existingBytes : null,
100, DownloadStatus.Completed));

return new DownloadResult(url, destPath, downloaded, elapsed, retries, true, null);
return new DownloadResult(asset, destPath, downloaded, elapsed, retries, true, null);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
sw.Stop();
return new DownloadResult(url, null, existingBytes, sw.Elapsed, retries, false, ex.Message);
return new DownloadResult(asset, null, existingBytes, sw.Elapsed, retries, false, ex.Message);
}
Comment on lines 79 to 83
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public OssDownloadExecutor(HttpClient client)
=> _client = client ?? throw new ArgumentNullException(nameof(client));

public async Task<DownloadResult> ExecuteAsync(
string url, string destPath,
DownloadAsset asset, string destPath,
IProgress<DownloadProgress>? progress = null,
CancellationToken token = default)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
try
{
using var response = await _client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token)
using var response = await _client.GetAsync(asset.Url, HttpCompletionOption.ResponseHeadersRead, token)
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var total = response.Content.Headers.ContentLength ?? -1;
Expand All @@ -37,12 +37,12 @@ public async Task<DownloadResult> ExecuteAsync(

progress?.Report(new DownloadProgress(
Path.GetFileName(destPath), downloaded, total > 0 ? total : null, 100, DownloadStatus.Completed));
return new DownloadResult(url, destPath, downloaded, elapsed, 0, true, null);
return new DownloadResult(asset, destPath, downloaded, elapsed, 0, true, null);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
sw.Stop();
return new DownloadResult(url, null, 0, sw.Elapsed, 0, false, ex.Message);
return new DownloadResult(asset, null, 0, sw.Elapsed, 0, false, ex.Message);
}
Comment on lines 42 to 46
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DownloadStatus Status
);

public record DownloadResult(
string? Url,
DownloadAsset Asset,
string? LocalPath,
long DownloadedBytes,
Comment on lines 17 to 20
TimeSpan Duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<DownloadReport> ExecuteAsync(
{
// Download
var downloadResult = await executor.ExecuteAsync(
asset.Url, destPath,
asset, destPath,
progress != null ? new AssetProgressReporter(progress, asset.Name) : null,
ct).ConfigureAwait(false);

Expand All @@ -100,7 +100,7 @@ public async Task<DownloadReport> ExecuteAsync(
}
catch (Exception ex)
{
return new DownloadResult(asset.Url, destPath,
return new DownloadResult(asset, destPath,
downloadResult.DownloadedBytes, downloadResult.Duration,
downloadResult.RetryCount, false, $"SHA256 verification failed: {ex.Message}");
}
Expand All @@ -122,7 +122,7 @@ public async Task<DownloadReport> ExecuteAsync(

// Dispatch all-completed event ONCE after all assets finish (only failed results)
var failedDetails = results.Where(r => !r.Success)
.Select(r => ((object)r.Url, r.ErrorMessage ?? "failed")).ToList();
.Select(r => ((object)r.Asset.Url, r.ErrorMessage ?? "failed")).ToList();
Comment on lines 124 to +125
DownloadProgressReporter.DispatchAllCompleted(
this,
results.All(r => r.Success),
Expand Down
4 changes: 2 additions & 2 deletions tests/CoreTest/Bootstrap/BootstrapFullParameterMatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ private sealed class StubDownloadPolicy : GeneralUpdate.Core.Download.Abstractio

private sealed class StubDownloadExecutor : GeneralUpdate.Core.Download.Abstractions.IDownloadExecutor
{
public Task<GeneralUpdate.Core.Download.Models.DownloadResult> ExecuteAsync(string url, string destPath,
public Task<GeneralUpdate.Core.Download.Models.DownloadResult> ExecuteAsync(GeneralUpdate.Core.Download.Models.DownloadAsset asset, string destPath,
IProgress<GeneralUpdate.Core.Download.Models.DownloadProgress>? progress = null, CancellationToken token = default)
=> Task.FromResult(new GeneralUpdate.Core.Download.Models.DownloadResult(url, destPath, 0, TimeSpan.Zero, 0, true, null));
=> Task.FromResult(new GeneralUpdate.Core.Download.Models.DownloadResult(asset, destPath, 0, TimeSpan.Zero, 0, true, null));
}

private sealed class StubDownloadSource : GeneralUpdate.Core.Download.Abstractions.IDownloadSource
Expand Down
4 changes: 2 additions & 2 deletions tests/CoreTest/Configuration/ConfigurationModelsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void DownloadProgress_Failed()
public void DownloadResult_Success()
{
var result = new DownloadResult(
"https://cdn.example.com/update.zip",
null!,
"/tmp/update.zip",
50L * 1024 * 1024,
TimeSpan.FromSeconds(30),
Expand All @@ -236,7 +236,7 @@ public void DownloadResult_Success()
public void DownloadResult_FailureWithRetries()
{
var result = new DownloadResult(
"https://cdn.example.com/update.zip",
null!,
null,
0,
TimeSpan.FromSeconds(15),
Expand Down
6 changes: 4 additions & 2 deletions tests/CoreTest/Download/OrchestratorOptionsBehaviourTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ public async Task HttpDownloadExecutor_EnableResumeFalse_DeletesExistingFile()

try
{
var asset = new DownloadAsset("file.bin", "http://example.com/file.bin", 0, null, "1.0.0");
var result = await executor.ExecuteAsync(
"http://example.com/file.bin", destPath, token: CancellationToken.None);
asset, destPath, token: CancellationToken.None);
Assert.True(result.Success, $"Download should succeed, error: {result.ErrorMessage}");
Assert.True(File.Exists(destPath));
}
Expand Down Expand Up @@ -308,8 +309,9 @@ public async Task HttpDownloadExecutor_WithRangeResponse_AppendsCorrectly()

try
{
var asset = new DownloadAsset("file.bin", "http://example.com/file.bin", 0, null, "1.0.0");
var result = await executor.ExecuteAsync(
"http://example.com/file.bin", destPath, token: CancellationToken.None);
asset, destPath, token: CancellationToken.None);
Assert.True(result.Success);
// Partial content (30 bytes) appended to existing (20 bytes) = 50 total
var fileInfo = new FileInfo(destPath);
Expand Down
Loading