diff options
6 files changed, 56 insertions, 88 deletions
diff --git a/src/SMAPI.Web/Controllers/LogParserController.cs b/src/SMAPI.Web/Controllers/LogParserController.cs index 0556a81e..2a071b6c 100644 --- a/src/SMAPI.Web/Controllers/LogParserController.cs +++ b/src/SMAPI.Web/Controllers/LogParserController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Clients.Pastebin; using StardewModdingAPI.Web.Framework.Compression; @@ -59,14 +60,14 @@ namespace StardewModdingAPI.Web.Controllers { // fresh page if (string.IsNullOrWhiteSpace(id)) - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, id)); + return this.View("Index", this.GetModel(id)); // log page PasteInfo paste = await this.GetAsync(id); ParsedLog log = paste.Success ? new LogParser().Parse(paste.Content) : new ParsedLog { IsValid = false, Error = "Pastebin error: " + paste.Error }; - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, id, log, raw)); + return this.View("Index", this.GetModel(id).SetResult(log, raw)); } /*** @@ -80,7 +81,7 @@ namespace StardewModdingAPI.Web.Controllers // get raw log text string input = this.Request.Form["input"].FirstOrDefault(); if (string.IsNullOrWhiteSpace(input)) - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, null) { UploadError = "The log file seems to be empty." }); + return this.View("Index", this.GetModel(null, uploadError: "The log file seems to be empty.")); // upload log input = this.GzipHelper.CompressString(input); @@ -88,7 +89,7 @@ namespace StardewModdingAPI.Web.Controllers // handle errors if (!result.Success) - return this.View("Index", new LogParserModel(this.Config.LogParserUrl, result.ID) { UploadError = $"Pastebin error: {result.Error ?? "unknown error"}" }); + return this.View("Index", this.GetModel(result.ID, uploadError: $"Pastebin error: {result.Error ?? "unknown error"}")); // redirect to view UriBuilder uri = new UriBuilder(new Uri(this.Config.LogParserUrl)); @@ -108,5 +109,39 @@ namespace StardewModdingAPI.Web.Controllers response.Content = this.GzipHelper.DecompressString(response.Content); return response; } + + /// <summary>Construct an instance.</summary> + /// <param name="pasteID">The paste ID.</param> + /// <param name="uploadError">An error which occurred while uploading the log to Pastebin.</param> + private LogParserModel GetModel(string pasteID, string uploadError = null) + { + string sectionUrl = this.Config.LogParserUrl; + Platform? platform = this.DetectClientPlatform(); + return new LogParserModel(sectionUrl, pasteID, platform) { UploadError = uploadError }; + } + + /// <summary>Detect the viewer's OS.</summary> + /// <returns>Returns the viewer OS if known, else null.</returns> + private Platform? DetectClientPlatform() + { + string userAgent = this.Request.Headers["User-Agent"]; + switch (userAgent) + { + case string ua when ua.Contains("Windows"): + return Platform.Windows; + + case string ua when ua.Contains("Android"): // check for Android before Linux because Android user agents also contain Linux + return Platform.Android; + + case string ua when ua.Contains("Linux"): + return Platform.Linux; + + case string ua when ua.Contains("Mac"): + return Platform.Mac; + + default: + return null; + } + } } } diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs deleted file mode 100644 index 2bf23ff9..00000000 --- a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using StardewModdingAPI.Toolkit.Utilities; - -namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection -{ - /// <summary>Middleware that detects the client's platform.</summary> - public class ClientPlatformMiddleware - { - /// <summary>The key used to retrieve the client's platform from <see cref="HttpContext.Items"/>.</summary> - public const string ClientPlatformKey = "ClientPlatformKey"; - - /// <summary>The next delegate in the middleware pipeline.</summary> - private readonly RequestDelegate Next; - - /// <summary>Construct an instance.</summary> - /// <param name="next">The next delegate in the middleware pipeline.</param> - public ClientPlatformMiddleware(RequestDelegate next) - { - this.Next = next; - } - - /// <summary>Invoke the middleware.</summary> - /// <param name="context">The HTTP request context.</param> - public async Task InvokeAsync(HttpContext context) - { - context.Items[ClientPlatformMiddleware.ClientPlatformKey] = this.DetectClientPlatform(context.Request.Headers["User-Agent"]); - - await this.Next(context); - } - - /// <summary>Detect the platform that the client is on.</summary> - /// <param name="userAgent">The client's user agent.</param> - /// <returns>The client's platform, or null if no platforms could be detected.</returns> - private Platform? DetectClientPlatform(string userAgent) - { - switch (userAgent) - { - case string ua when ua.Contains("Windows"): - return Platform.Windows; - // check for Android before Linux because Android user agents also contain Linux - case string ua when ua.Contains("Android"): - return Platform.Android; - case string ua when ua.Contains("Linux"): - return Platform.Linux; - case string ua when ua.Contains("Mac"): - return Platform.Mac; - default: - return null; - } - } - } -} diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs deleted file mode 100644 index 274e5486..00000000 --- a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Builder; - -namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection -{ - /// <summary>Extension methods for the client platform middleware.</summary> - internal static class ClientPlatformMiddlewareExtensions - { - /// <summary>Adds client platform detection to the request pipeline.</summary> - /// <param name="builder">The application builder.</param> - /// <returns>The application builder with the client platform middleware enabled.</returns> - public static IApplicationBuilder UseClientPlatform(this IApplicationBuilder builder) - { - return builder.UseMiddleware<ClientPlatformMiddleware>(); - } - } -} diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs index e12fa63b..da5c1f1b 100644 --- a/src/SMAPI.Web/Startup.cs +++ b/src/SMAPI.Web/Startup.cs @@ -15,7 +15,6 @@ using StardewModdingAPI.Web.Framework; using StardewModdingAPI.Web.Framework.Caching; using StardewModdingAPI.Web.Framework.Caching.Mods; using StardewModdingAPI.Web.Framework.Caching.Wiki; -using StardewModdingAPI.Web.Framework.ClientPlatformDetection; using StardewModdingAPI.Web.Framework.Clients.Chucklefish; using StardewModdingAPI.Web.Framework.Clients.GitHub; using StardewModdingAPI.Web.Framework.Clients.ModDrop; @@ -173,7 +172,6 @@ namespace StardewModdingAPI.Web ) .UseRewriter(this.GetRedirectRules()) .UseStaticFiles() // wwwroot folder - .UseClientPlatform() .UseMvc(); // enable Hangfire dashboard diff --git a/src/SMAPI.Web/ViewModels/LogParserModel.cs b/src/SMAPI.Web/ViewModels/LogParserModel.cs index 25493a29..f4c5214b 100644 --- a/src/SMAPI.Web/ViewModels/LogParserModel.cs +++ b/src/SMAPI.Web/ViewModels/LogParserModel.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using StardewModdingAPI.Toolkit.Utilities; using StardewModdingAPI.Web.Framework.LogParsing.Models; namespace StardewModdingAPI.Web.ViewModels @@ -24,6 +25,9 @@ namespace StardewModdingAPI.Web.ViewModels /// <summary>The paste ID.</summary> public string PasteID { get; set; } + /// <summary>The viewer's detected OS, if known.</summary> + public Platform? DetectedPlatform { get; set; } + /// <summary>The parsed log info.</summary> public ParsedLog ParsedLog { get; set; } @@ -46,24 +50,25 @@ namespace StardewModdingAPI.Web.ViewModels /// <summary>Construct an instance.</summary> /// <param name="sectionUrl">The root URL for the log parser controller.</param> /// <param name="pasteID">The paste ID.</param> - public LogParserModel(string sectionUrl, string pasteID) + /// <param name="platform">The viewer's detected OS, if known.</param> + public LogParserModel(string sectionUrl, string pasteID, Platform? platform) { this.SectionUrl = sectionUrl; this.PasteID = pasteID; + this.DetectedPlatform = platform; this.ParsedLog = null; this.ShowRaw = false; } - /// <summary>Construct an instance.</summary> - /// <param name="sectionUrl">The root URL for the log parser controller.</param> - /// <param name="pasteID">The paste ID.</param> + /// <summary>Set the log parser result.</summary> /// <param name="parsedLog">The parsed log info.</param> /// <param name="showRaw">Whether to show the raw unparsed log.</param> - public LogParserModel(string sectionUrl, string pasteID, ParsedLog parsedLog, bool showRaw) - : this(sectionUrl, pasteID) + public LogParserModel SetResult(ParsedLog parsedLog, bool showRaw) { this.ParsedLog = parsedLog; this.ShowRaw = showRaw; + + return this; } /// <summary>Get all content packs in the log grouped by the mod they're for.</summary> diff --git a/src/SMAPI.Web/Views/LogParser/Index.cshtml b/src/SMAPI.Web/Views/LogParser/Index.cshtml index 42887019..9a06d85a 100644 --- a/src/SMAPI.Web/Views/LogParser/Index.cshtml +++ b/src/SMAPI.Web/Views/LogParser/Index.cshtml @@ -1,6 +1,5 @@ @using Newtonsoft.Json @using StardewModdingAPI.Toolkit.Utilities -@using StardewModdingAPI.Web.Framework.ClientPlatformDetection @using StardewModdingAPI.Web.Framework.LogParsing.Models @model StardewModdingAPI.Web.ViewModels.LogParserModel @@ -69,13 +68,13 @@ else if (Model.ParsedLog?.IsValid == true) <h2>Where do I find my SMAPI log?</h2> <div>What system do you use?</div> <ul id="os-list"> - @{ - Platform? clientPlatform = Context.Items[ClientPlatformMiddleware.ClientPlatformKey] as Platform?; + @foreach (Platform platform in new[] { Platform.Android, Platform.Linux, Platform.Mac, Platform.Windows }) + { + <li> + <input type="radio" name="os" value="@platform" id="os-@platform" checked="@(Model.DetectedPlatform == platform)"/> + <label for="os-@platform">@platform</label> + </li> } - <li><input type="radio" name="os" value="android" id="os-android" @(clientPlatform == Platform.Android ? "checked" : "")/> <label for="os-android">Android</label></li> - <li><input type="radio" name="os" value="linux" id="os-linux" @(clientPlatform == Platform.Linux ? "checked" : "")/> <label for="os-linux">Linux</label></li> - <li><input type="radio" name="os" value="mac" id="os-mac" @(clientPlatform == Platform.Mac ? "checked" : "")/> <label for="os-mac">Mac</label></li> - <li><input type="radio" name="os" value="windows" id="os-windows" @(clientPlatform == Platform.Windows ? "checked" : "")/> <label for="os-windows">Windows</label></li> </ul> <div data-os="android"> On Android: |