2023-09-21 10:53:01 -04:00
|
|
|
|
using CG.Web.MegaApiClient;
|
|
|
|
|
using SPTInstaller.Helpers;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-03-19 11:29:46 -04:00
|
|
|
|
using Serilog;
|
2023-09-21 10:53:01 -04:00
|
|
|
|
|
|
|
|
|
namespace SPTInstaller.Models.Mirrors.Downloaders;
|
2024-05-01 10:31:55 -04:00
|
|
|
|
|
2023-09-21 10:53:01 -04:00
|
|
|
|
public class MegaMirrorDownloader : MirrorDownloaderBase
|
|
|
|
|
{
|
2024-04-27 14:51:54 -04:00
|
|
|
|
public MegaMirrorDownloader(PatchInfoMirror mirrorInfo) : base(mirrorInfo)
|
2023-09-21 10:53:01 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
2024-05-01 10:31:55 -04:00
|
|
|
|
|
2023-09-21 10:53:01 -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
|
|
|
|
|
2023-09-21 10:53:01 -04:00
|
|
|
|
// if mega fails to connect, just return
|
|
|
|
|
if (!megaClient.IsLoggedIn)
|
|
|
|
|
return null;
|
2024-05-01 10:31:55 -04:00
|
|
|
|
|
2023-09-21 10:53:01 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-03-23 15:54:59 -04:00
|
|
|
|
var file = new FileInfo(Path.Join(DownloadCacheHelper.CachePath, "patcher"));
|
2024-05-01 10:31:55 -04:00
|
|
|
|
|
|
|
|
|
if (file.Exists)
|
2024-03-24 09:14:51 -04:00
|
|
|
|
{
|
|
|
|
|
file.Delete();
|
|
|
|
|
}
|
2024-03-23 15:54:59 -04:00
|
|
|
|
|
|
|
|
|
await megaClient.DownloadFileAsync(new Uri(MirrorInfo.Link),
|
|
|
|
|
file.FullName, progress);
|
|
|
|
|
|
|
|
|
|
file.Refresh();
|
2024-05-01 10:31:55 -04:00
|
|
|
|
|
2024-03-23 15:54:59 -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;
|
2023-09-21 10:53:01 -04:00
|
|
|
|
}
|
2024-05-01 10:31:55 -04:00
|
|
|
|
catch (Exception ex)
|
2023-09-21 10:53:01 -04:00
|
|
|
|
{
|
2024-03-19 11:29:46 -04:00
|
|
|
|
Log.Error(ex, "Exception thrown while downloading from mega");
|
2023-09-21 10:53:01 -04:00
|
|
|
|
//most likely a 509 (Bandwidth limit exceeded) due to mega's user quotas.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-01 10:31:55 -04:00
|
|
|
|
}
|