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;
using StardewModdingAPI.Web.ViewModels;
namespace StardewModdingAPI.Web.Controllers
{
/// Provides a web UI and API for parsing SMAPI log files.
internal class LogParserController : Controller
{
/*********
** Properties
*********/
/// The log parser config settings.
private readonly LogParserConfig Config;
/// The underlying Pastebin client.
private readonly PastebinClient PastebinClient;
/*********
** Public methods
*********/
/***
** Constructor
***/
/// Construct an instance.
/// The log parser config settings.
public LogParserController(IOptions configProvider)
{
// init Pastebin client
this.Config = configProvider.Value;
string version = this.GetType().Assembly.GetName().Version.ToString(3);
string userAgent = string.Format(this.Config.PastebinUserAgent, version);
this.PastebinClient = new PastebinClient(this.Config.PastebinBaseUrl, userAgent, this.Config.PastebinUserKey, this.Config.PastebinDevKey);
}
/***
** Web UI
***/
/// Render the log parser UI.
/// The paste ID.
[HttpGet]
[Route("log")]
[Route("log/{id}")]
public ViewResult Index(string id = null)
{
return this.View("Index", new LogParserModel(this.Config.SectionUrl, id));
}
/***
** 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);
}
}
}