Implemented INeedsLogin in MegaDownloader

This commit is contained in:
erri120 2020-04-02 18:41:50 +02:00
parent 0dc18334ac
commit 9eac8d7641
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
2 changed files with 44 additions and 17 deletions

View File

@ -1,11 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive; using System.Reactive;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ReactiveUI; using ReactiveUI;
namespace Wabbajack.Lib.Downloaders namespace Wabbajack.Lib.Downloaders

View File

@ -1,12 +1,29 @@
using System; using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using CG.Web.MegaApiClient; using CG.Web.MegaApiClient;
using ReactiveUI;
using Wabbajack.Common; using Wabbajack.Common;
namespace Wabbajack.Lib.Downloaders 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<AbstractDownloadState> GetDownloaderState(dynamic archiveINI) public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
{ {
var url = archiveINI?.General?.directURL; var url = archiveINI?.General?.directURL;
@ -16,7 +33,7 @@ namespace Wabbajack.Lib.Downloaders
public AbstractDownloadState GetDownloaderState(string url) public AbstractDownloadState GetDownloaderState(string url)
{ {
if (url != null && url.StartsWith(Consts.MegaPrefix)) if (url != null && url.StartsWith(Consts.MegaPrefix))
return new State { Url = url }; return new State { Url = url, MegaApiClient = MegaApiClient};
return null; return null;
} }
@ -26,27 +43,35 @@ namespace Wabbajack.Lib.Downloaders
public class State : HTTPDownloader.State public class State : HTTPDownloader.State
{ {
public MegaApiClient MegaApiClient;
public override async Task<bool> Download(Archive a, string destination) public override async Task<bool> Download(Archive a, string destination)
{ {
var client = new MegaApiClient(); if (!MegaApiClient.IsLoggedIn)
Utils.Status("Logging into MEGA (as anonymous)"); {
client.LoginAnonymous(); Utils.Status("Logging into MEGA (as anonymous)");
MegaApiClient.LoginAnonymous();
}
var fileLink = new Uri(Url); var fileLink = new Uri(Url);
var node = client.GetNodeFromLink(fileLink); var node = MegaApiClient.GetNodeFromLink(fileLink);
Utils.Status($"Downloading MEGA file: {a.Name}"); Utils.Status($"Downloading MEGA file: {a.Name}");
client.DownloadFile(fileLink, destination); MegaApiClient.DownloadFile(fileLink, destination);
return true; return true;
} }
public override async Task<bool> Verify(Archive a) public override async Task<bool> Verify(Archive a)
{ {
var client = new MegaApiClient(); if (!MegaApiClient.IsLoggedIn)
Utils.Status("Logging into MEGA (as anonymous)"); {
client.LoginAnonymous(); Utils.Status("Logging into MEGA (as anonymous)");
MegaApiClient.LoginAnonymous();
}
var fileLink = new Uri(Url); var fileLink = new Uri(Url);
try try
{ {
var node = client.GetNodeFromLink(fileLink); var node = MegaApiClient.GetNodeFromLink(fileLink);
return true; return true;
} }
catch (Exception) catch (Exception)
@ -55,5 +80,13 @@ namespace Wabbajack.Lib.Downloaders
} }
} }
} }
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");
} }
} }