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
+37 -11
View File
@@ -3,17 +3,22 @@ package domain
import (
"crypto/sha256"
"fmt"
"strings"
"time"
"github.com/google/uuid"
)
// MaxMessageAttachments is the contract limit for attachment_ids on a message.
const MaxMessageAttachments = 10
type Message struct {
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
CreatedAt time.Time
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
AttachmentIDs []uuid.UUID
CreatedAt time.Time
}
// MessageIdempotency is the client Idempotency-Key together with the fingerprint
@@ -25,15 +30,36 @@ type MessageIdempotency struct {
}
type MessageDraft struct {
ChatID uuid.UUID
SenderID uuid.UUID
Text string
Idempotency MessageIdempotency
ChatID uuid.UUID
SenderID uuid.UUID
Text string
AttachmentIDs []uuid.UUID
Idempotency MessageIdempotency
}
// Validate enforces the contract rule that a message carries either non-blank
// text or at least one attachment, never neither.
func (d MessageDraft) Validate() error {
if err := validateNotBlank("text", d.Text); err != nil {
return err
if strings.TrimSpace(d.Text) == "" && len(d.AttachmentIDs) == 0 {
return fmt.Errorf("message has neither text nor attachments: %w", ErrValidation)
}
if len(d.AttachmentIDs) > MaxMessageAttachments {
return fmt.Errorf("too many attachments: %w", ErrValidation)
}
seen := make(map[uuid.UUID]struct{}, len(d.AttachmentIDs))
for _, id := range d.AttachmentIDs {
if id == uuid.Nil {
return fmt.Errorf("attachment id is empty: %w", ErrValidation)
}
if _, ok := seen[id]; ok {
return fmt.Errorf("duplicate attachment id %s: %w", id, ErrValidation)
}
seen[id] = struct{}{}
}
if d.Idempotency.Key == "" {