feat(worker): process attachments with thumbnails and bounded retries

This commit is contained in:
kk10
2026-11-15 16:50:27 +03:00
parent 8b0ab5440d
commit 19152e7d18
15 changed files with 994 additions and 50 deletions
+42 -2
View File
@@ -35,6 +35,7 @@ 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)
ListAttachmentVariants(ctx context.Context, arg db.ListAttachmentVariantsParams) ([]db.AttachmentVariant, error)
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
}
@@ -183,6 +184,8 @@ func (s *AttachmentService) Upload(
SizeBytes: row.SizeBytes,
Sha256: row.Sha256,
Status: model.AttachmentStatus(row.Status),
Width: row.Width,
Height: row.Height,
ErrorCode: row.ErrorCode,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
@@ -205,7 +208,24 @@ func (s *AttachmentService) Get(
return model.Attachment{}, err
}
rows, err := s.q.ListAttachmentVariants(ctx, db.ListAttachmentVariantsParams{AttachmentID: row.ID, PipelineVersion: row.PipelineVersion})
if err != nil {
return model.Attachment{}, err
}
variants := make([]model.AttachmentVariant, 0, len(rows))
for _, v := range rows {
variants = append(variants, model.AttachmentVariant{
Name: model.AttachmentVariantName(v.Variant),
ContentType: model.ContentType(v.ContentType),
SizeBytes: v.SizeBytes,
Width: v.Width,
Height: v.Height,
Sha256: v.Sha256,
})
}
return model.Attachment{
Variants: variants,
ID: row.ID,
ChatID: row.ChatID,
OwnerID: row.OwnerID,
@@ -214,6 +234,8 @@ func (s *AttachmentService) Get(
SizeBytes: row.SizeBytes,
Sha256: row.Sha256,
Status: model.AttachmentStatus(row.Status),
Width: row.Width,
Height: row.Height,
ErrorCode: row.ErrorCode,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
@@ -223,6 +245,7 @@ func (s *AttachmentService) Get(
func (s *AttachmentService) GetContent(
ctx context.Context,
userID, attachmentID uuid.UUID,
variant string,
) ([]byte, model.ContentType, error) {
row, err := s.q.GetAttachmentByID(ctx, attachmentID)
if postgres.IsNotFound(err) {
@@ -240,7 +263,24 @@ func (s *AttachmentService) GetContent(
return nil, "", ErrAttachmentNotReady
}
out, err := s.s3.Get(ctx, row.S3Key)
key, contentType := row.S3Key, model.ContentType(row.ContentType)
if variant != "" && variant != model.AttachmentContentOriginal {
rows, err := s.q.ListAttachmentVariants(ctx, db.ListAttachmentVariantsParams{AttachmentID: row.ID, PipelineVersion: row.PipelineVersion})
if err != nil {
return nil, "", err
}
found := false
for _, v := range rows {
if string(v.Variant) == variant {
key, contentType, found = v.S3Key, model.ContentType(v.ContentType), true
break
}
}
if !found {
return nil, "", ErrAttachmentNotFound
}
}
out, err := s.s3.Get(ctx, key)
if err != nil {
return nil, "", err
}
@@ -251,7 +291,7 @@ func (s *AttachmentService) GetContent(
return nil, "", err
}
return data, model.ContentType(row.ContentType), nil
return data, contentType, nil
}
func (s *AttachmentService) checkImage(ctx context.Context, r io.ReadSeeker, contentType string) (model.ContentType, int, int, error) {