lab4: S3-вложения, presigned upload и проверки

This commit is contained in:
Vladislav Kunetskiy
2026-10-21 22:42:48 +03:00
parent d342b2a227
commit 49c2fe3071
79 changed files with 12266 additions and 910 deletions
+46 -20
View File
@@ -7,12 +7,15 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"time"
"github.com/google/uuid"
"mtusi-messenger/internal/domain"
attachmentService "mtusi-messenger/internal/service/attachment"
"mtusi-messenger/internal/transport/rest/internal/oas"
)
@@ -51,14 +54,23 @@ type SessionService interface {
Revoke(ctx context.Context, userID uuid.UUID, token string) error
}
type AttachmentService interface {
Upload(ctx context.Context, input attachmentService.UploadInput) (domain.Attachment, error)
Get(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, error)
Open(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error)
Presign(ctx context.Context, request domain.PresignRequest) (domain.PresignResult, error)
Complete(ctx context.Context, ownerID, id uuid.UUID, sha256 string) (domain.Attachment, error)
}
type Handler struct {
oas.UnimplementedHandler
users UserService
chats ChatService
messages MessageService
health HealthChecker
sessions SessionService
users UserService
chats ChatService
messages MessageService
attachments AttachmentService
health HealthChecker
sessions SessionService
}
var noCursor *domain.Cursor
@@ -67,15 +79,17 @@ func NewHandler(
users UserService,
chats ChatService,
messages MessageService,
attachments AttachmentService,
health HealthChecker,
sessions SessionService,
) *Handler {
return &Handler{
users: users,
chats: chats,
messages: messages,
health: health,
sessions: sessions,
users: users,
chats: chats,
messages: messages,
attachments: attachments,
health: health,
sessions: sessions,
}
}
@@ -238,9 +252,10 @@ func (h *Handler) CreateMessage(
}
message, err := h.messages.Send(ctx, domain.MessageDraft{
ChatID: params.ChatID,
SenderID: callerID,
Text: req.Text,
ChatID: params.ChatID,
SenderID: callerID,
Text: req.Text,
AttachmentIDs: req.AttachmentIds,
Idempotency: domain.MessageIdempotency{
Key: params.IdempotencyKey,
RequestHash: fingerprint,
@@ -260,9 +275,16 @@ func (h *Handler) CreateMessage(
// order of the client JSON cannot change the fingerprint, while a different
// payload with the same Idempotency-Key can (contract 1.3.0).
func messageFingerprint(req *oas.CreateMessage) ([sha256.Size]byte, error) {
ids := make([]uuid.UUID, 0, len(req.AttachmentIds))
ids = append(ids, req.AttachmentIds...)
slices.SortFunc(ids, func(left, right uuid.UUID) int {
return bytes.Compare(left[:], right[:])
})
payload, err := json.Marshal(struct {
Text string `json:"text"`
}{Text: req.Text})
Text string `json:"text"`
AttachmentIDs []uuid.UUID `json:"attachment_ids"`
}{Text: req.Text, AttachmentIDs: ids})
if err != nil {
return [sha256.Size]byte{}, fmt.Errorf("encode message payload: %w", err)
}
@@ -396,11 +418,15 @@ func chatResponse(chat domain.Chat) oas.Chat {
}
func messageResponse(message domain.Message) oas.Message {
ids := make([]uuid.UUID, 0, len(message.AttachmentIDs))
ids = append(ids, message.AttachmentIDs...)
return oas.Message{
ID: message.ID,
ChatID: message.ChatID,
SenderID: message.SenderID,
Text: message.Text,
CreatedAt: message.CreatedAt,
ID: message.ID,
ChatID: message.ChatID,
SenderID: message.SenderID,
Text: message.Text,
AttachmentIds: ids,
CreatedAt: message.CreatedAt,
}
}