using Femto.Api.Controllers.Media.Dto; using Femto.Modules.Media.Application; using Femto.Modules.Media.Contracts; using Femto.Modules.Media.Contracts.LoadFile; using Femto.Modules.Media.Contracts.SaveFile; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Femto.Api.Controllers.Media; [ApiController] [Route("media")] public class MediaController(IMediaModule mediaModule) : ControllerBase { [HttpPost] [Authorize] public async Task> UploadMedia( IFormFile file, CancellationToken cancellationToken ) { await using var data = file.OpenReadStream(); var id = await mediaModule.PostCommand( 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}")] [Authorize] public async Task GetMedia(Guid id, CancellationToken cancellationToken) { var res = await mediaModule.PostQuery(new LoadFileQuery(id), cancellationToken); HttpContext.Response.ContentType = res.Type; HttpContext.Response.ContentLength = res.Size; await res.Data.CopyToAsync(HttpContext.Response.Body, cancellationToken); } }