feat: add durable image processing pipeline
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
const attachmentColumns = `
|
||||
id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, created_at, updated_at, ready_at`
|
||||
object_key, status, created_at, updated_at, ready_at, pipeline_version, error_code, staging_key`
|
||||
|
||||
type rowScanner interface {
|
||||
Scan(...any) error
|
||||
@@ -74,7 +74,7 @@ func (s *Store) AttachmentForUser(ctx context.Context, attachmentID, userID stri
|
||||
SELECT ` + attachmentColumnsWithAlias("a") + `
|
||||
FROM attachments a
|
||||
JOIN chat_members member ON member.chat_id = a.chat_id AND member.user_id = $2
|
||||
WHERE a.id = $1 AND a.status = 'ready'`
|
||||
WHERE a.id = $1 AND a.status IN ('queued', 'processing', 'ready', 'failed')`
|
||||
|
||||
var attachment model.Attachment
|
||||
err := scanAttachment(s.pool.QueryRow(ctx, query, attachmentID, userID), &attachment)
|
||||
@@ -84,9 +84,68 @@ func (s *Store) AttachmentForUser(ctx context.Context, attachmentID, userID stri
|
||||
if err != nil {
|
||||
return model.Attachment{}, fmt.Errorf("get attachment for user: %w", err)
|
||||
}
|
||||
if attachment.Status == model.AttachmentReady {
|
||||
attachment.Variants, err = s.attachmentVariants(ctx, attachment.ID, attachment.PipelineVersion)
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
}
|
||||
return publicAttachment(attachment), nil
|
||||
}
|
||||
|
||||
func (s *Store) AttachmentVariantForUser(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
userID string,
|
||||
variantName string,
|
||||
) (model.Attachment, model.Variant, error) {
|
||||
attachment, err := s.AttachmentForUser(ctx, attachmentID, userID)
|
||||
if err != nil {
|
||||
return model.Attachment{}, model.Variant{}, err
|
||||
}
|
||||
if attachment.Status != model.AttachmentReady {
|
||||
return model.Attachment{}, model.Variant{}, ErrConflict
|
||||
}
|
||||
for _, variant := range attachment.Variants {
|
||||
if variant.Name == variantName {
|
||||
return attachment, variant, nil
|
||||
}
|
||||
}
|
||||
if variantName == "original" && attachment.ObjectKey != "" {
|
||||
return attachment, model.Variant{
|
||||
Name: "original", ContentType: attachment.ContentType, SizeBytes: attachment.SizeBytes,
|
||||
SHA256: attachment.SHA256, ObjectKey: attachment.ObjectKey,
|
||||
PipelineVersion: attachment.PipelineVersion,
|
||||
}, nil
|
||||
}
|
||||
return model.Attachment{}, model.Variant{}, ErrNotFound
|
||||
}
|
||||
|
||||
func (s *Store) attachmentVariants(ctx context.Context, attachmentID string, pipelineVersion int) ([]model.Variant, error) {
|
||||
const query = `
|
||||
SELECT name, content_type, size_bytes, sha256, width, height, object_key, pipeline_version
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id = $1 AND pipeline_version = $2
|
||||
ORDER BY CASE name
|
||||
WHEN 'original' THEN 0
|
||||
WHEN 'thumbnail' THEN 1
|
||||
WHEN 'crop' THEN 2
|
||||
WHEN 'optimized' THEN 3
|
||||
ELSE 4
|
||||
END`
|
||||
rows, err := s.pool.Query(ctx, query, attachmentID, pipelineVersion)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list attachment variants: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
variants, err := pgx.CollectRows(rows, pgx.RowToStructByPos[model.Variant])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan attachment variants: %w", err)
|
||||
}
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func (s *Store) AttachmentByStorage(ctx context.Context, attachmentID, objectKey string) (model.Attachment, error) {
|
||||
const query = `SELECT ` + attachmentColumns + ` FROM attachments WHERE id = $1 AND object_key = $2`
|
||||
|
||||
@@ -102,7 +161,12 @@ func (s *Store) AttachmentByStorage(ctx context.Context, attachmentID, objectKey
|
||||
}
|
||||
|
||||
func (s *Store) AttachmentExistsByObjectKey(ctx context.Context, objectKey string) (bool, error) {
|
||||
const query = `SELECT EXISTS (SELECT 1 FROM attachments WHERE object_key = $1)`
|
||||
const query = `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM attachments WHERE object_key = $1 OR staging_key = $1
|
||||
UNION ALL
|
||||
SELECT 1 FROM attachment_variants WHERE object_key = $1
|
||||
)`
|
||||
var exists bool
|
||||
if err := s.pool.QueryRow(ctx, query, objectKey).Scan(&exists); err != nil {
|
||||
return false, fmt.Errorf("check attachment by object key: %w", err)
|
||||
@@ -144,12 +208,18 @@ func (s *Store) ListAttachments(ctx context.Context, afterObjectKey string, limi
|
||||
}
|
||||
|
||||
func scanAttachmentWithReference(row rowScanner, attachment *model.Attachment) error {
|
||||
return row.Scan(
|
||||
var stagingKey *string
|
||||
err := row.Scan(
|
||||
&attachment.ID, &attachment.ChatID, &attachment.OwnerID, &attachment.Filename,
|
||||
&attachment.ContentType, &attachment.SizeBytes, &attachment.SHA256, &attachment.ObjectKey,
|
||||
&attachment.Status, &attachment.CreatedAt, &attachment.UpdatedAt, &attachment.ReadyAt,
|
||||
&attachment.PipelineVersion, &attachment.ErrorCode, &stagingKey,
|
||||
&attachment.Referenced,
|
||||
)
|
||||
if stagingKey != nil {
|
||||
attachment.StagingKey = *stagingKey
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) AttachmentReferenced(ctx context.Context, attachmentID string) (bool, error) {
|
||||
@@ -232,23 +302,36 @@ func (s *Store) DeleteAttachmentIfDeleting(ctx context.Context, attachmentID str
|
||||
}
|
||||
|
||||
func scanAttachment(row rowScanner, attachment *model.Attachment) error {
|
||||
return row.Scan(
|
||||
var stagingKey *string
|
||||
err := row.Scan(
|
||||
&attachment.ID, &attachment.ChatID, &attachment.OwnerID, &attachment.Filename,
|
||||
&attachment.ContentType, &attachment.SizeBytes, &attachment.SHA256, &attachment.ObjectKey,
|
||||
&attachment.Status, &attachment.CreatedAt, &attachment.UpdatedAt, &attachment.ReadyAt,
|
||||
&attachment.PipelineVersion, &attachment.ErrorCode, &stagingKey,
|
||||
)
|
||||
if stagingKey != nil {
|
||||
attachment.StagingKey = *stagingKey
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func attachmentColumnsWithAlias(alias string) string {
|
||||
return fmt.Sprintf(
|
||||
"%[1]s.id, %[1]s.chat_id, %[1]s.owner_id, %[1]s.filename, %[1]s.content_type, "+
|
||||
"%[1]s.size_bytes, %[1]s.sha256, %[1]s.object_key, %[1]s.status, "+
|
||||
"%[1]s.created_at, %[1]s.updated_at, %[1]s.ready_at",
|
||||
"%[1]s.created_at, %[1]s.updated_at, %[1]s.ready_at, %[1]s.pipeline_version, "+
|
||||
"%[1]s.error_code, %[1]s.staging_key",
|
||||
alias,
|
||||
)
|
||||
}
|
||||
|
||||
func publicAttachment(attachment model.Attachment) model.Attachment {
|
||||
attachment.Variants = []model.Variant{}
|
||||
variants := make([]model.Variant, 0, len(attachment.Variants))
|
||||
for _, variant := range attachment.Variants {
|
||||
if variant.Name != "original" {
|
||||
variants = append(variants, variant)
|
||||
}
|
||||
}
|
||||
attachment.Variants = variants
|
||||
return attachment
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user