Skip to content

Commit

Permalink
Add support for alternative CDN when downloading packages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anttimaki committed Dec 14, 2023
1 parent d9a887b commit 846b7d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/components/mixins/UtilityMixin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -100,6 +101,7 @@ export default class UtilityMixin extends Vue {
}
}
ThunderstoreDownloaderProvider.instance.setUseAlternativeCdn(useAlt);
this.$store.commit("setUseAlternativeCdn", useAlt);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion src/r2mm/downloading/BetterThunderstoreDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down

0 comments on commit 846b7d3

Please sign in to comment.