add download option to log view

This commit is contained in:
Jesse Plamondon-Willard 2022-01-02 19:59:38 -05:00
parent 1fab386ab1
commit e30e427628
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
4 changed files with 56 additions and 10 deletions

View File

@ -13,6 +13,7 @@
* Improved translations. Thanks to ChulkyBow (added Ukrainian)!
* For the web UI:
* Added log download option.
* Fixed log parser not correctly handling multiple mods having the exact same name.
## 3.13.2

View File

@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using StardewModdingAPI.Toolkit.Utilities;
@ -39,24 +40,42 @@ namespace StardewModdingAPI.Web.Controllers
***/
/// <summary>Render the log parser UI.</summary>
/// <param name="id">The stored file ID.</param>
/// <param name="raw">Whether to display the raw unparsed log.</param>
/// <param name="format">How to render the log view.</param>
/// <param name="renew">Whether to reset the log expiry.</param>
[HttpGet]
[Route("log")]
[Route("log/{id}")]
public async Task<ViewResult> Index(string id = null, bool raw = false, bool renew = false)
public async Task<ActionResult> Index(string id = null, LogViewFormat format = LogViewFormat.Default, bool renew = false)
{
// fresh page
if (string.IsNullOrWhiteSpace(id))
return this.View("Index", this.GetModel(id));
// log page
// fetch log
StoredFileInfo file = await this.Storage.GetAsync(id, renew);
ParsedLog log = file.Success
? new LogParser().Parse(file.Content)
: new ParsedLog { IsValid = false, Error = file.Error };
return this.View("Index", this.GetModel(id, uploadWarning: file.Warning, expiry: file.Expiry).SetResult(log, raw));
// render view
switch (format)
{
case LogViewFormat.Default:
case LogViewFormat.RawView:
{
ParsedLog log = file.Success
? new LogParser().Parse(file.Content)
: new ParsedLog { IsValid = false, Error = file.Error };
return this.View("Index", this.GetModel(id, uploadWarning: file.Warning, expiry: file.Expiry).SetResult(log, showRaw: format == LogViewFormat.RawView));
}
case LogViewFormat.RawDownload:
{
string content = file.Error ?? file.Content;
return this.File(Encoding.UTF8.GetBytes(content), "plain/text", $"SMAPI log ({id}).txt");
}
default:
throw new InvalidOperationException($"Unknown log view format '{format}'.");
}
}
/***

View File

@ -0,0 +1,15 @@
namespace StardewModdingAPI.Web.ViewModels
{
/// <summary>How a log file should be displayed.</summary>
public enum LogViewFormat
{
/// <summary>Render a parsed log and metadata.</summary>
Default,
/// <summary>Render a raw log with parsed metadata.</summary>
RawView,
/// <summary>Render directly as a text file.</summary>
RawDownload
}
}

View File

@ -2,6 +2,7 @@
@using StardewModdingAPI.Toolkit.Utilities
@using StardewModdingAPI.Web.Framework
@using StardewModdingAPI.Web.Framework.LogParsing.Models
@using StardewModdingAPI.Web.ViewModels
@model StardewModdingAPI.Web.ViewModels.LogParserModel
@{
@ -338,14 +339,24 @@ else if (Model.ParsedLog?.IsValid == true)
}
}
</table>
<small><a href="@this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID, raw = true })">view raw log</a></small>
}
else
{
<pre v-pre>@Model.ParsedLog.RawText</pre>
<small><a href="@this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID })">view parsed log</a></small>
}
<small>
@if (Model.ShowRaw)
{
<a href="@this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID })">view parsed log</a>
}
else
{
<a href="@this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID, format = LogViewFormat.RawView })">view raw log</a>
}
| <a href="@this.Url.PlainAction("Index", "LogParser", new { id = Model.PasteID, format = LogViewFormat.RawDownload })" download>download</a>
</small>
</div>
}
else if (Model.ParsedLog?.IsValid == false)