Files
mtusi2026lab5/internal/attachment/store/postgres.go
T

90 lines
3.2 KiB
Go

package store
import (
"context"
"crypto/sha256"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/attachment"
"messenger/internal/platform/apperr"
"messenger/internal/platform/postgres"
)
type Attachments struct{ pool *pgxpool.Pool }
var _ attachment.AttachmentRepository = (*Attachments)(nil)
func NewAttachments(pool *pgxpool.Pool) *Attachments { return &Attachments{pool: pool} }
func (r *Attachments) Enqueue(ctx context.Context, a attachment.Attachment, stage string, checkStage func() error) error {
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
// A request can pause after validation for longer than the GC grace.
// Verify the source after serializing with deletion of this exact key.
stageHash := sha256.Sum256([]byte(stage))
if err := attachment.LockHash(ctx, tx, stageHash[:]); err != nil {
return err
}
if err := checkStage(); err != nil {
return err
}
if err := attachment.LockHash(ctx, tx, a.FileHash); err != nil {
return err
}
if _, err := tx.Exec(ctx, `
INSERT INTO files(hash,size_bytes,mime_type,status) VALUES ($1,$2,$3,'pending')
ON CONFLICT (hash) DO NOTHING`, a.FileHash, a.SizeBytes, a.MIMEType); err != nil {
return err
}
if _, err := tx.Exec(ctx, `
INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at)
VALUES ($1,$2,$3,$4,$5,$6)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.FileHash, a.CreatedAt); err != nil {
return err
}
_, err := tx.Exec(ctx, `INSERT INTO attachment_jobs(attachment_id,source_key) VALUES ($1,$2)`, a.ID, stage)
return err
}))
}
func (r *Attachments) GetForMember(ctx context.Context, id, actor uuid.UUID) (attachment.Attachment, error) {
var a attachment.Attachment
// A single statement returns one snapshot of the published version, even
// when a worker completes a newer version during a download.
rows, err := r.pool.Query(ctx, `
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.file_hash, f.size_bytes, f.mime_type,
s.status, s.error_code, a.created_at, s.updated_at,
coalesce(v.variant,''),coalesce(v.object_key,''),coalesce(v.mime_type,''),
coalesce(v.size_bytes,0),coalesce(v.width,0),coalesce(v.height,0),v.sha256
FROM attachments a
JOIN chat_members m ON m.chat_id=a.chat_id AND m.user_id=$2
JOIN files f ON f.hash=a.file_hash
JOIN attachment_states s ON s.id=a.id
LEFT JOIN attachment_variants v ON v.attachment_id=a.id AND v.pipeline_version=a.published_version AND s.status='ready'
WHERE a.id=$1 ORDER BY v.variant`, id, actor)
if err != nil {
return a, postgres.Classify(err)
}
defer rows.Close()
for rows.Next() {
var v attachment.Variant
err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.FileHash, &a.SizeBytes, &a.MIMEType,
&a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
&v.Name, &v.ObjectKey, &v.MIMEType, &v.SizeBytes, &v.Width, &v.Height, &v.Hash)
if err != nil {
return attachment.Attachment{}, postgres.Classify(err)
}
if v.Name != "" {
a.Variants = append(a.Variants, v)
}
}
if err := rows.Err(); err != nil {
return attachment.Attachment{}, postgres.Classify(err)
}
if a.ID == uuid.Nil {
return a, apperr.NotFound("attachment not found")
}
return a, nil
}