feat: add direct upload to 5 track

This commit is contained in:
TAskMAster339
2026-10-18 23:13:35 +03:00
parent 4d91357932
commit d7e7ea93f1
30 changed files with 2942 additions and 580 deletions
+106
View File
@@ -0,0 +1,106 @@
package repositories
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
func (s *AttachmentStore) CleanupDirectUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
result := CleanupResult{DryRun: dryRun}
rows, err := s.db.Query(ctx, `SELECT id::text FROM direct_uploads WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
if err != nil {
return result, err
}
ids := []string{}
for rows.Next() {
var id string
if err = rows.Scan(&id); err != nil {
rows.Close()
return result, err
}
ids = append(ids, id)
}
err = rows.Err()
rows.Close()
if err != nil {
return result, err
}
result.Candidates = len(ids)
for _, id := range ids {
outcome, err := s.cleanupDirectUpload(ctx, id, dryRun, remove)
if err != nil {
result.Failed++
continue
}
switch outcome {
case "deleted":
result.Deleted++
case "eligible":
result.Eligible++
default:
result.Skipped++
}
}
if result.Failed > 0 {
return result, fmt.Errorf("%d direct upload cleanup operations failed", result.Failed)
}
return result, nil
}
func (s *AttachmentStore) cleanupDirectUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
tx, err := s.db.Begin(ctx)
if err != nil {
return "", err
}
defer tx.Rollback(context.Background())
var key, state string
var barrier, complete, purge, now time.Time
err = tx.QueryRow(ctx, `SELECT staging_key,state,complete_before+interval '600 seconds',complete_before,purge_after,clock_timestamp() FROM direct_uploads WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key, &state, &barrier, &complete, &purge, &now)
if errors.Is(err, pgx.ErrNoRows) {
return "skipped", nil
}
if err != nil {
return "", err
}
parsed, err := uuid.Parse(id)
if err != nil || key != "staging/direct/"+parsed.String() {
return "", errors.New("invalid direct staging key")
}
if state == "pending" && now.Before(barrier) {
return "skipped", nil
}
if dryRun {
return "eligible", nil
}
if err = remove(ctx, key); err != nil {
return "", err
}
if now.After(purge) && !now.Before(barrier) {
_, err = tx.Exec(ctx, `DELETE FROM direct_uploads WHERE id=$1`, id)
} else {
if state == "pending" && !now.Before(complete) {
state = "expired"
}
// An early cleanup is not final: a still valid POST can recreate staging.
// Keep a second cleanup after the safety barrier, then retain the receipt 24h.
next := barrier
if !now.Before(barrier) {
next = purge
}
_, err = tx.Exec(ctx, `UPDATE direct_uploads SET state=$2,cleanup_after=$3 WHERE id=$1`, id, state, next)
}
if err != nil {
return "", err
}
if err = tx.Commit(ctx); err != nil {
return "", err
}
return "deleted", nil
}