Files
mtusi2026lab4/internal/service/attachments.go
T

124 lines
3.7 KiB
Go

package service
import (
"context"
"io"
"log"
"strings"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/objectstore"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/upload"
"github.com/google/uuid"
)
type Attachments struct {
db *store.PostgresStore
objects *objectstore.S3
validator *upload.Validator
}
func NewAttachments(db *store.PostgresStore, objects *objectstore.S3) *Attachments {
return &Attachments{db: db, objects: objects, validator: upload.NewValidator("")}
}
func (s *Attachments) Ready(ctx context.Context) error { return s.objects.Check(ctx) }
func (s *Attachments) Create(ctx context.Context, actor, chatID, filename, mime string, body io.Reader, length int64) (model.Attachment, error) {
if !uuidPattern.MatchString(chatID) {
return model.Attachment{}, ErrInvalidRequest
}
chatID = strings.ToLower(chatID)
if _, err := s.db.GetChat(ctx, actor, chatID); err != nil {
return model.Attachment{}, err
}
if length > upload.MaxBytes {
return model.Attachment{}, upload.ErrTooLarge
}
p, err := s.validator.Prepare(ctx, body, mime, filename)
if err != nil {
return model.Attachment{}, err
}
defer func() {
if err := p.Close(); err != nil {
log.Print("attachment staging cleanup failed")
}
}()
id, err := uuid.NewRandom()
if err != nil {
return model.Attachment{}, store.ErrUnavailable
}
a := model.Attachment{ID: id.String(), ChatID: chatID, OwnerID: actor, Filename: filename,
ContentType: p.ContentType, SizeBytes: p.SizeBytes, SHA256: p.SHA256, Status: "ready", Variants: []model.Variant{}}
if err := s.db.BeginUpload(ctx, a.ID, chatID, actor); err != nil {
return model.Attachment{}, err
}
if err := s.objects.Put(ctx, a.ID, p.File, p.SizeBytes, p.ContentType, p.SHA256); err != nil {
return model.Attachment{}, err
}
key, _ := objectstore.ObjectKey(a.ID)
// Publication and intent removal commit together; failed/unknown outcomes
// are resolved by cleanup under the same DB row lock after the grace period.
return s.db.SaveAttachment(ctx, a, key)
}
type CleanupSummary struct {
DryRun bool `json:"dry_run"`
Candidates int `json:"candidates"`
Deleted int `json:"deleted"`
Protected int `json:"protected"`
WouldDelete int `json:"would_delete"`
Skipped int `json:"skipped"`
Errors int `json:"errors"`
}
func (s *Attachments) Cleanup(ctx context.Context, dryRun bool) (CleanupSummary, error) {
result := CleanupSummary{DryRun: dryRun}
ids, err := s.db.CleanupCandidates(ctx)
if err != nil {
return result, err
}
result.Candidates = len(ids)
for _, id := range ids {
action, err := s.db.CleanupUpload(ctx, id, dryRun, s.objects.Delete)
if err != nil {
result.Errors++
continue
}
switch action {
case "deleted":
result.Deleted++
case "protected":
result.Protected++
case "would_delete":
result.WouldDelete++
case "skipped":
result.Skipped++
}
}
if result.Errors > 0 {
return result, store.ErrUnavailable
}
return result, nil
}
func (s *Attachments) Get(ctx context.Context, actor, id string) (model.Attachment, error) {
if !uuidPattern.MatchString(id) {
return model.Attachment{}, ErrInvalidRequest
}
return s.db.GetAttachment(ctx, actor, strings.ToLower(id))
}
func (s *Attachments) Open(ctx context.Context, a model.Attachment) (objectstore.Original, error) {
original, err := s.objects.Open(ctx, a.ID)
if err != nil {
return objectstore.Original{}, err
}
if original.SizeBytes != a.SizeBytes || original.ContentType != a.ContentType {
original.Body.Close()
return objectstore.Original{}, store.ErrUnavailable
}
return original, nil
}