Updated the proostekrant to work properly again.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Configuration.AddEnvironmentVariables();
|
||||
|
||||
builder.Services.AddRazorComponents();
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
|
||||
builder.Services.AddAuthentication("Krant").AddCookie("Krant", options =>
|
||||
{
|
||||
options.Cookie.Name = "ProostenKrant";
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.SameSite = SameSiteMode.Lax;
|
||||
options.Cookie.IsEssential = true;
|
||||
options.Cookie.Path = "/";
|
||||
options.LoginPath = "/login";
|
||||
options.AccessDeniedPath = "/";
|
||||
options.ExpireTimeSpan = TimeSpan.FromDays(30);
|
||||
options.SlidingExpiration = true;
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy("Editor", policy => policy.RequireClaim("uid"));
|
||||
});
|
||||
|
||||
builder.Services.AddAntiforgery();
|
||||
|
||||
builder.Services.AddSingleton<ProostenKrant.Services.Db>();
|
||||
builder.Services.AddScoped<ProostenKrant.Services.UserService>();
|
||||
builder.Services.AddScoped<ProostenKrant.Services.SeasonService>();
|
||||
builder.Services.AddScoped<ProostenKrant.Services.ArticleService>();
|
||||
builder.Services.AddScoped<ProostenKrant.Services.LatexService>();
|
||||
builder.Services.AddScoped<ProostenKrant.Services.UploadService>();
|
||||
builder.Services.AddScoped<ProostenKrant.Services.KrantInfo>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/fout", createScopeForErrors: true);
|
||||
}
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseAntiforgery();
|
||||
|
||||
app.MapRazorComponents<ProostenKrant.Components.App>();
|
||||
|
||||
// Krant drukken (LaTeX-compilatie). [FromForm] activeert automatische
|
||||
// antiforgery-validatie; RequireAuthorization vereist een redactie-cookie.
|
||||
app.MapPost("/api/genereer-pdf",
|
||||
async ([FromForm] int? SeizoenId, ProostenKrant.Services.LatexService latex) =>
|
||||
{
|
||||
var result = await latex.GeneratePdfAsync(SeizoenId);
|
||||
var target = result.Success
|
||||
? $"/seizoenen?melding={Uri.EscapeDataString("De editie is succesvol gedrukt.")}&pdf={Uri.EscapeDataString(result.FileName!)}"
|
||||
: $"/seizoenen?fout={Uri.EscapeDataString(result.Error ?? "onbekende fout")}";
|
||||
return Results.Redirect(target);
|
||||
}).RequireAuthorization("Editor");
|
||||
|
||||
app.MapGet("/edities/{bestand}", (string bestand) =>
|
||||
{
|
||||
var safe = Path.GetFileName(bestand);
|
||||
if (!safe.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
var dirs = new[]
|
||||
{
|
||||
Path.Combine(app.Environment.ContentRootPath, app.Configuration["Paths:LatexOutput"] ?? "storage/latex"),
|
||||
Path.Combine(app.Environment.ContentRootPath, app.Configuration["Paths:StaticArchive"] ?? "archive"),
|
||||
};
|
||||
var path = dirs.Select(d => Path.Combine(d, safe)).FirstOrDefault(File.Exists);
|
||||
return path is null ? Results.NotFound() : Results.File(path, "application/pdf", safe);
|
||||
});
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user