2022-05-16 22:14:52 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-05-19 03:21:38 +00:00
|
|
|
|
using Wabbajack.Common;
|
2022-05-16 22:14:52 +00:00
|
|
|
|
using Wabbajack.Downloaders.Interfaces;
|
2022-02-27 21:31:13 +00:00
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
|
using Wabbajack.DTOs.DownloadStates;
|
2022-05-16 22:14:52 +00:00
|
|
|
|
using Wabbajack.DTOs.Interventions;
|
2022-02-27 21:31:13 +00:00
|
|
|
|
using Wabbajack.DTOs.Validation;
|
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
2023-05-07 20:32:18 +00:00
|
|
|
|
using Wabbajack.Networking.Http.Interfaces;
|
2022-02-27 21:31:13 +00:00
|
|
|
|
using Wabbajack.Paths;
|
2022-05-16 22:14:52 +00:00
|
|
|
|
using Wabbajack.Paths.IO;
|
2022-02-27 21:31:13 +00:00
|
|
|
|
using Wabbajack.RateLimiter;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Downloaders.Manual;
|
|
|
|
|
|
2022-06-20 23:21:04 +00:00
|
|
|
|
public class ManualDownloader : ADownloader<DTOs.DownloadStates.Manual>, IProxyable
|
2022-02-27 21:31:13 +00:00
|
|
|
|
{
|
2022-05-16 22:14:52 +00:00
|
|
|
|
private readonly ILogger<ManualDownloader> _logger;
|
|
|
|
|
private readonly IUserInterventionHandler _interventionHandler;
|
2023-05-07 20:32:18 +00:00
|
|
|
|
private readonly IHttpDownloader _downloader;
|
2022-05-16 22:14:52 +00:00
|
|
|
|
|
2023-05-07 20:32:18 +00:00
|
|
|
|
public ManualDownloader(ILogger<ManualDownloader> logger, IUserInterventionHandler interventionHandler, IHttpDownloader downloader)
|
2022-05-16 22:14:52 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_interventionHandler = interventionHandler;
|
2023-05-07 20:32:18 +00:00
|
|
|
|
_downloader = downloader;
|
2022-05-16 22:14:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<Hash> Download(Archive archive, DTOs.DownloadStates.Manual state, AbsolutePath destination, IJob job, CancellationToken token)
|
2022-02-27 21:31:13 +00:00
|
|
|
|
{
|
2022-05-16 22:14:52 +00:00
|
|
|
|
_logger.LogInformation("Starting manual download of {Url}", state.Url);
|
|
|
|
|
|
2023-05-07 20:32:18 +00:00
|
|
|
|
var intervention = new ManualDownload(archive);
|
|
|
|
|
_interventionHandler.Raise(intervention);
|
|
|
|
|
var browserState = await intervention.Task;
|
2022-05-19 03:21:38 +00:00
|
|
|
|
|
2023-05-07 20:32:18 +00:00
|
|
|
|
var msg = browserState.ToHttpRequestMessage();
|
|
|
|
|
return await _downloader.Download(msg, destination, job, token);
|
2022-02-27 21:31:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 22:14:52 +00:00
|
|
|
|
|
2022-10-07 22:14:01 +00:00
|
|
|
|
public override Task<bool> Prepare()
|
2022-02-27 21:31:13 +00:00
|
|
|
|
{
|
2022-10-07 22:14:01 +00:00
|
|
|
|
return Task.FromResult(true);
|
2022-02-27 21:31:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool IsAllowed(ServerAllowList allowList, IDownloadState state)
|
|
|
|
|
{
|
|
|
|
|
return allowList.AllowedPrefixes.Any(p => ((DTOs.DownloadStates.Manual) state).Url.ToString().StartsWith(p));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDownloadState? Resolve(IReadOnlyDictionary<string, string> iniData)
|
|
|
|
|
{
|
2022-08-21 20:23:11 +00:00
|
|
|
|
if (iniData.ContainsKey("manualURL") && Uri.TryCreate(iniData["manualURL"].CleanIniString(), UriKind.Absolute, out var uri))
|
2022-02-27 21:31:13 +00:00
|
|
|
|
{
|
|
|
|
|
iniData.TryGetValue("prompt", out var prompt);
|
|
|
|
|
|
|
|
|
|
var state = new DTOs.DownloadStates.Manual
|
|
|
|
|
{
|
|
|
|
|
Url = uri,
|
|
|
|
|
Prompt = prompt ?? ""
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Priority Priority { get; } = Priority.Lowest;
|
2022-10-07 22:14:01 +00:00
|
|
|
|
public override Task<bool> Verify(Archive archive, DTOs.DownloadStates.Manual archiveState, IJob job, CancellationToken token)
|
2022-02-27 21:31:13 +00:00
|
|
|
|
{
|
2022-10-07 22:14:01 +00:00
|
|
|
|
return Task.FromResult(true);
|
2022-02-27 21:31:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<string> MetaIni(Archive a, DTOs.DownloadStates.Manual state)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return new[] {$"manualURL={state.Url}", $"prompt={state.Prompt}"};
|
|
|
|
|
}
|
2022-06-20 23:21:04 +00:00
|
|
|
|
|
|
|
|
|
public IDownloadState? Parse(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
return new DTOs.DownloadStates.Manual() {Url = uri};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Uri UnParse(IDownloadState state)
|
|
|
|
|
{
|
|
|
|
|
return (state as DTOs.DownloadStates.Manual)!.Url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<T> DownloadStream<T>(Archive archive, Func<Stream, Task<T>> fn, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2022-02-27 21:31:13 +00:00
|
|
|
|
}
|