forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
// Package media implements attachment authorization and the S3 publication protocol.
|
||||
package media
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
||||
)
|
||||
|
||||
// Metadata persists reservations before object creation.
|
||||
type Metadata interface {
|
||||
// Reserve inserts an uploading reservation.
|
||||
Reserve(context.Context, dto.Attachment) error
|
||||
// QueueAttachment atomically exposes queued metadata and durable processing intent.
|
||||
QueueAttachment(context.Context, uuid.UUID) (dto.Attachment, error)
|
||||
// Attachment retrieves metadata by a validated ID.
|
||||
Attachment(context.Context, uuid.UUID) (dto.Attachment, error)
|
||||
}
|
||||
|
||||
// Objects exposes private object operations without a concrete S3 client.
|
||||
type Objects interface {
|
||||
// Put uploads a known bounded length without a multipart buffer.
|
||||
Put(context.Context, string, string, io.Reader, int64) error
|
||||
// Get opens an original stream; the caller closes it.
|
||||
Get(context.Context, string) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// Chats authorizes access using the existing business service.
|
||||
type Chats interface {
|
||||
// ChatsGetByID checks the caller and membership.
|
||||
ChatsGetByID(context.Context, string, string) (dto.ChatDTO, error)
|
||||
}
|
||||
|
||||
// Service coordinates bounded validation and durable publication.
|
||||
type Service struct {
|
||||
metadata Metadata
|
||||
objects Objects
|
||||
chats Chats
|
||||
slots chan struct{}
|
||||
}
|
||||
|
||||
// New validates required dependencies and caps simultaneous image decoding.
|
||||
func New(metadata Metadata, objects Objects, chats Chats) (*Service, error) {
|
||||
if metadata == nil || objects == nil || chats == nil {
|
||||
return nil, fmt.Errorf("attachment dependencies are required")
|
||||
}
|
||||
return &Service{metadata: metadata, objects: objects, chats: chats, slots: make(chan struct{}, 2)}, nil
|
||||
}
|
||||
|
||||
// Upload validates access and original bytes before reserving and publishing.
|
||||
func (s *Service) Upload(ctx context.Context, user, chat, filename, contentType string, body []byte) (dto.Attachment, error) {
|
||||
scope, err := s.chats.ChatsGetByID(ctx, user, chat)
|
||||
if err != nil {
|
||||
return dto.Attachment{}, err
|
||||
}
|
||||
select {
|
||||
case s.slots <- struct{}{}:
|
||||
defer func() {
|
||||
<-s.slots
|
||||
}()
|
||||
default:
|
||||
return dto.Attachment{}, service.ErrUnavailable
|
||||
}
|
||||
if err := validateImage(filename, contentType, body); err != nil {
|
||||
return dto.Attachment{}, err
|
||||
}
|
||||
owner, err := uuid.Parse(user)
|
||||
if err != nil {
|
||||
return dto.Attachment{}, service.ErrUnauthorized
|
||||
}
|
||||
sum := sha256.Sum256(body)
|
||||
id := uuid.New()
|
||||
now := time.Now().UTC()
|
||||
attachment := dto.Attachment{
|
||||
ID: id,
|
||||
ChatID: scope.ID,
|
||||
OwnerID: owner,
|
||||
Filename: filename,
|
||||
ContentType: contentType,
|
||||
SizeBytes: int64(len(body)),
|
||||
SHA256: hex.EncodeToString(sum[:]),
|
||||
Status: "uploading",
|
||||
ObjectKey: "staging/" + id.String(),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := s.metadata.Reserve(ctx, attachment); err != nil {
|
||||
return dto.Attachment{}, err
|
||||
}
|
||||
if err := s.objects.Put(ctx, attachment.ObjectKey, contentType, bytes.NewReader(body), attachment.SizeBytes); err != nil {
|
||||
return dto.Attachment{}, service.ErrUnavailable
|
||||
}
|
||||
// A lost COMMIT acknowledgement must never trigger deletion of a possibly published object.
|
||||
return s.metadata.QueueAttachment(ctx, id)
|
||||
}
|
||||
|
||||
// Get authorizes metadata access without exposing private object coordinates.
|
||||
func (s *Service) Get(ctx context.Context, user, rawID string) (dto.Attachment, error) {
|
||||
id, err := uuid.Parse(rawID)
|
||||
if err != nil {
|
||||
return dto.Attachment{}, service.ErrInvalid
|
||||
}
|
||||
attachment, err := s.metadata.Attachment(ctx, id)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
return dto.Attachment{}, service.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return dto.Attachment{}, err
|
||||
}
|
||||
if _, err := s.chats.ChatsGetByID(ctx, user, attachment.ChatID.String()); err != nil {
|
||||
return dto.Attachment{}, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
// Download validates access and variant before opening the original stream.
|
||||
func (s *Service) Download(ctx context.Context, user, id, variant string) (dto.Attachment, io.ReadCloser, error) {
|
||||
attachment, err := s.Get(ctx, user, id)
|
||||
if err != nil {
|
||||
return dto.Attachment{}, nil, err
|
||||
}
|
||||
if attachment.Status != "ready" {
|
||||
return dto.Attachment{}, nil, service.ErrConflict
|
||||
}
|
||||
if variant != "" && variant != "original" {
|
||||
found := false
|
||||
for _, candidate := range attachment.Variants {
|
||||
if candidate.Name == variant {
|
||||
attachment.ObjectKey = candidate.ObjectKey
|
||||
attachment.ContentType = candidate.ContentType
|
||||
attachment.SizeBytes = candidate.SizeBytes
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return dto.Attachment{}, nil, service.ErrNotFound
|
||||
}
|
||||
}
|
||||
reader, err := s.objects.Get(ctx, attachment.ObjectKey)
|
||||
if err != nil {
|
||||
return dto.Attachment{}, nil, service.ErrUnavailable
|
||||
}
|
||||
return attachment, reader, nil
|
||||
}
|
||||
Reference in New Issue
Block a user