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

80 lines
3.2 KiB
Go

package store
import (
"context"
"errors"
"fmt"
"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
blobs attachment.Blobs
}
func NewAttachments(pool *pgxpool.Pool, blobs attachment.Blobs) *Attachments {
return &Attachments{pool, blobs}
}
func (r *Attachments) IsMember(ctx context.Context, chatID, actor uuid.UUID) (bool, error) {
var ok bool
err := r.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2)`, chatID, actor).Scan(&ok)
return ok, postgres.Classify(err)
}
// Publication and garbage collection use the same hash lock. The transaction
// spans only publication, never client transfer or image decoding.
func (r *Attachments) Publish(ctx context.Context, a attachment.Attachment, stage string) error {
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
if _, err := tx.Exec(ctx, `SET LOCAL idle_in_transaction_session_timeout = '35s'`); err != nil {
return err
}
if err := attachment.LockHash(ctx, tx, a.FileHash); err != nil {
return err
}
var status string
err := tx.QueryRow(ctx, `SELECT status FROM files WHERE hash=$1 FOR UPDATE`, a.FileHash).Scan(&status)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return err
}
copyNeeded := status != "ready"
if !copyNeeded {
obj, e := r.blobs.Stat(ctx, attachment.BlobKey(a.FileHash))
if errors.Is(e, attachment.ErrObjectMissing) {
copyNeeded = true
} else if e != nil {
return apperr.Wrap(apperr.KindUnavailable, "object storage is unavailable", e)
} else if obj.Size != a.SizeBytes {
copyNeeded = true
}
}
if copyNeeded {
if err := r.blobs.Copy(ctx, stage, attachment.BlobKey(a.FileHash)); err != nil {
return apperr.Wrap(apperr.KindUnavailable, "object storage is unavailable", err)
}
}
_, err = tx.Exec(ctx, `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES($1,$2,$3,'ready') ON CONFLICT(hash) DO UPDATE SET size_bytes=excluded.size_bytes,mime_type=excluded.mime_type,status='ready'`, a.FileHash, a.SizeBytes, a.MIMEType)
if err != nil {
return fmt.Errorf("publish file: %w", err)
}
_, 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)
return err
}))
}
func (r *Attachments) GetForMember(ctx context.Context, id, actor uuid.UUID) (attachment.Attachment, error) {
var a attachment.Attachment
err := r.pool.QueryRow(ctx, `SELECT a.id,a.chat_id,a.owner_id,a.filename,a.file_hash,f.size_bytes,f.mime_type,f.status,a.created_at
FROM attachments a JOIN files f ON f.hash=a.file_hash
WHERE a.id=$1 AND EXISTS(SELECT 1 FROM chat_members m WHERE m.chat_id=a.chat_id AND m.user_id=$2)`, id, actor).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.FileHash, &a.SizeBytes, &a.MIMEType, &a.Status, &a.CreatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return attachment.Attachment{}, apperr.NotFound("attachment not found")
}
return a, postgres.Classify(err)
}