feat(cleanup): remove stale queued attachments after grace period

This commit is contained in:
kk10
2026-11-03 15:12:55 +03:00
parent fbd9c56b1f
commit 1ee4f8ecec
7 changed files with 189 additions and 36 deletions
+14 -5
View File
@@ -30,6 +30,7 @@ type attachmentQueries interface {
CreateAttachment(ctx context.Context, arg db.CreateAttachmentParams) (db.Attachment, 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 {
@@ -104,11 +105,8 @@ func (s *AttachmentService) Upload(
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
return model.Attachment{}, err
}
if err := s.s3.Put(ctx, key, tmp, size, string(detectedType)); err != nil {
return model.Attachment{}, err
}
row, err := s.q.CreateAttachment(ctx, db.CreateAttachmentParams{
_, err = s.q.CreateAttachment(ctx, db.CreateAttachmentParams{
ID: id,
ChatID: chatID,
OwnerID: userID,
@@ -117,9 +115,20 @@ func (s *AttachmentService) Upload(
ContentType: db.AttachmentContentType(detectedType),
SizeBytes: int(size),
Sha256: hash,
Status: db.AttachmentStatusReady,
ErrorCode: "",
})
if postgres.IsNotFound(err) {
return model.Attachment{}, ErrAttachmentNotFound
}
if err != nil {
return model.Attachment{}, err
}
if err := s.s3.Put(ctx, key, tmp, size, string(detectedType)); err != nil {
return model.Attachment{}, err
}
row, err := s.q.MarkAttachmentReady(ctx, id)
if err != nil {
return model.Attachment{}, err
}