|
| 1 | +using CCE.Api.Common.Auth; |
| 2 | +using CCE.Api.Common.Extensions; |
| 3 | +using CCE.Application.Common.Interfaces; |
| 4 | +using CCE.Application.Content.Commands.UploadAsset; |
| 5 | +using CCE.Application.Content.Queries.GetAssetById; |
| 6 | +using CCE.Infrastructure; |
| 7 | +using MediatR; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Routing; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | + |
| 13 | +namespace CCE.Api.External.Endpoints; |
| 14 | + |
| 15 | +public static class AssetEndpoints |
| 16 | +{ |
| 17 | + public static IEndpointRouteBuilder MapAssetEndpoints(this IEndpointRouteBuilder app) |
| 18 | + { |
| 19 | + var assets = app.MapGroup("/api/assets").WithTags("Assets").RequireAuthorization(); |
| 20 | + |
| 21 | + assets.MapPost("", async ( |
| 22 | + IFormFile file, |
| 23 | + ICurrentUserAccessor currentUser, |
| 24 | + IMediator mediator, |
| 25 | + IOptions<CceInfrastructureOptions> infraOpts, |
| 26 | + CancellationToken ct) => |
| 27 | + { |
| 28 | + if (currentUser.GetUserId() is null) return Results.Unauthorized(); |
| 29 | + |
| 30 | + if (file is null || file.Length == 0) |
| 31 | + return Results.BadRequest(new { error = "Upload requires a non-empty file." }); |
| 32 | + |
| 33 | + var allowed = infraOpts.Value.AllowedAssetMimeTypes; |
| 34 | + if (!allowed.Contains(file.ContentType, System.StringComparer.OrdinalIgnoreCase)) |
| 35 | + return Results.StatusCode(StatusCodes.Status415UnsupportedMediaType); |
| 36 | + |
| 37 | + await using var stream = file.OpenReadStream(); |
| 38 | + var result = await mediator.Send( |
| 39 | + new UploadAssetCommand(stream, file.FileName, file.ContentType, file.Length), |
| 40 | + ct).ConfigureAwait(false); |
| 41 | + |
| 42 | + return result.Success |
| 43 | + ? Results.Created($"/api/assets/{result.Data!.Id}", result) |
| 44 | + : result.ToHttpResult(); |
| 45 | + }) |
| 46 | + .WithName("UploadAsset") |
| 47 | + .DisableAntiforgery() |
| 48 | + .WithMetadata(new RequestSizeLimitMetadataImpl(20L * 1024L * 1024L)); |
| 49 | + |
| 50 | + assets.MapGet("{id:guid}", async ( |
| 51 | + System.Guid id, |
| 52 | + ICurrentUserAccessor currentUser, |
| 53 | + IMediator mediator, |
| 54 | + CancellationToken ct) => |
| 55 | + { |
| 56 | + var userId = currentUser.GetUserId() ?? System.Guid.Empty; |
| 57 | + if (userId == System.Guid.Empty) return Results.Unauthorized(); |
| 58 | + |
| 59 | + var result = await mediator.Send(new GetAssetByIdQuery(id), ct).ConfigureAwait(false); |
| 60 | + |
| 61 | + if (!result.Success || result.Data!.UploadedById != userId) |
| 62 | + return Results.NotFound(); |
| 63 | + |
| 64 | + return result.ToHttpResult(); |
| 65 | + }) |
| 66 | + .WithName("GetAssetById"); |
| 67 | + |
| 68 | + return app; |
| 69 | + } |
| 70 | + |
| 71 | + private sealed class RequestSizeLimitMetadataImpl : Microsoft.AspNetCore.Http.Metadata.IRequestSizeLimitMetadata |
| 72 | + { |
| 73 | + public RequestSizeLimitMetadataImpl(long? max) { MaxRequestBodySize = max; } |
| 74 | + public long? MaxRequestBodySize { get; } |
| 75 | + } |
| 76 | +} |
0 commit comments