forked from wubbl0rz/VmChamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Downloader.cs
46 lines (34 loc) · 1.22 KB
/
Downloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace VmChamp;
using System.Net;
using Spectre.Console;
public class Downloader
{
private readonly DirectoryInfo _cacheDirectory;
public Downloader(DirectoryInfo cacheDirectory)
{
_cacheDirectory = cacheDirectory;
}
public async Task<FileInfo> DownloadAsync(DistroInfo distroInfo, bool force = false)
{
var targetFile = new FileInfo(Path.Combine(_cacheDirectory.FullName, distroInfo.ImageName + ".qcow2"));
if (targetFile.Exists && !force)
{
AnsiConsole.WriteLine($"Using existing image: {distroInfo.ImageName}");
return targetFile;
}
var uri = new Uri(new Uri(distroInfo.Url), distroInfo.ImageName);
AnsiConsole.WriteLine($"Download: {uri}");
#pragma warning disable SYSLIB0014
var webClient = new WebClient();
#pragma warning restore SYSLIB0014
await AnsiConsole.Progress()
.Columns(new SpinnerColumn(), new PercentageColumn(), new RemainingTimeColumn())
.StartAsync(async ctx =>
{
var task = ctx.AddTask($"progress");
webClient.DownloadProgressChanged += (_, eventArgs) => { task.Value = eventArgs.ProgressPercentage; };
await webClient.DownloadFileTaskAsync(uri, targetFile.FullName);
});
return targetFile;
}
}