detect the client's platform and check the appropriate input in the log parser

This commit is contained in:
Dan Volchek 2019-08-25 17:14:55 -07:00 committed by Jesse Plamondon-Willard
parent 15fd868f59
commit 00b067fead
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
5 changed files with 96 additions and 4 deletions

View File

@ -0,0 +1,52 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace StardewModdingAPI.Web.Framework.UserAgentParsing
{
/// <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;
}
}
}
}

View File

@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Builder;
namespace StardewModdingAPI.Web.Framework.UserAgentParsing
{
/// <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>();
}
}
}

View File

@ -0,0 +1,18 @@
namespace StardewModdingAPI.Web.Framework.UserAgentParsing
{
/// <summary>A software platform.</summary>
public enum Platform
{
/// <summary>The Android platform.</summary>
Android,
/// <summary>The Linux platform.</summary>
Linux,
/// <summary>The Mac platform.</summary>
Mac,
/// <summary>The Windows platform.</summary>
Windows
}
}

View File

@ -23,6 +23,7 @@ using StardewModdingAPI.Web.Framework.Clients.Pastebin;
using StardewModdingAPI.Web.Framework.Compression;
using StardewModdingAPI.Web.Framework.ConfigModels;
using StardewModdingAPI.Web.Framework.RewriteRules;
using StardewModdingAPI.Web.Framework.UserAgentParsing;
namespace StardewModdingAPI.Web
{
@ -172,6 +173,7 @@ namespace StardewModdingAPI.Web
)
.UseRewriter(this.GetRedirectRules())
.UseStaticFiles() // wwwroot folder
.UseClientPlatform()
.UseMvc();
// enable Hangfire dashboard

View File

@ -1,5 +1,6 @@
@using Newtonsoft.Json
@using StardewModdingAPI.Web.Framework.LogParsing.Models
@using StardewModdingAPI.Web.Framework.UserAgentParsing
@model StardewModdingAPI.Web.ViewModels.LogParserModel
@{
@ -67,10 +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">
<li><input type="radio" name="os" value="android" id="os-android" /> <label for="os-android">Android</label></li>
<li><input type="radio" name="os" value="linux" id="os-linux" /> <label for="os-linux">Linux</label></li>
<li><input type="radio" name="os" value="mac" id="os-mac" /> <label for="os-mac">Mac</label></li>
<li><input type="radio" name="os" value="windows" id="os-windows" /> <label for="os-windows">Windows</label></li>
@{
Platform? clientPlatform = Context.Items[ClientPlatformMiddleware.ClientPlatformKey] as Platform?;
}
<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: