sillifactoring
This commit is contained in:
parent
a31458f415
commit
1205bbe365
8 changed files with 126 additions and 44 deletions
39
DOIOU.Server/Controllers/InfoController.cs
Normal file
39
DOIOU.Server/Controllers/InfoController.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DOIOU.Server.Controllers
|
||||
{
|
||||
[Route("/info")]
|
||||
public class InfoController : Controller
|
||||
{
|
||||
private readonly ILogger<InfoController> _logger;
|
||||
|
||||
public InfoController(ILogger<InfoController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet("faq")]
|
||||
public IActionResult FAQ()
|
||||
{
|
||||
return View("FAQ");
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View("Error!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System.Diagnostics;
|
||||
using System.Text.RegularExpressions;
|
||||
using DOIOU.Server.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
@ -21,28 +22,34 @@ public class MainController : Controller
|
|||
}
|
||||
|
||||
[HttpGet("{*doiou}")]
|
||||
public IActionResult Get(string doiou) => Resolve(doiou);
|
||||
public async Task<IActionResult> Get(string doiou) => await Resolve(doiou);
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Post([FromForm] string doiou) => Resolve(doiou);
|
||||
public async Task<IActionResult> Post([FromForm] string doiou) => await Resolve(doiou);
|
||||
|
||||
private const string DOI_REGEX = @"^((?<scheme>[a-z]+):(\/\/)?)?(?<domain>[a-z0-9.\-]+\/)?((?<prefix>\d{1,3})\.(?<publisher>\d{4,5})\/(?<id>[\S]+[^;,.\s]))$";
|
||||
|
||||
[NonAction]
|
||||
private IActionResult Resolve(string doiou)
|
||||
private async Task<IActionResult> Resolve(string doiou)
|
||||
{
|
||||
int i = doiou.IndexOf('.'), prefix = 69;
|
||||
var match = Regex.Match(doiou, DOI_REGEX);
|
||||
|
||||
if (i != -1 && !int.TryParse(doiou[..i], out prefix))
|
||||
if (!match.Success)
|
||||
{
|
||||
return BadRequest("Invalid format");
|
||||
}
|
||||
|
||||
// TODO further validation
|
||||
int prefix = int.Parse(match.Groups["prefix"].Value);
|
||||
int publisher = int.Parse(match.Groups["publisher"].Value);
|
||||
string id = match.Groups["id"].Value;
|
||||
|
||||
// Maybe add other providers if they exist?
|
||||
string query = $"{prefix}.{publisher}/{id}";
|
||||
|
||||
if (prefix == 69)
|
||||
// TODO further validation (use schemes?)
|
||||
|
||||
if (prefix == 7 || prefix == 69)
|
||||
{
|
||||
string? q = _data.Query(doiou);
|
||||
string? q = _data.Query(query);
|
||||
return q != null ? Redirect(q) : NotFound("DOIOU not found");
|
||||
}
|
||||
|
||||
|
@ -52,7 +59,22 @@ public class MainController : Controller
|
|||
_ => null
|
||||
};
|
||||
|
||||
return urlBase != null ? Redirect($"{urlBase}/{doiou}") : NotFound($"Unsupported prefix: {prefix}");
|
||||
if (urlBase == null)
|
||||
return BadRequest($"Unsupported prefix: {prefix}");
|
||||
|
||||
using HttpClient client = new();
|
||||
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "DOIOU v1.0");
|
||||
|
||||
var response = await client.GetAsync($"{urlBase}/{doiou}");
|
||||
if (((int)response.StatusCode / 100) == 3)
|
||||
{
|
||||
string location = response.Headers!.Location!.ToString();
|
||||
return Redirect(location);
|
||||
}
|
||||
|
||||
//TODO: cache
|
||||
|
||||
return NotFound("DOIOU not found");
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
8
DOIOU.Server/Views/Info/Faq.cshtml
Normal file
8
DOIOU.Server/Views/Info/Faq.cshtml
Normal file
|
@ -0,0 +1,8 @@
|
|||
@{
|
||||
ViewData["Title"] = "FAQ";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-4">FAQ</h1>
|
||||
<p>something something estradiol</p>
|
||||
</div>
|
8
DOIOU.Server/Views/Info/Index.cshtml
Normal file
8
DOIOU.Server/Views/Info/Index.cshtml
Normal file
|
@ -0,0 +1,8 @@
|
|||
@{
|
||||
ViewData["Title"] = "Info";
|
||||
}
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="display-4">Info</h1>
|
||||
<p>idk</p>
|
||||
</div>
|
|
@ -12,7 +12,6 @@
|
|||
<header>
|
||||
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" asp-area="" asp-controller="Main" asp-action="Index">DOIOU</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
|
||||
aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
|
@ -22,6 +21,12 @@
|
|||
<li class="nav-item">
|
||||
<a class="nav-link text-dark" asp-area="" asp-controller="Main" asp-action="Index">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link text-dark" asp-area="" asp-controller="Info" asp-action="FAQ">FAQ</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link text-dark" asp-area="" asp-controller="User" asp-action="Dashboard">Members</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,28 +1,28 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DOIOU.Server", "DOIOU.Server\DOIOU.Server.csproj", "{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DOIOU.Tests", "DOIOU.Tests\DOIOU.Tests.csproj", "{D48F56A0-2791-4631-88E5-EE32381B7374}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DOIOU.Server", "DOIOU.Server\DOIOU.Server.csproj", "{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DOIOU.Tests", "DOIOU.Tests\DOIOU.Tests.csproj", "{D48F56A0-2791-4631-88E5-EE32381B7374}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7DE4EF6C-BFB1-4157-93EA-1B34F5CBBE8A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D48F56A0-2791-4631-88E5-EE32381B7374}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -1,4 +1,4 @@
|
|||
# DOIOU
|
||||
|
||||
*DOI but free*
|
||||
*DOI but free...* \
|
||||
A simple extension of the DOI system for arbitrary documents.
|
||||
|
|
Loading…
Reference in a new issue