39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
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);
|
|
}
|
|
}
|