From f0e1abc9bf69256072fa04571ba1223f89d3407a Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Thu, 21 Sep 2023 10:53:01 -0400 Subject: [PATCH] add mirror downloaders to only handle downloading of a mirror --- SPTInstaller/Interfaces/IMirrorDownloader.cs | 9 +++++ .../Models/{ => Mirrors}/DownloadMirror.cs | 2 +- .../Downloaders/HttpMirrorDownloader.cs | 15 +++++++++ .../Downloaders/MegaMirrorDownloader.cs | 33 +++++++++++++++++++ .../Downloaders/MirrorDownloaderBase.cs | 13 ++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 SPTInstaller/Interfaces/IMirrorDownloader.cs rename SPTInstaller/Models/{ => Mirrors}/DownloadMirror.cs (71%) create mode 100644 SPTInstaller/Models/Mirrors/Downloaders/HttpMirrorDownloader.cs create mode 100644 SPTInstaller/Models/Mirrors/Downloaders/MegaMirrorDownloader.cs create mode 100644 SPTInstaller/Models/Mirrors/Downloaders/MirrorDownloaderBase.cs diff --git a/SPTInstaller/Interfaces/IMirrorDownloader.cs b/SPTInstaller/Interfaces/IMirrorDownloader.cs new file mode 100644 index 0000000..1540620 --- /dev/null +++ b/SPTInstaller/Interfaces/IMirrorDownloader.cs @@ -0,0 +1,9 @@ +using SPTInstaller.Models.Mirrors; +using System.Threading.Tasks; + +namespace SPTInstaller.Interfaces; +public interface IMirrorDownloader +{ + public DownloadMirror MirrorInfo { get; } + public Task Download(IProgress progress); +} diff --git a/SPTInstaller/Models/DownloadMirror.cs b/SPTInstaller/Models/Mirrors/DownloadMirror.cs similarity index 71% rename from SPTInstaller/Models/DownloadMirror.cs rename to SPTInstaller/Models/Mirrors/DownloadMirror.cs index 40a83fd..b44a00a 100644 --- a/SPTInstaller/Models/DownloadMirror.cs +++ b/SPTInstaller/Models/Mirrors/DownloadMirror.cs @@ -1,4 +1,4 @@ -namespace SPTInstaller.Models; +namespace SPTInstaller.Models.Mirrors; public class DownloadMirror { diff --git a/SPTInstaller/Models/Mirrors/Downloaders/HttpMirrorDownloader.cs b/SPTInstaller/Models/Mirrors/Downloaders/HttpMirrorDownloader.cs new file mode 100644 index 0000000..5a7ca2d --- /dev/null +++ b/SPTInstaller/Models/Mirrors/Downloaders/HttpMirrorDownloader.cs @@ -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 Download(IProgress progress) + { + return await DownloadCacheHelper.DownloadFileAsync("patcher.zip", MirrorInfo.Link, progress); + } +} diff --git a/SPTInstaller/Models/Mirrors/Downloaders/MegaMirrorDownloader.cs b/SPTInstaller/Models/Mirrors/Downloaders/MegaMirrorDownloader.cs new file mode 100644 index 0000000..e4d08c1 --- /dev/null +++ b/SPTInstaller/Models/Mirrors/Downloaders/MegaMirrorDownloader.cs @@ -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 Download(IProgress 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; + } + } +} diff --git a/SPTInstaller/Models/Mirrors/Downloaders/MirrorDownloaderBase.cs b/SPTInstaller/Models/Mirrors/Downloaders/MirrorDownloaderBase.cs new file mode 100644 index 0000000..2fb6dcb --- /dev/null +++ b/SPTInstaller/Models/Mirrors/Downloaders/MirrorDownloaderBase.cs @@ -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 Download(IProgress progress); + public MirrorDownloaderBase(DownloadMirror mirrorInfo) + { + MirrorInfo = mirrorInfo; + } +}