wabbajack/Wabbajack.CLI/Verbs/DownloadUrl.cs

54 lines
1.8 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
2021-09-27 12:42:46 +00:00
using System.Threading;
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
using Microsoft.Extensions.Logging;
2022-02-11 23:40:38 +00:00
using Wabbajack.Common;
2021-09-27 12:42:46 +00:00
using Wabbajack.Downloaders;
using Wabbajack.DTOs;
using Wabbajack.Paths;
2022-02-11 23:40:38 +00:00
using Wabbajack.Paths.IO;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.CLI.Verbs;
public class DownloadUrl : IVerb
{
2021-10-23 16:51:17 +00:00
private readonly DownloadDispatcher _dispatcher;
private readonly ILogger<DownloadUrl> _logger;
2021-10-23 16:51:17 +00:00
public DownloadUrl(ILogger<DownloadUrl> logger, DownloadDispatcher dispatcher)
{
_logger = logger;
_dispatcher = dispatcher;
}
2020-02-24 17:05:42 +00:00
2021-10-23 16:51:17 +00:00
public Command MakeCommand()
{
var command = new Command("download-url");
command.Add(new Option<Uri>(new[] {"-u", "-url"}, "Url to parse"));
command.Add(new Option<AbsolutePath>(new[] {"-o", "-output"}, "Output file"));
2022-08-09 23:41:11 +00:00
command.Add(new Option<bool>(new [] {"-p", "--proxy"}, "Use the Wabbajack Proxy (default: true)"));
2021-10-23 16:51:17 +00:00
command.Description = "Downloads a file to a given output";
command.Handler = CommandHandler.Create(Run);
return command;
}
2022-08-09 23:41:11 +00:00
private async Task<int> Run(Uri url, AbsolutePath output, bool proxy = true)
2021-10-23 16:51:17 +00:00
{
var parsed = _dispatcher.Parse(url);
if (parsed == null)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
_logger.LogCritical("No downloader found for {Url}", url);
2021-10-23 16:51:17 +00:00
return 1;
}
2021-10-23 16:51:17 +00:00
var archive = new Archive() {State = parsed, Name = output.FileName.ToString()};
2022-08-09 23:41:11 +00:00
var hash = await _dispatcher.Download(archive, output, CancellationToken.None, proxy); ;
2022-02-11 23:40:38 +00:00
Console.WriteLine($"Download complete: {output.Size().ToFileSizeString()} {hash} {hash.ToHex()} {(long)hash}");
2021-10-23 16:51:17 +00:00
return 0;
}
2021-09-27 12:42:46 +00:00
}