2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
|
|
|
using System.CommandLine;
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
2020-02-08 23:53:11 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Wabbajack.Downloaders;
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
|
|
|
using Wabbajack.Paths;
|
|
|
|
using Wabbajack.Paths.IO;
|
2020-02-08 23:53:11 +00:00
|
|
|
|
|
|
|
namespace Wabbajack.CLI.Verbs
|
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
|
|
public class DownloadUrl : IVerb
|
2020-02-08 23:53:11 +00:00
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
private readonly ILogger<DownloadUrl> _logger;
|
|
|
|
private readonly DownloadDispatcher _dispatcher;
|
2020-02-08 23:53:11 +00:00
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
public DownloadUrl(ILogger<DownloadUrl> logger, DownloadDispatcher dispatcher)
|
2020-02-08 23:53:11 +00:00
|
|
|
{
|
2021-09-27 12:42:46 +00:00
|
|
|
_logger = logger;
|
|
|
|
_dispatcher = dispatcher;
|
|
|
|
}
|
2020-02-24 17:05:42 +00:00
|
|
|
|
2021-09-27 12:42:46 +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"));
|
|
|
|
command.Description = "Downloads a file to a given output";
|
|
|
|
command.Handler = CommandHandler.Create(Run);
|
|
|
|
return command;
|
|
|
|
}
|
2020-02-08 23:53:11 +00:00
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
private async Task<int> Run(Uri url, AbsolutePath output)
|
|
|
|
{
|
|
|
|
var parsed = _dispatcher.Parse(url);
|
|
|
|
if (parsed == null)
|
|
|
|
{
|
|
|
|
_logger.LogCritical("No downloader found for {Url}", url);
|
2020-02-08 23:53:11 +00:00
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-02-08 23:53:11 +00:00
|
|
|
|
2021-09-27 12:42:46 +00:00
|
|
|
var archive = new Archive() { State = parsed, Name = output.FileName.ToString() };
|
|
|
|
await _dispatcher.Download(archive, output, CancellationToken.None);
|
2020-02-08 23:53:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|