feat: integration cloudfile
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Text;
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
@@ -8,7 +9,8 @@ namespace mws.backend.dotnet.application.Documents;
|
||||
|
||||
public record CreateDocumentCommand(Guid UserId, Guid ProjectId, CreateDocumentRequest Request) : IRequest<DocumentDto>;
|
||||
|
||||
public class CreateDocumentHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<CreateDocumentCommand, DocumentDto>
|
||||
public class CreateDocumentHandler(IUnitOfWork uow, IMapper mapper, IDocumentContentStore contentStore)
|
||||
: IRequestHandler<CreateDocumentCommand, DocumentDto>
|
||||
{
|
||||
public async Task<DocumentDto> Handle(CreateDocumentCommand command, CancellationToken ct)
|
||||
{
|
||||
@@ -43,7 +45,6 @@ public class CreateDocumentHandler(IUnitOfWork uow, IMapper mapper) : IRequestHa
|
||||
ParentId = request.ParentId,
|
||||
Title = request.Title.Trim(),
|
||||
Type = request.Type,
|
||||
Content = request.Type == DocumentType.Document ? request.Content : null,
|
||||
CreatedBy = command.UserId,
|
||||
CreatedAt = now,
|
||||
UpdatedBy = command.UserId,
|
||||
@@ -52,6 +53,21 @@ public class CreateDocumentHandler(IUnitOfWork uow, IMapper mapper) : IRequestHa
|
||||
|
||||
uow.Documents.Add(doc);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
|
||||
if (request.Type == DocumentType.Document && !string.IsNullOrEmpty(request.Content))
|
||||
{
|
||||
await contentStore.EnsureBucketAsync(doc.ProjectId, ct);
|
||||
var bytes = Encoding.UTF8.GetBytes(request.Content);
|
||||
using (var ms = new MemoryStream(bytes))
|
||||
{
|
||||
await contentStore.UploadAsync(doc.ProjectId, doc.Id, "text/html; charset=utf-8", ms, ct);
|
||||
}
|
||||
|
||||
doc.ContentSize = bytes.Length;
|
||||
doc.UpdatedContentAt = now;
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
return mapper.Map<DocumentDto>(doc);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user