From 846b7d3f5d711a3591c898b7a851d6b652aceff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Thu, 14 Dec 2023 14:18:56 +0200 Subject: [PATCH] Add support for alternative CDN when downloading packages Since the actual download URL is returned by the Thunderstore API as a redirect, signal the API with a query parameter that an alternative CDN is preferred. Initial plan was that the download provider would take alternative CDN flag as a constructor parameter. This didn't work in practice, since the initialization is done in beforeCreate(), which doesn't seem to support calling methods defined in UtilityMixin. So the method is now called in created(), and provider's state is accessed via getter and setter. Refs TS-2003 --- src/components/mixins/UtilityMixin.vue | 2 ++ .../ror2/downloading/ThunderstoreDownloaderProvider.ts | 9 +++++++++ src/r2mm/downloading/BetterThunderstoreDownloader.ts | 8 +++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/components/mixins/UtilityMixin.vue b/src/components/mixins/UtilityMixin.vue index 6b2f46c62..c035a583d 100644 --- a/src/components/mixins/UtilityMixin.vue +++ b/src/components/mixins/UtilityMixin.vue @@ -5,6 +5,7 @@ import Component from 'vue-class-component'; import R2Error from '../../model/errors/R2Error'; import GameManager from '../../model/game/GameManager'; import Profile from '../../model/Profile'; +import ThunderstoreDownloaderProvider from '../../providers/ror2/downloading/ThunderstoreDownloaderProvider'; import LoggerProvider, { LogSeverity } from '../../providers/ror2/logging/LoggerProvider'; import ThunderstorePackages from '../../r2mm/data/ThunderstorePackages'; import ProfileModList from '../../r2mm/mods/ProfileModList'; @@ -100,6 +101,7 @@ export default class UtilityMixin extends Vue { } } + ThunderstoreDownloaderProvider.instance.setUseAlternativeCdn(useAlt); this.$store.commit("setUseAlternativeCdn", useAlt); } } diff --git a/src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts b/src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts index 5034ce0a7..d900bc163 100644 --- a/src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts +++ b/src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts @@ -10,6 +10,7 @@ import Game from '../../../model/game/Game'; import Profile from '../../../model/Profile'; export default abstract class ThunderstoreDownloaderProvider { + private useAlternativeCdn = false; private static provider: () => ThunderstoreDownloaderProvider; static provide(provided: () => ThunderstoreDownloaderProvider): void { @@ -23,6 +24,14 @@ export default abstract class ThunderstoreDownloaderProvider { return ThunderstoreDownloaderProvider.provider(); } + public getUseAlternativeCdn(): boolean { + return this.useAlternativeCdn; + } + + public setUseAlternativeCdn(value: boolean) { + this.useAlternativeCdn = value; + } + /** * Resolve all downloadable dependencies of a ThunderstoreVersion matching their exact version numbers. * This method is recursive to allow dependency building from nested dependencies. diff --git a/src/r2mm/downloading/BetterThunderstoreDownloader.ts b/src/r2mm/downloading/BetterThunderstoreDownloader.ts index cb9696ef6..fdcf5f5fe 100644 --- a/src/r2mm/downloading/BetterThunderstoreDownloader.ts +++ b/src/r2mm/downloading/BetterThunderstoreDownloader.ts @@ -288,7 +288,13 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader callback(100, StatusEnum.SUCCESS, null); return; } - axios.get(combo.getVersion().getDownloadUrl(), { + + let downloadUrl = combo.getVersion().getDownloadUrl() + if (this.getUseAlternativeCdn()) { + downloadUrl += "?mirror=1"; + } + + axios.get(downloadUrl, { onDownloadProgress: progress => { callback((progress.loaded / progress.total) * 100, StatusEnum.PENDING, null); },