using Hutopy.Application.AzureBlobStorage.Constants; using Hutopy.Infrastructure.AzureBlob; using Hutopy.Web.Features.Contents.Data; namespace Hutopy.Web.Features.Contents.Handlers; [PublicAPI] public record ChangeBannerRequest( Guid CreatorId, IFormFile File); [PublicAPI] public record ChangeBannerResponse( string BlobUrl); [PublicAPI] public class ChangeBannerHandler( ContentDbContext context, AzureBlobStorage blobStorage) : Endpoint { public override void Configure() { Post("/api/creators/{CreatorId}/banner"); Options(o => o.WithTags("Creators")); AllowFileUploads(); } public override async Task HandleAsync( ChangeBannerRequest request, CancellationToken ct) { var creator = await context .Creators .Include(c => c.Images) .SingleOrDefaultAsync( c => c.Id == request.CreatorId, cancellationToken: ct); if (creator is null) { await SendNotFoundAsync(ct); return; } var blobUrl = await blobStorage.UploadFileAsync( ContainerNames.Creators, $"{request.CreatorId}/{SubDirectoryNames.Profile}/{CommonFileNames.BannerPicture}", request.File.OpenReadStream(), request.File.ContentType, ct); creator.Images.Banner = blobUrl; await context.SaveChangesAsync(ct); await SendOkAsync( new ChangeBannerResponse(blobUrl), ct); } }