49 lines
1.4 KiB
C#
Raw Normal View History

using CG.Web.MegaApiClient;
using SPTInstaller.Helpers;
using System.Threading.Tasks;
using Serilog;
namespace SPTInstaller.Models.Mirrors.Downloaders;
2024-05-01 10:31:55 -04:00
public class MegaMirrorDownloader : MirrorDownloaderBase
{
2024-04-27 14:51:54 -04:00
public MegaMirrorDownloader(PatchInfoMirror mirrorInfo) : base(mirrorInfo)
{
}
2024-05-01 10:31:55 -04:00
public override async Task<FileInfo?> Download(IProgress<double> progress)
{
var megaClient = new MegaApiClient();
await megaClient.LoginAnonymousAsync();
2024-05-01 10:31:55 -04:00
// if mega fails to connect, just return
if (!megaClient.IsLoggedIn)
return null;
2024-05-01 10:31:55 -04:00
try
{
var file = new FileInfo(Path.Join(DownloadCacheHelper.CachePath, "patcher"));
2024-05-01 10:31:55 -04:00
if (file.Exists)
{
file.Delete();
}
await megaClient.DownloadFileAsync(new Uri(MirrorInfo.Link),
file.FullName, progress);
file.Refresh();
2024-05-01 10:31:55 -04:00
if (!file.Exists)
2023-09-21 18:51:20 -04:00
return null;
2024-05-01 10:31:55 -04:00
2023-09-21 18:51:20 -04:00
return FileHashHelper.CheckHash(file, MirrorInfo.Hash) ? file : null;
}
2024-05-01 10:31:55 -04:00
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;
}
}
2024-05-01 10:31:55 -04:00
}