From adee66b3b4ea111b0082a31108e55726fab10643 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 25 Dec 2017 01:47:10 -0500 Subject: add basic download page (#411) --- src/SMAPI.Web/Controllers/IndexController.cs | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/SMAPI.Web/Controllers/IndexController.cs (limited to 'src/SMAPI.Web/Controllers/IndexController.cs') diff --git a/src/SMAPI.Web/Controllers/IndexController.cs b/src/SMAPI.Web/Controllers/IndexController.cs new file mode 100644 index 00000000..c2c5f2fe --- /dev/null +++ b/src/SMAPI.Web/Controllers/IndexController.cs @@ -0,0 +1,79 @@ +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using StardewModdingAPI.Web.Framework.Clients.GitHub; +using StardewModdingAPI.Web.ViewModels; + +namespace StardewModdingAPI.Web.Controllers +{ + /// Provides an info/download page about SMAPI. + [Route("")] + [Route("install")] + internal class IndexController : Controller + { + /********* + ** Properties + *********/ + /// The GitHub API client. + private readonly IGitHubClient GitHub; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The GitHub API client. + public IndexController(IGitHubClient github) + { + this.GitHub = github; + } + + /// Display the index page. + [HttpGet] + public async Task Index() + { + // fetch latest SMAPI release + GitRelease release = await this.GitHub.GetLatestReleaseAsync("Pathoschild/SMAPI"); + string downloadUrl = this.GetMainDownloadUrl(release); + string devDownloadUrl = this.GetDevDownloadUrl(release); + + // render view + var model = new IndexModel(release.Name, release.Body, downloadUrl, devDownloadUrl); + return this.View(model); + } + + + /********* + ** Private methods + *********/ + /// Get the main download URL for a SMAPI release. + /// The SMAPI release. + private string GetMainDownloadUrl(GitRelease release) + { + // get main download URL + foreach (GitAsset asset in release.Assets ?? new GitAsset[0]) + { + if (Regex.IsMatch(asset.FileName, @"SMAPI-[\d\.]+-installer.zip")) + return asset.DownloadUrl; + } + + // fallback just in case + return "https://github.com/pathoschild/SMAPI/releases"; + } + + /// Get the for-developers download URL for a SMAPI release. + /// The SMAPI release. + private string GetDevDownloadUrl(GitRelease release) + { + // get dev download URL + foreach (GitAsset asset in release.Assets ?? new GitAsset[0]) + { + if (Regex.IsMatch(asset.FileName, @"SMAPI-[\d\.]+-installer-for-developers.zip")) + return asset.DownloadUrl; + } + + // fallback just in case + return "https://github.com/pathoschild/SMAPI/releases"; + } + } +} -- cgit From 3da98ff0a032255d8b567d27554f13abe1dba62e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 25 Dec 2017 02:18:24 -0500 Subject: cache release info (#411) --- src/SMAPI.Web/Controllers/IndexController.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Web/Controllers/IndexController.cs') diff --git a/src/SMAPI.Web/Controllers/IndexController.cs b/src/SMAPI.Web/Controllers/IndexController.cs index c2c5f2fe..5d45118f 100644 --- a/src/SMAPI.Web/Controllers/IndexController.cs +++ b/src/SMAPI.Web/Controllers/IndexController.cs @@ -1,6 +1,8 @@ +using System; using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; using StardewModdingAPI.Web.Framework.Clients.GitHub; using StardewModdingAPI.Web.ViewModels; @@ -14,17 +16,25 @@ namespace StardewModdingAPI.Web.Controllers /********* ** Properties *********/ + /// The cache in which to store release data. + private readonly IMemoryCache Cache; + /// The GitHub API client. private readonly IGitHubClient GitHub; + /// The cache time for release info. + private readonly TimeSpan CacheTime = TimeSpan.FromMinutes(5); + /********* ** Public methods *********/ /// Construct an instance. + /// The cache in which to store release data. /// The GitHub API client. - public IndexController(IGitHubClient github) + public IndexController(IMemoryCache cache, IGitHubClient github) { + this.Cache = cache; this.GitHub = github; } @@ -33,7 +43,11 @@ namespace StardewModdingAPI.Web.Controllers public async Task Index() { // fetch latest SMAPI release - GitRelease release = await this.GitHub.GetLatestReleaseAsync("Pathoschild/SMAPI"); + GitRelease release = await this.Cache.GetOrCreateAsync("latest-smapi-release", async entry => + { + entry.AbsoluteExpiration = DateTimeOffset.UtcNow.Add(this.CacheTime); + return await this.GitHub.GetLatestReleaseAsync("Pathoschild/SMAPI"); + }); string downloadUrl = this.GetMainDownloadUrl(release); string devDownloadUrl = this.GetDevDownloadUrl(release); -- cgit