wabbajack/Wabbajack.Lib/Downloaders/MEGADownloader.cs

153 lines
5.5 KiB
C#
Raw Normal View History

2019-10-12 18:03:45 +00:00
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Security;
using System.Threading.Tasks;
2019-10-12 18:03:45 +00:00
using CG.Web.MegaApiClient;
using Newtonsoft.Json;
using ReactiveUI;
2019-10-12 18:03:45 +00:00
using Wabbajack.Common;
using Wabbajack.Common.Serialization.Json;
2019-10-12 18:03:45 +00:00
namespace Wabbajack.Lib.Downloaders
2019-10-12 18:03:45 +00:00
{
public class MegaDownloader : IUrlDownloader, INeedsLoginCredentials
2019-10-12 18:03:45 +00:00
{
public MegaApiClient MegaApiClient;
private const string DataName = "mega-cred";
public LoginReturnMessage LoginWithCredentials(string username, SecureString password)
{
MegaApiClient.AuthInfos authInfos;
try
{
authInfos = MegaApiClient.GenerateAuthInfos(username, password.ToNormalString());
username = null;
password = null;
}
catch (ApiException e)
{
return e.ApiResultCode switch
{
ApiResultCode.BadArguments => new LoginReturnMessage($"Email or password was wrong! {e.Message}",
true),
ApiResultCode.InternalError => new LoginReturnMessage(
$"Internal error occured! Please report this to the Wabbajack Team! {e.Message}", true),
_ => new LoginReturnMessage($"Error generating authentication information! {e.Message}", true)
};
}
try
{
MegaApiClient.Login(authInfos);
}
catch (ApiException e)
{
if ((int)e.ApiResultCode == -26)
{
return new LoginReturnMessage("Two-Factor Authentication needs to be disabled before login!", true);
}
return e.ApiResultCode switch
{
ApiResultCode.InternalError => new LoginReturnMessage(
$"Internal error occured! Please report this to the Wabbajack Team! {e.Message}", true),
_ => new LoginReturnMessage($"Error during login: {e.Message}", true)
};
}
if(MegaApiClient.IsLoggedIn)
authInfos.ToEcryptedJson(DataName);
return new LoginReturnMessage("Logged in successfully, you can now close this window.",
!MegaApiClient.IsLoggedIn || !Utils.HaveEncryptedJson(DataName));
}
public MegaDownloader()
{
MegaApiClient = new MegaApiClient();
TriggerLogin = ReactiveCommand.Create(() => { },
IsLoggedIn.Select(b => !b).ObserveOnGuiThread());
ClearLogin = ReactiveCommand.Create(() => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson(DataName)),
IsLoggedIn.ObserveOnGuiThread());
}
2020-04-03 03:57:59 +00:00
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI, bool quickMode)
2019-10-12 18:03:45 +00:00
{
2019-11-21 15:51:57 +00:00
var url = archiveINI?.General?.directURL;
return GetDownloaderState(url);
}
public AbstractDownloadState GetDownloaderState(string url)
{
2019-10-12 18:03:45 +00:00
if (url != null && url.StartsWith(Consts.MegaPrefix))
2020-04-04 22:55:17 +00:00
return new State { Url = url};
2019-10-12 18:03:45 +00:00
return null;
}
2019-12-07 02:45:13 +00:00
public async Task Prepare()
{
}
[JsonName("MegaDownloader")]
public class State : HTTPDownloader.State
2019-10-12 18:03:45 +00:00
{
2020-04-04 22:55:17 +00:00
private static MegaApiClient MegaApiClient => DownloadDispatcher.GetInstance<MegaDownloader>().MegaApiClient;
private void MegaLogin()
2019-10-12 18:03:45 +00:00
{
if (MegaApiClient.IsLoggedIn)
return;
if (!Utils.HaveEncryptedJson(DataName))
{
Utils.Status("Logging into MEGA (as anonymous)");
MegaApiClient.LoginAnonymous();
}
else
{
Utils.Status("Logging into MEGA with saved credentials.");
var authInfo = Utils.FromEncryptedJson<MegaApiClient.AuthInfos>(DataName);
MegaApiClient.Login(authInfo);
}
}
2020-03-25 22:30:43 +00:00
public override async Task<bool> Download(Archive a, AbsolutePath destination)
2019-10-12 18:03:45 +00:00
{
MegaLogin();
2019-11-21 15:51:57 +00:00
var fileLink = new Uri(Url);
2019-10-12 18:03:45 +00:00
Utils.Status($"Downloading MEGA file: {a.Name}");
2020-04-04 22:15:55 +00:00
await MegaApiClient.DownloadFileAsync(fileLink, (string)destination, new Progress<double>(p => Utils.Status($"Downloading MEGA File: {a.Name}", Percent.FactoryPutInRange(p))));
return true;
2019-10-12 18:03:45 +00:00
}
public override async Task<bool> Verify(Archive a)
2019-10-12 18:03:45 +00:00
{
MegaLogin();
2019-11-21 15:51:57 +00:00
var fileLink = new Uri(Url);
2019-10-12 18:03:45 +00:00
try
{
var node = MegaApiClient.GetNodeFromLink(fileLink);
2019-10-12 18:03:45 +00:00
return true;
}
catch (Exception)
{
return false;
}
}
}
public ReactiveCommand<Unit, Unit> TriggerLogin { get; }
public ReactiveCommand<Unit, Unit> ClearLogin { get; }
public IObservable<bool> IsLoggedIn => Utils.HaveEncryptedJsonObservable(DataName);
public string SiteName => "MEGA";
public IObservable<string> MetaInfo => Observable.Return("");
public Uri SiteURL => new Uri("https://mega.nz/");
public Uri IconUri => new Uri("https://mega.nz/favicon.ico");
2019-10-12 18:03:45 +00:00
}
}