using FozFc.Api.Api.Authorization; using FozFc.Api.Api.Extensions; using FozFc.Api.Application.Common; using FozFc.Api.Infrastructure.Storage; using MongoDB.Bson; namespace FozFc.Api.Api.Endpoints; public static class UploadEndpoints { public static IEndpointRouteBuilder MapUploadEndpoints(this IEndpointRouteBuilder app) { app.MapPost("/api/upload", async (HttpContext ctx, IFileStorage storage, CancellationToken ct) => { EndpointGuards.RequireUid(ctx.User); if (!ctx.Request.HasFormContentType) throw ApiException.BadRequest("Expected multipart/form-data."); var form = await ctx.Request.ReadFormAsync(ct); var file = form.Files.GetFile("file") ?? form.Files.FirstOrDefault(); if (file is null || file.Length == 0) throw ApiException.BadRequest("No file provided."); await using var stream = file.OpenReadStream(); var stored = await storage.SaveAsync(stream, file.FileName, ct); return BsonResults.Json(new BsonDocument { { "url", stored.Url }, { "path", stored.Path }, { "name", stored.Name }, }, 201); }); return app; } }