media upload
This commit is contained in:
parent
befaa207d7
commit
0d7da2ea85
33 changed files with 257 additions and 353 deletions
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Modules.Media.Contracts.LoadFile.Dto;
|
||||
|
||||
public record LoadFileQueryResult(Stream Data, string Type, long? Size);
|
6
Femto.Modules.Media/Contracts/LoadFile/LoadFileQuery.cs
Normal file
6
Femto.Modules.Media/Contracts/LoadFile/LoadFileQuery.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
using Femto.Modules.Media.Contracts.LoadFile.Dto;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Media.Contracts.LoadFile;
|
||||
|
||||
public record LoadFileQuery(Guid FileId) : IRequest<LoadFileQueryResult>;
|
|
@ -0,0 +1,25 @@
|
|||
using Femto.Modules.Media.Contracts.LoadFile.Dto;
|
||||
using Femto.Modules.Media.Data;
|
||||
using Femto.Modules.Media.Infrastructure;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Media.Contracts.LoadFile;
|
||||
|
||||
internal class LoadFileQueryHandler(IStorageProvider storage, MediaContext context)
|
||||
: IRequestHandler<LoadFileQuery, LoadFileQueryResult>
|
||||
{
|
||||
public async Task<LoadFileQueryResult> Handle(
|
||||
LoadFileQuery query,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var blob = await context
|
||||
.SavedBlobs.Where(b => b.Id == query.FileId)
|
||||
.SingleAsync(cancellationToken: cancellationToken);
|
||||
|
||||
var data = await storage.LoadBlob(query.FileId.ToString());
|
||||
|
||||
return new(data, blob.Type, blob.Size);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Media.Contracts.SaveFile;
|
||||
|
||||
public record SaveFileCommand(Stream Data, string ContentType, long? ContentLength) : IRequest<Guid>;
|
|
@ -0,0 +1,18 @@
|
|||
using Femto.Modules.Media.Data;
|
||||
using Femto.Modules.Media.Infrastructure;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Media.Contracts.SaveFile;
|
||||
|
||||
internal class SaveFileCommandHandler(IStorageProvider storage, MediaContext context)
|
||||
: IRequestHandler<SaveFileCommand, Guid>
|
||||
{
|
||||
public async Task<Guid> Handle(SaveFileCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var id = Guid.CreateVersion7();
|
||||
await storage.SaveBlob(id.ToString(), command.Data);
|
||||
await context.AddAsync(new SavedBlob(id, command.ContentType, command.ContentLength), cancellationToken);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return id;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue