diff --git a/Wabbajack.Lib/Downloaders/INeedsLogin.cs b/Wabbajack.Lib/Downloaders/INeedsLogin.cs index fb01a4a8..6d7fae66 100644 --- a/Wabbajack.Lib/Downloaders/INeedsLogin.cs +++ b/Wabbajack.Lib/Downloaders/INeedsLogin.cs @@ -1,11 +1,5 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; using System.Reactive; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Input; using ReactiveUI; namespace Wabbajack.Lib.Downloaders diff --git a/Wabbajack.Lib/Downloaders/MEGADownloader.cs b/Wabbajack.Lib/Downloaders/MEGADownloader.cs index 81757ee6..6c49691a 100644 --- a/Wabbajack.Lib/Downloaders/MEGADownloader.cs +++ b/Wabbajack.Lib/Downloaders/MEGADownloader.cs @@ -1,12 +1,29 @@ using System; +using System.Reactive; +using System.Reactive.Linq; using System.Threading.Tasks; using CG.Web.MegaApiClient; +using ReactiveUI; using Wabbajack.Common; namespace Wabbajack.Lib.Downloaders { - public class MegaDownloader : IDownloader, IUrlDownloader + public class MegaDownloader : IUrlDownloader, INeedsLogin { + public MegaApiClient MegaApiClient; + private const string DataName = "mega-cred"; + + public MegaDownloader() + { + MegaApiClient = new MegaApiClient(); + + TriggerLogin = ReactiveCommand.Create(() => { }, + IsLoggedIn.Select(b => !b).ObserveOnGuiThread()); + + ClearLogin = ReactiveCommand.Create(() => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson(DataName)), + IsLoggedIn.ObserveOnGuiThread()); + } + public async Task GetDownloaderState(dynamic archiveINI) { var url = archiveINI?.General?.directURL; @@ -16,7 +33,7 @@ namespace Wabbajack.Lib.Downloaders public AbstractDownloadState GetDownloaderState(string url) { if (url != null && url.StartsWith(Consts.MegaPrefix)) - return new State { Url = url }; + return new State { Url = url, MegaApiClient = MegaApiClient}; return null; } @@ -26,27 +43,35 @@ namespace Wabbajack.Lib.Downloaders public class State : HTTPDownloader.State { + public MegaApiClient MegaApiClient; + public override async Task Download(Archive a, string destination) { - var client = new MegaApiClient(); - Utils.Status("Logging into MEGA (as anonymous)"); - client.LoginAnonymous(); + if (!MegaApiClient.IsLoggedIn) + { + Utils.Status("Logging into MEGA (as anonymous)"); + MegaApiClient.LoginAnonymous(); + } + var fileLink = new Uri(Url); - var node = client.GetNodeFromLink(fileLink); + var node = MegaApiClient.GetNodeFromLink(fileLink); Utils.Status($"Downloading MEGA file: {a.Name}"); - client.DownloadFile(fileLink, destination); + MegaApiClient.DownloadFile(fileLink, destination); return true; } public override async Task Verify(Archive a) { - var client = new MegaApiClient(); - Utils.Status("Logging into MEGA (as anonymous)"); - client.LoginAnonymous(); + if (!MegaApiClient.IsLoggedIn) + { + Utils.Status("Logging into MEGA (as anonymous)"); + MegaApiClient.LoginAnonymous(); + } + var fileLink = new Uri(Url); try { - var node = client.GetNodeFromLink(fileLink); + var node = MegaApiClient.GetNodeFromLink(fileLink); return true; } catch (Exception) @@ -55,5 +80,13 @@ namespace Wabbajack.Lib.Downloaders } } } + + public ReactiveCommand TriggerLogin { get; } + public ReactiveCommand ClearLogin { get; } + public IObservable IsLoggedIn => Utils.HaveEncryptedJsonObservable(DataName); + public string SiteName => "MEGA"; + public IObservable MetaInfo => Observable.Return(""); + public Uri SiteURL => new Uri("https://mega.nz/"); + public Uri IconUri => new Uri("https://mega.nz/favicon.ico"); } }