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"; } } }