2021-02-17 05:46:05 +00:00
|
|
|
|
using System.Linq;
|
2021-02-19 00:09:12 +00:00
|
|
|
|
using System.Net.Http;
|
2021-02-17 05:46:05 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
using System.Threading;
|
2019-12-06 05:29:17 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-05-21 22:12:51 +00:00
|
|
|
|
using Wabbajack.Common.Exceptions;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
2021-02-19 00:09:12 +00:00
|
|
|
|
using Wabbajack.Lib.Http;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using Wabbajack.Lib.Validation;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2021-07-23 10:12:28 +00:00
|
|
|
|
public class GoogleDriveDownloader : IUrlDownloader
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2021-07-23 10:12:28 +00:00
|
|
|
|
var url = archiveINI.General?.directURL;
|
2019-10-16 11:44:45 +00:00
|
|
|
|
return GetDownloaderState(url);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 10:12:28 +00:00
|
|
|
|
private static readonly Regex GDriveRegex = new("((?<=id=)[a-zA-Z0-9_-]*)|(?<=\\/file\\/d\\/)[a-zA-Z0-9_-]*", RegexOptions.Compiled);
|
2021-07-25 12:37:55 +00:00
|
|
|
|
public AbstractDownloadState? GetDownloaderState(string? url)
|
2019-10-16 11:44:45 +00:00
|
|
|
|
{
|
2021-07-25 12:37:55 +00:00
|
|
|
|
if (url == null) return null;
|
|
|
|
|
|
|
|
|
|
if (!url.StartsWith("https://drive.google.com"))
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-07-23 10:12:28 +00:00
|
|
|
|
var match = GDriveRegex.Match(url);
|
2021-07-31 03:29:16 +00:00
|
|
|
|
|
2021-07-25 12:37:55 +00:00
|
|
|
|
return new State(match.ToString());
|
2019-10-12 21:37:16 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 02:45:13 +00:00
|
|
|
|
public async Task Prepare()
|
2019-10-12 22:15:20 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonName("GoogleDriveDownloader")]
|
2019-10-12 21:37:16 +00:00
|
|
|
|
public class State : AbstractDownloadState
|
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string Id { get; }
|
2021-02-09 02:40:51 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public override object[] PrimaryKey => new object[] { Id };
|
|
|
|
|
|
|
|
|
|
public State(string id)
|
|
|
|
|
{
|
|
|
|
|
Id = id;
|
|
|
|
|
}
|
2020-01-01 16:19:06 +00:00
|
|
|
|
|
2019-10-12 21:37:16 +00:00
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
|
|
|
|
return whitelist.GoogleIDs.Contains(Id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public override async Task<bool> Download(Archive a, AbsolutePath destination)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2021-02-09 02:40:51 +00:00
|
|
|
|
var state = await ToHttpState(download: true);
|
2021-02-17 05:46:05 +00:00
|
|
|
|
if (state == null)
|
|
|
|
|
return false;
|
2020-01-18 19:57:26 +00:00
|
|
|
|
return await state.Download(a, destination);
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 02:40:51 +00:00
|
|
|
|
private async Task<HTTPDownloader.State?> ToHttpState(bool download)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2021-02-09 02:40:51 +00:00
|
|
|
|
if (download)
|
2021-02-19 00:09:12 +00:00
|
|
|
|
{
|
2021-02-09 02:40:51 +00:00
|
|
|
|
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 };
|
|
|
|
|
}
|
2021-02-19 00:09:12 +00:00
|
|
|
|
|
2021-02-09 02:40:51 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public override async Task<bool> Verify(Archive a, CancellationToken? token)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2021-02-09 02:40:51 +00:00
|
|
|
|
var state = await ToHttpState(download: false);
|
2021-02-17 05:46:05 +00:00
|
|
|
|
if (state == null)
|
|
|
|
|
return false;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
return await state.Verify(a, token);
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
2019-10-12 22:15:20 +00:00
|
|
|
|
|
|
|
|
|
public override IDownloader GetDownloader()
|
|
|
|
|
{
|
|
|
|
|
return DownloadDispatcher.GetInstance<GoogleDriveDownloader>();
|
|
|
|
|
}
|
2019-10-12 22:54:25 +00:00
|
|
|
|
|
2020-02-02 12:23:07 +00:00
|
|
|
|
public override string GetManifestURL(Archive a)
|
2020-02-02 12:15:29 +00:00
|
|
|
|
{
|
2020-02-02 12:23:07 +00:00
|
|
|
|
return $"https://drive.google.com/uc?id={Id}&export=download";
|
2020-02-02 12:15:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
|
|
|
|
return new [] {"[General]",$"directURL=https://drive.google.com/uc?id={Id}&export=download"};
|
|
|
|
|
}
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|