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 Download(IProgress 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; } } }