media upload
This commit is contained in:
parent
befaa207d7
commit
0d7da2ea85
33 changed files with 257 additions and 353 deletions
|
@ -1,3 +1,3 @@
|
|||
namespace Femto.Api.Controllers.Authors;
|
||||
namespace Femto.Api.Controllers.Authors.Dto;
|
||||
|
||||
public record GetAuthorPostsSearchParams(Guid? From, int? Amount);
|
3
Femto.Api/Controllers/Media/Dto/UploadMediaResponse.cs
Normal file
3
Femto.Api/Controllers/Media/Dto/UploadMediaResponse.cs
Normal file
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Api.Controllers.Media.Dto;
|
||||
|
||||
public record UploadMediaResponse(Guid MediaId, string Url);
|
39
Femto.Api/Controllers/Media/MediaController.cs
Normal file
39
Femto.Api/Controllers/Media/MediaController.cs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue