Выполнение lab4 на 4
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
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
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type PersistentMessenger struct{ db *store.PostgresStore }
|
||||
@@ -96,11 +97,24 @@ func (m *PersistentMessenger) ListChats(ctx context.Context, actor string, limit
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (m *PersistentMessenger) CreateMessage(ctx context.Context, actor, chatID, text, key string) (model.Message, error) {
|
||||
if !uuidPattern.MatchString(chatID) || !validText(text, 4000) || !idempotencyKeyPattern.MatchString(key) {
|
||||
func (m *PersistentMessenger) CreateMessage(ctx context.Context, actor, chatID, text, key string, attachmentIDs ...string) (model.Message, error) {
|
||||
if !uuidPattern.MatchString(chatID) || !utf8.ValidString(text) || utf8.RuneCountInString(text) > 4000 || (strings.TrimSpace(text) == "" && len(attachmentIDs) == 0) || len(attachmentIDs) > 10 || !idempotencyKeyPattern.MatchString(key) {
|
||||
return model.Message{}, ErrInvalidRequest
|
||||
}
|
||||
return m.db.CreateMessage(ctx, actor, strings.ToLower(chatID), text, key)
|
||||
normalized := make([]string, len(attachmentIDs))
|
||||
seen := make(map[string]bool, len(attachmentIDs))
|
||||
for i, id := range attachmentIDs {
|
||||
if !uuidPattern.MatchString(id) {
|
||||
return model.Message{}, ErrInvalidRequest
|
||||
}
|
||||
id = strings.ToLower(id)
|
||||
if seen[id] {
|
||||
return model.Message{}, ErrInvalidRequest
|
||||
}
|
||||
seen[id] = true
|
||||
normalized[i] = id
|
||||
}
|
||||
return m.db.CreateMessage(ctx, actor, strings.ToLower(chatID), text, key, normalized...)
|
||||
}
|
||||
func (m *PersistentMessenger) ListMessages(ctx context.Context, actor, chatID string, limit int, cursor string) (Page[model.Message], error) {
|
||||
chat, err := m.GetChat(ctx, actor, chatID)
|
||||
|
||||
Reference in New Issue
Block a user