From ad5bb5b49af49c4668fd30fb2a0e606dcefe4ec0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 27 Oct 2017 19:39:13 -0400 Subject: proxy Pastebin requests through our API instead of third parties, improve error-handling (#358) --- src/SMAPI.Web/Controllers/LogParserController.cs | 54 +++++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Web/Controllers') diff --git a/src/SMAPI.Web/Controllers/LogParserController.cs b/src/SMAPI.Web/Controllers/LogParserController.cs index 4ed8898a..893d9a52 100644 --- a/src/SMAPI.Web/Controllers/LogParserController.cs +++ b/src/SMAPI.Web/Controllers/LogParserController.cs @@ -1,19 +1,69 @@ +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using StardewModdingAPI.Web.Framework; +using StardewModdingAPI.Web.Framework.ConfigModels; +using StardewModdingAPI.Web.Framework.LogParser; namespace StardewModdingAPI.Web.Controllers { /// Provides a web UI and API for parsing SMAPI log files. - [Route("log")] internal class LogParserController : Controller { + /********* + ** Properties + *********/ + /// The underlying Pastebin client. + private readonly PastebinClient PastebinClient; + + /********* ** Public methods *********/ - /// Render the web UI to upload a log file. + /*** + ** Constructor + ***/ + /// Construct an instance. + /// The log parser config settings. + public LogParserController(IOptions configProvider) + { + // init Pastebin client + LogParserConfig config = configProvider.Value; + string version = this.GetType().Assembly.GetName().Version.ToString(3); + string userAgent = string.Format(config.PastebinUserAgent, version); + this.PastebinClient = new PastebinClient(config.PastebinBaseUrl, userAgent, config.PastebinDevKey); + } + + /*** + ** Web UI + ***/ + /// Render the log parser UI. [HttpGet] + [Route("log")] public ViewResult Index() { return this.View("Index"); } + + /*** + ** JSON + ***/ + /// Fetch raw text from Pastebin. + /// The Pastebin paste ID. + [HttpGet, Produces("application/json")] + [Route("log/fetch/{id}")] + public async Task GetAsync(string id) + { + return await this.PastebinClient.GetAsync(id); + } + + /// Save raw log data. + /// The log content to save. + [HttpPost, Produces("application/json"), AllowLargePosts] + [Route("log/save")] + public async Task PostAsync([FromBody] string content) + { + return await this.PastebinClient.PostAsync(content); + } } } -- cgit