37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
using System.Text;
|
|
using AutoMapper;
|
|
using MediatR;
|
|
using mws.backend.dotnet.application.Common;
|
|
using mws.backend.dotnet.domain.Documents;
|
|
|
|
namespace mws.backend.dotnet.application.Documents;
|
|
|
|
public record GetDocumentQuery(Guid UserId, Guid DocumentId) : IRequest<DocumentDto>;
|
|
|
|
public class GetDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentContentStore contentStore)
|
|
: IRequestHandler<GetDocumentQuery, DocumentDto>
|
|
{
|
|
public async Task<DocumentDto> Handle(GetDocumentQuery query, CancellationToken ct)
|
|
{
|
|
var doc = await DocumentAccess.GetDocumentForUserAsync(uow, query.UserId, query.DocumentId, ct);
|
|
var dto = mapper.Map<DocumentDto>(doc);
|
|
|
|
if (doc.Type == DocumentType.Document && !string.IsNullOrEmpty(doc.StorageKey))
|
|
{
|
|
await using var stream = await contentStore.DownloadAsync(doc.StorageKey, ct);
|
|
if (stream != Stream.Null)
|
|
{
|
|
using var reader = new StreamReader(stream, Encoding.UTF8);
|
|
var content = await reader.ReadToEndAsync(ct);
|
|
dto.Content = string.IsNullOrEmpty(content) ? null : content;
|
|
}
|
|
else
|
|
{
|
|
dto.Content = null;
|
|
}
|
|
}
|
|
|
|
return dto;
|
|
}
|
|
}
|