add mirror downloaders to only handle downloading of a mirror

This commit is contained in:
IsWaffle 2023-09-21 10:53:01 -04:00
parent 84c9f0975a
commit f0e1abc9bf
5 changed files with 71 additions and 1 deletions

View File

@ -0,0 +1,9 @@
using SPTInstaller.Models.Mirrors;
using System.Threading.Tasks;
namespace SPTInstaller.Interfaces;
public interface IMirrorDownloader
{
public DownloadMirror MirrorInfo { get; }
public Task<FileInfo?> Download(IProgress<double> progress);
}

View File

@ -1,4 +1,4 @@
namespace SPTInstaller.Models;
namespace SPTInstaller.Models.Mirrors;
public class DownloadMirror
{

View File

@ -0,0 +1,15 @@
using SPTInstaller.Helpers;
using System.Threading.Tasks;
namespace SPTInstaller.Models.Mirrors.Downloaders;
public class HttpMirrorDownloader : MirrorDownloaderBase
{
public HttpMirrorDownloader(DownloadMirror mirror) : base(mirror)
{
}
public override async Task<FileInfo?> Download(IProgress<double> progress)
{
return await DownloadCacheHelper.DownloadFileAsync("patcher.zip", MirrorInfo.Link, progress);
}
}

View File

@ -0,0 +1,33 @@
using CG.Web.MegaApiClient;
using SPTInstaller.Helpers;
using System.Threading.Tasks;
namespace SPTInstaller.Models.Mirrors.Downloaders;
public class MegaMirrorDownloader : MirrorDownloaderBase
{
public MegaMirrorDownloader(DownloadMirror 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
{
using var megaDownloadStream = await megaClient.DownloadAsync(new Uri(MirrorInfo.Link), progress);
return await DownloadCacheHelper.DownloadFileAsync("patcher.zip", megaDownloadStream);
}
catch
{
//most likely a 509 (Bandwidth limit exceeded) due to mega's user quotas.
return null;
}
}
}

View File

@ -0,0 +1,13 @@
using SPTInstaller.Interfaces;
using System.Threading.Tasks;
namespace SPTInstaller.Models.Mirrors.Downloaders;
public abstract class MirrorDownloaderBase : IMirrorDownloader
{
public DownloadMirror MirrorInfo { get; private set; }
public abstract Task<FileInfo?> Download(IProgress<double> progress);
public MirrorDownloaderBase(DownloadMirror mirrorInfo)
{
MirrorInfo = mirrorInfo;
}
}