Use a different Google Drive link to verify links during compile

Using the export link to compile is kind of unreliable as the
arbitrary traffic control Google implements will affect how things
work.  Using the /file/d/ link still gives a pretty solid indication
that the file is valid and available while not needing to be able
to download the file at compile time.
This commit is contained in:
Chris Bessent 2021-02-08 19:40:51 -07:00
parent 5092d6325c
commit 945cf4f2dc

View File

@ -56,35 +56,48 @@ namespace Wabbajack.Lib.Downloaders
public override async Task<bool> Download(Archive a, AbsolutePath destination)
{
var state = await ToHttpState();
var state = await ToHttpState(download: true);
if (state == null)
return false;
return await state.Download(a, destination);
}
private async Task<HTTPDownloader.State?> ToHttpState()
private async Task<HTTPDownloader.State?> ToHttpState(bool download)
{
var initialURL = $"https://drive.google.com/uc?id={Id}&export=download";
var client = new Wabbajack.Lib.Http.Client();
using var response = await client.GetAsync(initialURL);
if (!response.IsSuccessStatusCode)
throw new HttpException((int)response.StatusCode, response.ReasonPhrase ?? "Unknown");
var cookies = response.GetSetCookies();
var warning = cookies.FirstOrDefault(c => c.Key.StartsWith("download_warning_"));
response.Dispose();
if (warning == default)
if (download)
{
return new HTTPDownloader.State(initialURL) { Client = client };
}
var initialURL = $"https://drive.google.com/uc?id={Id}&export=download";
var client = new Wabbajack.Lib.Http.Client();
using var response = await client.GetAsync(initialURL);
if (!response.IsSuccessStatusCode)
throw new HttpException((int)response.StatusCode, response.ReasonPhrase ?? "Unknown");
var cookies = response.GetSetCookies();
var warning = cookies.FirstOrDefault(c => c.Key.StartsWith("download_warning_"));
response.Dispose();
if (warning == default)
{
return new HTTPDownloader.State(initialURL) { Client = client };
}
var url = $"https://drive.google.com/uc?export=download&confirm={warning.Value}&id={Id}";
var httpState = new HTTPDownloader.State(url) { Client = client };
return httpState;
var url = $"https://drive.google.com/uc?export=download&confirm={warning.Value}&id={Id}";
var httpState = new HTTPDownloader.State(url) { Client = client };
return httpState;
}
else
{
var url = $"https://drive.google.com/file/d/{Id}/edit";
var client = new Wabbajack.Lib.Http.Client();
using var response = await client.GetAsync(url, errorsAsExceptions: false);
if (!response.IsSuccessStatusCode)
throw new HttpException((int)response.StatusCode, response.ReasonPhrase ?? "Unknown");
var httpState = new HTTPDownloader.State(url) { Client = client };
return httpState;
}
}
public override async Task<bool> Verify(Archive a, CancellationToken? token)
{
var state = await ToHttpState();
var state = await ToHttpState(download: false);
if (state == null)
return false;
return await state.Verify(a, token);