feat(attachments): accept uploads through staging and transactional outbox

This commit is contained in:
kk10
2026-11-14 19:52:51 +03:00
parent 4dbbd65bcf
commit 8b0ab5440d
10 changed files with 70 additions and 262 deletions
+37 -7
View File
@@ -14,6 +14,8 @@ import (
"os"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"mtusi2026-messenger/internal/db"
"mtusi2026-messenger/internal/model"
@@ -31,13 +33,15 @@ var (
type attachmentQueries interface {
CreateAttachment(ctx context.Context, arg db.CreateAttachmentParams) (db.Attachment, error)
CreateOutboxEntry(ctx context.Context, arg db.CreateOutboxEntryParams) (db.Outbox, error)
GetAttachmentByID(ctx context.Context, id uuid.UUID) (db.Attachment, error)
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
MarkAttachmentReady(ctx context.Context, id uuid.UUID) (db.Attachment, error)
}
type AttachmentService struct {
pool txBeginner
q attachmentQueries
withTx func(pgx.Tx) attachmentQueries
s3 *s3store.Client
maxBytes int
maxPixels int
@@ -45,12 +49,17 @@ type AttachmentService struct {
}
func NewAttachmentService(
q attachmentQueries,
pool *pgxpool.Pool,
q *db.Queries,
s3 *s3store.Client,
maxBytes, maxPixels int,
) *AttachmentService {
return &AttachmentService{
q: q,
pool: pool,
q: q,
withTx: func(tx pgx.Tx) attachmentQueries {
return q.WithTx(tx)
},
s3: s3,
maxBytes: maxBytes,
maxPixels: maxPixels,
@@ -107,19 +116,37 @@ func (s *AttachmentService) Upload(
if err != nil {
return model.Attachment{}, err
}
jobID, err := uuid.NewV7()
if err != nil {
return model.Attachment{}, err
}
hash := hex.EncodeToString(hasher.Sum(nil))
key := "chats/" + chatID.String() + "/attachments/" + id.String()
stagingKey := "staging/" + id.String()
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
return model.Attachment{}, err
}
_, err = s.q.CreateAttachment(ctx, db.CreateAttachmentParams{
if err := s.s3.Put(ctx, stagingKey, tmp, size, string(detectedType)); err != nil {
return model.Attachment{}, err
}
tx, err := s.pool.Begin(ctx)
if err != nil {
return model.Attachment{}, err
}
defer tx.Rollback(ctx)
q := s.withTx(tx)
row, err := q.CreateAttachment(ctx, db.CreateAttachmentParams{
ID: id,
ChatID: chatID,
OwnerID: userID,
S3Key: key,
StagingKey: stagingKey,
JobID: jobID,
Filename: filename,
ContentType: db.AttachmentContentType(detectedType),
SizeBytes: int(size),
@@ -135,12 +162,15 @@ func (s *AttachmentService) Upload(
return model.Attachment{}, err
}
if err := s.s3.Put(ctx, key, tmp, size, string(detectedType)); err != nil {
if _, err := q.CreateOutboxEntry(ctx, db.CreateOutboxEntryParams{
AttachmentID: id,
Attempt: 1,
AvailableAt: row.CreatedAt,
}); err != nil {
return model.Attachment{}, err
}
row, err := s.q.MarkAttachmentReady(ctx, id)
if err != nil {
if err := tx.Commit(ctx); err != nil {
return model.Attachment{}, err
}
+1 -1
View File
@@ -77,7 +77,7 @@ func (s *MessageService) Create(
return model.Message{}, ErrAttachmentNotFound
}
for _, status := range statuses {
if status != db.AttachmentStatusReady {
if status == db.AttachmentStatusFailed {
return model.Message{}, ErrAttachmentNotReady
}
}