media upload

This commit is contained in:
john 2025-05-04 21:46:24 +02:00
parent befaa207d7
commit 0d7da2ea85
33 changed files with 257 additions and 353 deletions

View file

@ -1,3 +1,3 @@
namespace Femto.Api.Controllers.Authors;
namespace Femto.Api.Controllers.Authors.Dto;
public record GetAuthorPostsSearchParams(Guid? From, int? Amount);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Media.Dto;
public record UploadMediaResponse(Guid MediaId, string Url);

View file

@ -0,0 +1,39 @@
using Femto.Api.Controllers.Media.Dto;
using Femto.Modules.Media.Contracts;
using Femto.Modules.Media.Contracts.LoadFile;
using Femto.Modules.Media.Contracts.SaveFile;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Femto.Api.Controllers.Media;
[ApiController]
[Route("media")]
public class MediaController(IMediator mediator) : ControllerBase
{
[HttpPost]
public async Task<ActionResult<UploadMediaResponse>> UploadMedia(
IFormFile file,
CancellationToken cancellationToken
)
{
await using var data = file.OpenReadStream();
var id = await mediator.Send(
new SaveFileCommand(data, file.ContentType, file.Length),
cancellationToken
);
var fileGetUrl =
$"{this.HttpContext.Request.Scheme}://{this.HttpContext.Request.Host}/media/{id}";
return new UploadMediaResponse(id, fileGetUrl);
}
[HttpGet("{id}")]
public async Task GetMedia(Guid id, CancellationToken cancellationToken)
{
var res = await mediator.Send(new LoadFileQuery(id), cancellationToken);
HttpContext.Response.ContentType = res.Type;
HttpContext.Response.ContentLength = res.Size;
await res.Data.CopyToAsync(HttpContext.Response.Body, cancellationToken);
}
}