From 21f8c72c16bf295ba79ae454c3df9c29a1b14f0f 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 b7003575e..cd8a9174a 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'; @@ -103,6 +104,7 @@ export default class UtilityMixin extends Vue { } this.$store.commit("setPreferredCdn", preferredCdn); + ThunderstoreDownloaderProvider.instance.setPreferredCdn(preferredCdn); } } diff --git a/src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts b/src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts index 5034ce0a7..4e105d078 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 preferredCdn = ""; private static provider: () => ThunderstoreDownloaderProvider; static provide(provided: () => ThunderstoreDownloaderProvider): void { @@ -23,6 +24,14 @@ export default abstract class ThunderstoreDownloaderProvider { return ThunderstoreDownloaderProvider.provider(); } + public getPreferredCdn(): string { + return this.preferredCdn; + } + + public setPreferredCdn(value: string) { + this.preferredCdn = 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..7c41036c1 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.getPreferredCdn()) { + downloadUrl += `?cdn=${this.getPreferredCdn()}`; + } + + axios.get(downloadUrl, { onDownloadProgress: progress => { callback((progress.loaded / progress.total) * 100, StatusEnum.PENDING, null); },