wabbajack/Wabbajack.Lib/Downloaders/MEGADownloader.cs
Justin Swanson 3bac5d2f00 AbstractDownloadState.Download returns bool
There's sort of a theoretical disagreement, still.  Should a failed download throw an exception, or return false?  Users of this function still need to handle/prep for either/or.

Still, this is better than before, where some failures were being swallowed completely
2020-01-18 14:07:27 -06:00

60 lines
1.8 KiB
C#

using System;
using System.Threading.Tasks;
using CG.Web.MegaApiClient;
using Wabbajack.Common;
namespace Wabbajack.Lib.Downloaders
{
public class MegaDownloader : IDownloader, IUrlDownloader
{
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
{
var url = archiveINI?.General?.directURL;
return GetDownloaderState(url);
}
public AbstractDownloadState GetDownloaderState(string url)
{
if (url != null && url.StartsWith(Consts.MegaPrefix))
return new State { Url = url };
return null;
}
public async Task Prepare()
{
}
public class State : HTTPDownloader.State
{
public override async Task<bool> Download(Archive a, string destination)
{
var client = new MegaApiClient();
Utils.Status("Logging into MEGA (as anonymous)");
client.LoginAnonymous();
var fileLink = new Uri(Url);
var node = client.GetNodeFromLink(fileLink);
Utils.Status($"Downloading MEGA file: {a.Name}");
client.DownloadFile(fileLink, destination);
return true;
}
public override async Task<bool> Verify(Archive a)
{
var client = new MegaApiClient();
Utils.Status("Logging into MEGA (as anonymous)");
client.LoginAnonymous();
var fileLink = new Uri(Url);
try
{
var node = client.GetNodeFromLink(fileLink);
return true;
}
catch (Exception)
{
return false;
}
}
}
}
}