2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using CG.Web.MegaApiClient;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-08-21 20:23:11 +00:00
|
|
|
using Wabbajack.Common;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.Downloaders.Interfaces;
|
|
|
|
using Wabbajack.DTOs;
|
|
|
|
using Wabbajack.DTOs.DownloadStates;
|
|
|
|
using Wabbajack.DTOs.Validation;
|
|
|
|
using Wabbajack.Hashing.xxHash64;
|
2023-10-31 03:04:52 +00:00
|
|
|
using Wabbajack.Networking.Http.Interfaces;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.Paths;
|
|
|
|
using Wabbajack.Paths.IO;
|
|
|
|
using Wabbajack.RateLimiter;
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Downloaders.ModDB;
|
|
|
|
|
2022-06-08 03:48:13 +00:00
|
|
|
public class MegaDownloader : ADownloader<Mega>, IUrlDownloader, IProxyable
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
private const string MegaPrefix = "https://mega.nz/#!";
|
|
|
|
private const string MegaFilePrefix = "https://mega.nz/file/";
|
|
|
|
private readonly MegaApiClient _apiClient;
|
|
|
|
private readonly ILogger<MegaDownloader> _logger;
|
2023-10-31 03:04:52 +00:00
|
|
|
private readonly ITokenProvider<MegaToken> _tokenProvider;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
2023-10-31 03:04:52 +00:00
|
|
|
public MegaDownloader(ILogger<MegaDownloader> logger, MegaApiClient apiClient, ITokenProvider<MegaToken> tokenProvider)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
_logger = logger;
|
|
|
|
_apiClient = apiClient;
|
2023-10-31 03:04:52 +00:00
|
|
|
_tokenProvider = tokenProvider;
|
2021-10-23 16:51:17 +00:00
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override async Task<bool> Prepare()
|
|
|
|
{
|
2023-10-31 03:04:52 +00:00
|
|
|
await LoginIfNotLoggedIn();
|
2021-10-23 16:51:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override bool IsAllowed(ServerAllowList allowList, IDownloadState state)
|
|
|
|
{
|
|
|
|
var megaState = (Mega) state;
|
|
|
|
return allowList.AllowedPrefixes.Any(p => megaState.Url.ToString().StartsWith(p));
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override IDownloadState? Resolve(IReadOnlyDictionary<string, string> iniData)
|
|
|
|
{
|
2022-08-21 20:23:11 +00:00
|
|
|
return iniData.ContainsKey("directURL") ? GetDownloaderState(iniData["directURL"].CleanIniString()) : null;
|
2021-10-23 16:51:17 +00:00
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override Priority Priority => Priority.Normal;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public IDownloadState? Parse(Uri uri)
|
|
|
|
{
|
|
|
|
return GetDownloaderState(uri.ToString());
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public Uri UnParse(IDownloadState state)
|
|
|
|
{
|
|
|
|
return ((Mega) state).Url;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2022-06-08 13:12:44 +00:00
|
|
|
public async Task<T> DownloadStream<T>(Archive archive, Func<Stream, Task<T>> fn, CancellationToken token)
|
|
|
|
{
|
|
|
|
var state = archive.State as Mega;
|
2023-10-31 03:04:52 +00:00
|
|
|
await LoginIfNotLoggedIn();
|
|
|
|
|
2022-10-07 22:14:01 +00:00
|
|
|
await using var ins = await _apiClient.DownloadAsync(state!.Url, cancellationToken: token);
|
2022-06-08 13:12:44 +00:00
|
|
|
return await fn(ins);
|
|
|
|
}
|
|
|
|
|
2023-10-31 03:04:52 +00:00
|
|
|
private async Task LoginIfNotLoggedIn()
|
2021-10-23 16:51:17 +00:00
|
|
|
{
|
|
|
|
if (!_apiClient.IsLoggedIn)
|
2023-10-31 03:04:52 +00:00
|
|
|
{
|
|
|
|
if (_tokenProvider.HaveToken())
|
|
|
|
{
|
|
|
|
var authInfo = await _tokenProvider.Get();
|
|
|
|
_logger.LogInformation("Logging into Mega with {Email}", authInfo!.Email);
|
|
|
|
await _apiClient.LoginAsync(authInfo!.Email, authInfo.Password);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Logging into Mega without credentials");
|
|
|
|
await _apiClient.LoginAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 03:59:54 +00:00
|
|
|
|
2023-10-31 03:04:52 +00:00
|
|
|
public override async Task<Hash> Download(Archive archive, Mega state, AbsolutePath destination, IJob job,
|
|
|
|
CancellationToken token)
|
|
|
|
{
|
|
|
|
await LoginIfNotLoggedIn();
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
await using var ous = destination.Open(FileMode.Create, FileAccess.Write, FileShare.None);
|
|
|
|
await using var ins = await _apiClient.DownloadAsync(state.Url, cancellationToken: token);
|
|
|
|
return await ins.HashingCopy(ous, token, job);
|
|
|
|
}
|
2021-10-13 03:59:54 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
private Mega? GetDownloaderState(string? url)
|
|
|
|
{
|
|
|
|
if (url == null) return null;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
if (url.StartsWith(MegaPrefix) || url.StartsWith(MegaFilePrefix))
|
|
|
|
return new Mega {Url = new Uri(url)};
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public override async Task<bool> Verify(Archive archive, Mega archiveState, IJob job, CancellationToken token)
|
|
|
|
{
|
2023-10-31 03:04:52 +00:00
|
|
|
await LoginIfNotLoggedIn();
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
for (var times = 0; times < 5; times++)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var node = await _apiClient.GetNodeFromLinkAsync(archiveState.Url);
|
|
|
|
if (node != null)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Task.Delay(TimeSpan.FromMilliseconds(500), token);
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override IEnumerable<string> MetaIni(Archive a, Mega state)
|
|
|
|
{
|
|
|
|
return new[] {$"directURL={state.Url}"};
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
}
|