Some checks failed
Build & Deploy PLDpro.Web Test to 192.168.1.100 / build-and-deploy (push) Failing after 47s
This reverts commit 35f4133001.
112 lines
3.4 KiB
C#
112 lines
3.4 KiB
C#
using Amazon.Runtime;
|
|
using Amazon.S3;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.Extensions.Options;
|
|
using MudBlazor.Services;
|
|
using Pldpro.Web.Components;
|
|
using Pldpro.Web.Components.Pages;
|
|
using Pldpro.Web.Models;
|
|
using Pldpro.Web.Services;
|
|
using System.Net.NetworkInformation;
|
|
using System.Runtime.Intrinsics.Arm;
|
|
using static MudBlazor.CategoryTypes;
|
|
using static MudBlazor.Colors;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add MudBlazor services
|
|
builder.Services.AddMudServices();
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
|
|
|
|
// --- S3 / RustFS Settings binding ---
|
|
builder.Services.Configure<S3Settings>(builder.Configuration.GetSection("S3"));
|
|
|
|
// Optional: größere Uploads erlauben (z. B. 512MB)
|
|
builder.Services.Configure<FormOptions>(o => { o.MultipartBodyLengthLimit = 512L * 1024 * 1024; });
|
|
|
|
// IAmazonS3 via DI (lokaler S3-kompatibler Endpoint)
|
|
builder.Services.AddSingleton<IAmazonS3>(sp =>
|
|
{
|
|
var s = sp.GetRequiredService<IOptions<S3Settings>>().Value;
|
|
var cfg = new Amazon.S3.AmazonS3Config
|
|
{
|
|
ServiceURL = s.ServiceURL,
|
|
ForcePathStyle = s.ForcePathStyle,
|
|
UseHttp = s.UseHttp
|
|
// AuthenticationRegion ist bei Custom-S3 i. d. R. egal
|
|
}
|
|
;
|
|
var creds = new BasicAWSCredentials(s.AccessKey, s.SecretKey);
|
|
return new AmazonS3Client(creds, cfg);
|
|
});
|
|
|
|
// Domain-Service
|
|
builder.Services.AddScoped<IStorageService, S3StorageService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
app.UseAntiforgery();
|
|
|
|
app.MapStaticAssets();
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
|
|
// --- Minimal APIs für Storage ---
|
|
var storage = app.MapGroup("/api/storage");
|
|
|
|
// Buckets auflisten
|
|
storage.MapGet("/buckets", async (IStorageService svc) =>
|
|
{
|
|
var result = await svc.ListBucketsAsync();
|
|
return Results.Ok(result);
|
|
});
|
|
|
|
// Bucket erstellen
|
|
storage.MapPost("/buckets", async (IStorageService svc, S3CreateBucketDto dto) =>
|
|
{
|
|
if (string.IsNullOrWhiteSpace(dto.BucketName)) return Results.BadRequest("BucketName required");
|
|
await svc.CreateBucketAsync(dto.BucketName);
|
|
return Results.Ok();
|
|
}).DisableAntiforgery(); // für XHR-POST ohne Token
|
|
|
|
// Objekte eines Buckets auflisten
|
|
storage.MapGet("/buckets/{bucket}/objects", async (IStorageService svc, string bucket) =>
|
|
{
|
|
var objects = await svc.ListObjectsAsync(bucket);
|
|
return Results.Ok(objects);
|
|
});
|
|
|
|
// Datei in Bucket hochladen (Form-Data: file)
|
|
storage.MapPost("/buckets/{bucket}/upload", async (HttpRequest req, IStorageService svc, string bucket) =>
|
|
{
|
|
if (!req.HasFormContentType) return Results.BadRequest("Multipart/form-data expected");
|
|
var form = await req.ReadFormAsync();
|
|
var file = form.Files["file"];
|
|
if (file is null) return Results.BadRequest("'file' missing");
|
|
|
|
await using var stream = file.OpenReadStream(); // Streamlimit über FormOptions konfiguriert
|
|
await svc.UploadObjectAsync(bucket, file.FileName, stream, file.ContentType ?? "application/octet-stream");
|
|
return Results.Ok();
|
|
}).DisableAntiforgery(); // für Blazor XHR Upload
|
|
|
|
|
|
|
|
app.Run();
|