Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit 4bd25c2

Browse files
Merge pull request #4 from OpenTabletDriver/framework-download-shortcut
Add service for getting the latest .NET version for OpenTabletDriver
2 parents ecb2370 + 4d0fd57 commit 4bd25c2

7 files changed

Lines changed: 171 additions & 2 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Data.SqlTypes;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
using OpenTabletDriver.Web.Core.Services;
8+
9+
namespace OpenTabletDriver.Web.Core.Framework
10+
{
11+
public class DotnetCoreService : IFrameworkService
12+
{
13+
private const string AKA_MS = @"https://aka.ms/dotnet";
14+
private const string CHANNEL = "current";
15+
16+
public string GetLatestVersionUrl(FrameworkPlatform platform, FrameworkArchetecture archetecture)
17+
{
18+
string product = GetProduct(platform);
19+
string extension = GetExtension(platform);
20+
string os = GetNormalizedOS(platform);
21+
string arch = GetNormalizedArchitecture(archetecture);
22+
23+
switch (platform)
24+
{
25+
case FrameworkPlatform.Linux:
26+
// Link to package manager instructions instead of binaries or an installer.
27+
return "https://docs.microsoft.com/en-us/dotnet/core/install/linux";
28+
case FrameworkPlatform.Windows:
29+
case FrameworkPlatform.MacOS:
30+
return $"{AKA_MS}/{CHANNEL}/{product}-{os}-{arch}.{extension}";
31+
default:
32+
throw new Exception("Unsupported platform.");
33+
}
34+
}
35+
36+
private string GetProduct(FrameworkPlatform platform)
37+
{
38+
switch (platform)
39+
{
40+
case FrameworkPlatform.Linux:
41+
case FrameworkPlatform.MacOS:
42+
return "dotnet-runtime";
43+
case FrameworkPlatform.Windows:
44+
return "windowsdesktop-runtime";
45+
default:
46+
return null;
47+
}
48+
}
49+
50+
private string GetExtension(FrameworkPlatform platform)
51+
{
52+
switch (platform)
53+
{
54+
case FrameworkPlatform.Linux:
55+
return "tar.gz";
56+
case FrameworkPlatform.MacOS:
57+
return "pkg";
58+
case FrameworkPlatform.Windows:
59+
return "exe";
60+
default:
61+
return null;
62+
}
63+
}
64+
65+
private static string GetNormalizedOS(FrameworkPlatform platform)
66+
{
67+
return platform switch
68+
{
69+
FrameworkPlatform.Windows => "win",
70+
FrameworkPlatform.Linux => "linux",
71+
FrameworkPlatform.MacOS => "osx",
72+
_ => null
73+
};
74+
}
75+
76+
private string GetNormalizedArchitecture(FrameworkArchetecture archetecture)
77+
{
78+
return archetecture switch
79+
{
80+
FrameworkArchetecture.x64 => "x64",
81+
FrameworkArchetecture.x86 => "x86",
82+
FrameworkArchetecture.ARM64 => "arm64",
83+
_ => null
84+
};
85+
}
86+
}
87+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace OpenTabletDriver.Web.Core
2+
{
3+
public enum FrameworkArchetecture
4+
{
5+
Unknown = 0,
6+
x86 = 1 << 0,
7+
x64 = 1 << 1,
8+
ARM64 = 1 << 2
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace OpenTabletDriver.Web.Core
2+
{
3+
public enum FrameworkPlatform
4+
{
5+
Unknown = 0,
6+
Windows = 1 << 0,
7+
Linux = 1 << 1,
8+
MacOS = 1 << 2
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace OpenTabletDriver.Web.Core.Services
4+
{
5+
public interface IFrameworkService
6+
{
7+
string GetLatestVersionUrl(FrameworkPlatform platform, FrameworkArchetecture archetecture);
8+
}
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Linq;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
4+
using Octokit;
5+
using OpenTabletDriver.Web.Core;
6+
using OpenTabletDriver.Web.Core.Services;
7+
8+
namespace OpenTabletDriver.Web.Controllers
9+
{
10+
public class FrameworkController : Controller
11+
{
12+
public FrameworkController(IFrameworkService frameworkService)
13+
{
14+
this.frameworkService = frameworkService;
15+
}
16+
17+
private IFrameworkService frameworkService;
18+
19+
public IActionResult Index()
20+
{
21+
string userAgentHeader = Request.Headers["User-Agent"];
22+
string userAgent = userAgentHeader.ToLower();
23+
FrameworkPlatform platform = FrameworkPlatform.Unknown;
24+
FrameworkArchetecture archetecture = FrameworkArchetecture.Unknown;
25+
26+
if (userAgent.Contains("windows"))
27+
platform = FrameworkPlatform.Windows;
28+
else if (userAgent.Contains("linux"))
29+
platform = FrameworkPlatform.Linux;
30+
else if (userAgent.Contains("mac"))
31+
platform = FrameworkPlatform.MacOS;
32+
33+
if (platform == FrameworkPlatform.MacOS || userAgent.Contains("x86_64") || userAgent.Contains("x64"))
34+
archetecture = FrameworkArchetecture.x64;
35+
else if (userAgent.Contains("x86"))
36+
archetecture = FrameworkArchetecture.x86;
37+
else if (userAgent.Contains("arm64"))
38+
archetecture = FrameworkArchetecture.ARM64;
39+
40+
string url = frameworkService.GetLatestVersionUrl(platform, archetecture);
41+
return Redirect(url);
42+
}
43+
}
44+
}

OpenTabletDriver.Web/OpenTabletDriver.Web.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
<PackageReference Include="Markdig" Version="0.25.0" />
1414
</ItemGroup>
1515

16+
<ItemGroup>
17+
<_ContentIncludedByDefault Remove="Views\Wiki\Index.cshtml" />
18+
</ItemGroup>
19+
1620
</Project>

OpenTabletDriver.Web/Startup.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
8+
using OpenTabletDriver.Web.Controllers;
9+
using OpenTabletDriver.Web.Core.Framework;
810
using OpenTabletDriver.Web.Core.GitHub.Services;
911
using OpenTabletDriver.Web.Core.Services;
1012

@@ -24,6 +26,7 @@ public void ConfigureServices(IServiceCollection services)
2426
{
2527
services.AddControllersWithViews();
2628
services.Add(new ServiceDescriptor(typeof(IReleaseService), new GitHubReleaseService()));
29+
services.Add(new ServiceDescriptor(typeof(IFrameworkService), new DotnetCoreService()));
2730
services.Configure<ForwardedHeadersOptions>(options =>
2831
{
2932
options.KnownProxies.Add(IPAddress.Parse("10.0.0.100"));
@@ -43,6 +46,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4346
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
4447
app.UseHsts();
4548
}
49+
4650
app.UseHttpsRedirection();
4751
app.UseStaticFiles();
4852

@@ -60,8 +64,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6064
{
6165
endpoints.MapControllerRoute(
6266
name: "default",
63-
pattern: "{controller=Home}/{action=Index}/{id?}");
67+
pattern: "{controller=Home}/{action=Index}/{id?}"
68+
);
6469
});
6570
}
6671
}
67-
}
72+
}

0 commit comments

Comments
 (0)