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
+90
View File
@@ -0,0 +1,90 @@
package domain
import (
"time"
"github.com/google/uuid"
)
// AttachmentStatus is the lifecycle of an attachment. Only ready attachments
// are exposed to members; pending rows exist while the object is being written,
// failed rows are reserved for later labs.
type AttachmentStatus string
const (
AttachmentPending AttachmentStatus = "pending"
AttachmentReady AttachmentStatus = "ready"
AttachmentFailed AttachmentStatus = "failed"
)
func (s AttachmentStatus) Valid() bool {
switch s {
case AttachmentPending, AttachmentReady, AttachmentFailed:
return true
default:
return false
}
}
// Attachment describes the original object. ObjectKey is internal: it never
// leaves the service and must not be mapped into an API response.
type Attachment struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
Filename string
ContentType string
SizeBytes int64
SHA256 string
Status AttachmentStatus
ErrorCode *string
ObjectKey string
CreatedAt time.Time
UpdatedAt time.Time
}
// AttachmentDraft is a fully validated attachment awaiting persistence.
type AttachmentDraft struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
ObjectKey string
Filename string
ContentType string
SizeBytes int64
SHA256 string
}
// AttachmentState is the minimal projection used while validating message
// attachments in a transaction.
type AttachmentState struct {
ID uuid.UUID
Status AttachmentStatus
}
// AttachmentKey is the minimal projection used by the orphan sweeper.
type AttachmentKey struct {
ID uuid.UUID
ObjectKey string
Status AttachmentStatus
CreatedAt time.Time
}
// PresignRequest is the validated client intent for a direct upload.
type PresignRequest struct {
ChatID uuid.UUID
OwnerID uuid.UUID
Filename string
ContentType string
SizeBytes int64
SHA256 string
}
// PresignResult is returned by the server and never contains credentials.
type PresignResult struct {
AttachmentID uuid.UUID
UploadURL string
ExpiresAt time.Time
SizeBytes int64
SHA256 string
}
+12 -9
View File
@@ -3,13 +3,16 @@ package domain
import "errors"
var (
ErrNotFound = errors.New("not found")
ErrUsernameTaken = errors.New("username already taken")
ErrUnknownMember = errors.New("member does not exist")
ErrValidation = errors.New("validation failed")
ErrInvalidCursor = errors.New("invalid cursor")
ErrConflict = errors.New("conflict")
ErrIdempotency = errors.New("idempotency key already used with another payload")
ErrUnauthenticated = errors.New("unauthenticated")
ErrUnavailable = errors.New("temporarily unavailable")
ErrNotFound = errors.New("not found")
ErrUsernameTaken = errors.New("username already taken")
ErrUnknownMember = errors.New("member does not exist")
ErrValidation = errors.New("validation failed")
ErrInvalidCursor = errors.New("invalid cursor")
ErrConflict = errors.New("conflict")
ErrIdempotency = errors.New("idempotency key already used with another payload")
ErrUnauthenticated = errors.New("unauthenticated")
ErrUnavailable = errors.New("temporarily unavailable")
ErrTooLarge = errors.New("payload is too large")
ErrUnsupportedMedia = errors.New("unsupported media type")
ErrIncomplete = errors.New("object is not fully uploaded")
)
+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 == "" {