Переведите приём и чтение вложений на асинхронный контракт

This commit is contained in:
2026-11-18 15:33:32 +03:00
parent efc56b1382
commit bf970227d6
19 changed files with 667 additions and 367 deletions
+12 -1
View File
@@ -2,17 +2,20 @@ package config
import (
"fmt"
"math"
"strconv"
"time"
)
type S3 struct {
Endpoint, Region, Bucket string
AccessKey, SecretKey string
MaxBytes, MaxPixels int64
StagingRetention time.Duration
}
func LoadS3(lookup Lookup) (S3, error) {
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000}
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000, StagingRetention: 24 * time.Hour}
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
if v, ok := lookup(key); ok {
*dest = v
@@ -38,6 +41,14 @@ func LoadS3(lookup Lookup) (S3, error) {
*p.dest = n
}
}
if raw, ok := lookup("STAGING_RETENTION_SECONDS"); ok {
const maxSeconds = math.MaxInt64 / int64(time.Second)
n, err := strconv.ParseInt(raw, 10, 64)
if err != nil || n < 1 || n > maxSeconds {
return S3{}, fmt.Errorf("STAGING_RETENTION_SECONDS must be between 1 and %d", maxSeconds)
}
cfg.StagingRetention = time.Duration(n) * time.Second
}
return cfg, nil
}
+19
View File
@@ -3,6 +3,7 @@ package config_test
import (
"messenger/internal/platform/config"
"testing"
"time"
)
func TestS3LimitsAndSecretFiles(t *testing.T) {
@@ -25,3 +26,21 @@ func TestS3LimitsAndSecretFiles(t *testing.T) {
}
}
}
func TestS3RejectsInvalidStagingRetention(t *testing.T) {
values := map[string]string{"S3_ACCESS_KEY_ID_FILE": secretFile(t, "access\n"), "S3_SECRET_ACCESS_KEY_FILE": secretFile(t, "private\n")}
cfg, err := config.LoadS3(env(values))
if err != nil || cfg.StagingRetention != 24*time.Hour {
t.Fatalf("default retention %v %v", cfg.StagingRetention, err)
}
values["STAGING_RETENTION_SECONDS"] = "7200"
cfg, err = config.LoadS3(env(values))
if err != nil || cfg.StagingRetention != 2*time.Hour {
t.Fatalf("custom retention %v %v", cfg.StagingRetention, err)
}
for _, value := range []string{"0", "-1", "abc", "9223372036854775807"} {
values["STAGING_RETENTION_SECONDS"] = value
if _, err := config.LoadS3(env(values)); err == nil {
t.Errorf("accepted invalid retention %s", value)
}
}
}