2024-05-01 10:31:55 -04:00

49 lines
1.4 KiB
C#

using CG.Web.MegaApiClient;
using SPTInstaller.Helpers;
using System.Threading.Tasks;
using Serilog;
namespace SPTInstaller.Models.Mirrors.Downloaders;
public class MegaMirrorDownloader : MirrorDownloaderBase
{
public MegaMirrorDownloader(PatchInfoMirror mirrorInfo) : base(mirrorInfo)
{
}
public override async Task<FileInfo?> Download(IProgress<double> progress)
{
var megaClient = new MegaApiClient();
await megaClient.LoginAnonymousAsync();
// if mega fails to connect, just return
if (!megaClient.IsLoggedIn)
return null;
try
{
var file = new FileInfo(Path.Join(DownloadCacheHelper.CachePath, "patcher"));
if (file.Exists)
{
file.Delete();
}
await megaClient.DownloadFileAsync(new Uri(MirrorInfo.Link),
file.FullName, progress);
file.Refresh();
if (!file.Exists)
return null;
return FileHashHelper.CheckHash(file, MirrorInfo.Hash) ? file : null;
}
catch (Exception ex)
{
Log.Error(ex, "Exception thrown while downloading from mega");
//most likely a 509 (Bandwidth limit exceeded) due to mega's user quotas.
return null;
}
}
}