lab4: S3-вложения, presigned upload и проверки
This commit is contained in:
@@ -20,5 +20,6 @@ evidence
|
||||
.DS_Store
|
||||
bin
|
||||
coverage.out
|
||||
.cache
|
||||
coverage.out.tmp
|
||||
*.log
|
||||
|
||||
+23
-1
@@ -15,6 +15,7 @@ APP_ENV=dev
|
||||
SERVER_HOST=0.0.0.0
|
||||
SERVER_PORT=8080
|
||||
SERVER_READ_HEADER_TIMEOUT=10s
|
||||
SERVER_READ_TIMEOUT=60s
|
||||
SERVER_SHUTDOWN_TIMEOUT=10s
|
||||
# Предел размера тела запроса (байт): защищает реплику от чтения огромного тела
|
||||
# до валидации. Для вложений LAB4 (до 10 MiB) значение нужно поднять.
|
||||
@@ -65,7 +66,7 @@ IDEMPOTENCY_TTL_SECONDS=86400
|
||||
# budget, e.g. API_REPLICAS=4 API_CPU_LIMIT=0.25 API_MEM_LIMIT=192M.
|
||||
API_REPLICAS=2
|
||||
API_CPU_LIMIT=0.50
|
||||
API_MEM_LIMIT=256M
|
||||
API_MEM_LIMIT=384M
|
||||
|
||||
# Connections of ONE API replica to PostgreSQL; the total is replicas × value.
|
||||
# Track 1 checks whether the shared connection budget becomes the saturation
|
||||
@@ -82,3 +83,24 @@ POSTGRES_MEM_LIMIT=384M
|
||||
|
||||
# Loopback port of the HAProxy operator page (UP/DOWN of every replica).
|
||||
LB_STATS_PORT=8404
|
||||
|
||||
# --- Lab4: private S3 object store (SeaweedFS) -------------------------------
|
||||
# Synthetic local credentials. infra/s3/s3.json must contain the same values;
|
||||
# scripts/check_s3_creds.py enforces that before the stand starts.
|
||||
S3_ENDPOINT=http://localhost:8333
|
||||
S3_PUBLIC_ENDPOINT=http://127.0.0.1:8333
|
||||
S3_PORT=8333
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=messenger-attachments
|
||||
S3_ACCESS_KEY_ID=lab4app
|
||||
S3_SECRET_ACCESS_KEY=lab4app-secret
|
||||
S3_ADMIN_ACCESS_KEY=lab4admin
|
||||
S3_ADMIN_SECRET_ACCESS_KEY=lab4admin-secret
|
||||
|
||||
# Attachment limits from contract 1.4.0.
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Presigned upload lifetime and the sweeper's safety window.
|
||||
S3_PRESIGN_TTL=5m
|
||||
S3_SWEEP_INTERVAL=1m
|
||||
S3_SWEEP_GRACE=10m
|
||||
|
||||
@@ -47,7 +47,7 @@ jobs:
|
||||
tags: mtusi-messenger:ci
|
||||
|
||||
stand:
|
||||
name: Lab3 stand scenario
|
||||
name: Lab4 stand scenario
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
@@ -66,8 +66,14 @@ jobs:
|
||||
exit 1
|
||||
- name: Public smoke through the LB
|
||||
run: make test
|
||||
- name: Attachment negative scenarios
|
||||
run: make test-attachments
|
||||
- name: Presigned direct upload
|
||||
run: make test-presign
|
||||
- name: Parallel idempotency through the LB
|
||||
run: python3 student-tests/idempotency_parallel.py --base-url http://localhost:8080
|
||||
- name: Upload, stop the serving replica, download through another
|
||||
run: python3 student-tests/attachment_failover.py --base-url http://localhost:8080
|
||||
- name: Stop one API replica and keep writing
|
||||
run: make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
- name: Logs on failure
|
||||
|
||||
@@ -26,6 +26,7 @@ obj/
|
||||
coverage/
|
||||
coverage.out
|
||||
coverage.out.tmp
|
||||
.cache/
|
||||
*.log
|
||||
.DS_Store
|
||||
.idea/
|
||||
|
||||
@@ -27,6 +27,9 @@ packages:
|
||||
mtusi-messenger/internal/service/message:
|
||||
config:
|
||||
all: true
|
||||
mtusi-messenger/internal/service/attachment:
|
||||
config:
|
||||
all: true
|
||||
mtusi-messenger/internal/service/session:
|
||||
config:
|
||||
all: true
|
||||
|
||||
+14
-5
@@ -19,11 +19,15 @@ WORKDIR /opt
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# The Go compiler spikes well above 1 GiB per package on large dependencies, so
|
||||
# an unbounded parallel build OOMs small Docker VMs and CI runners. Cap
|
||||
# parallelism; override with --build-arg GOFLAGS=-p=$(nproc) on a bigger host.
|
||||
ARG GOFLAGS="-p=2"
|
||||
# The Go compiler spikes well above 1 GiB per package on large dependencies
|
||||
# (AWS SDK + pgx + Redis), so an unbounded or even -p=2 build OOMs Docker
|
||||
# Desktop's default VM. Build one package at a time; override with
|
||||
# --build-arg GOFLAGS=-p=$(nproc) on a bigger host.
|
||||
ARG GOFLAGS="-p=1"
|
||||
ENV GOFLAGS=$GOFLAGS
|
||||
# Keep the compiler well below Docker Desktop's default VM limit.
|
||||
ENV GOGC=50
|
||||
ENV GOMEMLIMIT=1GiB
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
|
||||
@@ -33,7 +37,11 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
|
||||
&& CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
|
||||
go build -trimpath -buildvcs=false -tags timetzdata \
|
||||
-ldflags="-s -w -X main.version=${VERSION}" \
|
||||
-o /out/migrate ./cmd/migrate
|
||||
-o /out/migrate ./cmd/migrate \
|
||||
&& CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
|
||||
go build -trimpath -buildvcs=false -tags timetzdata \
|
||||
-ldflags="-s -w -X main.version=${VERSION}" \
|
||||
-o /out/s3init ./cmd/s3init
|
||||
|
||||
FROM alpine:${ALPINE_VERSION} AS runtime
|
||||
RUN apk add --no-cache ca-certificates wget \
|
||||
@@ -42,6 +50,7 @@ RUN apk add --no-cache ca-certificates wget \
|
||||
|
||||
COPY --from=build --chown=10001:10001 /out/messenger /usr/local/bin/messenger
|
||||
COPY --from=build --chown=10001:10001 /out/migrate /usr/local/bin/migrate
|
||||
COPY --from=build --chown=10001:10001 /out/s3init /usr/local/bin/s3init
|
||||
COPY --chown=10001:10001 config/dev/config.yaml /etc/messenger/dev/config.yaml
|
||||
|
||||
USER 10001:10001
|
||||
|
||||
@@ -5,7 +5,7 @@ ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
PYTHON_LOCAL = env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1 $(PYTHON)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover smoke-local load-local test-idempotency test-lb-readiness up down ps logs
|
||||
.PHONY: help check validate test test-attachments test-presign load test-persistence test-failover smoke-local load-local test-idempotency test-lb-readiness up down ps logs
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@@ -13,6 +13,7 @@ help:
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
$(PYTHON) scripts/check_s3_creds.py
|
||||
|
||||
validate:
|
||||
$(PYTHON) scripts/validate_openapi.py
|
||||
@@ -20,6 +21,12 @@ validate:
|
||||
test:
|
||||
$(PYTHON_LOCAL) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-attachments:
|
||||
$(PYTHON_LOCAL) student-tests/attachments_negative.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-presign:
|
||||
$(PYTHON_LOCAL) student-tests/presign_flow.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
load:
|
||||
$(PYTHON_LOCAL) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
@@ -116,7 +123,8 @@ generate: generate-api generate-mocks sqlc
|
||||
|
||||
.PHONY: generate-api
|
||||
generate-api:
|
||||
$(GO) tool ogen --config $(OGEN_CONFIG) --target $(OAS_DIR) --package oas --clean $(OPENAPI_SPEC)
|
||||
$(GO) run ./tools/specgen
|
||||
$(GO) tool ogen --config $(OGEN_CONFIG) --target $(OAS_DIR) --package oas --clean .cache/openapi.codegen.json
|
||||
|
||||
.PHONY: generate-mocks
|
||||
generate-mocks:
|
||||
|
||||
+59
-1
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -15,7 +16,9 @@ import (
|
||||
"mtusi-messenger/internal/health"
|
||||
"mtusi-messenger/internal/logger"
|
||||
"mtusi-messenger/internal/platform/cache"
|
||||
"mtusi-messenger/internal/platform/objectstore"
|
||||
"mtusi-messenger/internal/platform/token"
|
||||
"mtusi-messenger/internal/service/attachment"
|
||||
"mtusi-messenger/internal/service/chat"
|
||||
"mtusi-messenger/internal/service/message"
|
||||
"mtusi-messenger/internal/service/session"
|
||||
@@ -57,6 +60,57 @@ func run(cfg config.Config, log *slog.Logger) error {
|
||||
|
||||
defer store.Close()
|
||||
|
||||
objectStore, err := objectstore.New(objectstore.Config{
|
||||
Endpoint: cfg.S3.Endpoint,
|
||||
PublicEndpoint: cfg.S3.PublicEndpoint,
|
||||
Region: cfg.S3.Region,
|
||||
Bucket: cfg.S3.Bucket,
|
||||
AccessKeyID: cfg.S3.AccessKeyID,
|
||||
SecretAccessKey: cfg.S3.SecretAccessKey,
|
||||
UsePathStyle: cfg.S3.UsePathStyle,
|
||||
Timeout: cfg.S3.Timeout,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect object store: %w", err)
|
||||
}
|
||||
|
||||
attachmentRepository := store.Attachments()
|
||||
attachmentService := attachment.NewService(
|
||||
attachmentRepository,
|
||||
objectStore,
|
||||
attachment.Config{
|
||||
MaxUploadBytes: cfg.S3.MaxUploadBytes,
|
||||
MaxImagePixels: cfg.S3.MaxImagePixels,
|
||||
PresignTTL: cfg.S3.PresignTTL,
|
||||
},
|
||||
log,
|
||||
)
|
||||
|
||||
sweeper := attachment.NewSweeper(
|
||||
attachmentRepository,
|
||||
objectStore,
|
||||
store,
|
||||
cfg.S3.SweepGrace,
|
||||
cfg.S3.SweepInterval,
|
||||
log,
|
||||
)
|
||||
|
||||
sweepCtx, stopSweeper := context.WithCancel(context.Background())
|
||||
var sweepWait sync.WaitGroup
|
||||
|
||||
sweepWait.Add(1)
|
||||
|
||||
go func() {
|
||||
defer sweepWait.Done()
|
||||
|
||||
sweeper.Run(sweepCtx)
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
stopSweeper()
|
||||
sweepWait.Wait()
|
||||
}()
|
||||
|
||||
redisCache, err := connectRedis(cfg.Redis)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -97,13 +151,16 @@ func run(cfg config.Config, log *slog.Logger) error {
|
||||
rest.NewHandler(
|
||||
user.NewService(store.Users()),
|
||||
chat.NewService(chatRepo),
|
||||
message.NewService(store.Messages()),
|
||||
message.NewService(store.Messages(), attachmentRepository),
|
||||
attachmentService,
|
||||
health.New(pingers...),
|
||||
sessionService,
|
||||
),
|
||||
rest.NewSecurityHandler(sessionService),
|
||||
rest.NewExtensions(attachmentService, sessionService, log),
|
||||
log,
|
||||
cfg.Server.MaxBodyBytes,
|
||||
cfg.S3.MaxUploadBytes,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("build router: %w", err)
|
||||
@@ -113,6 +170,7 @@ func run(cfg config.Config, log *slog.Logger) error {
|
||||
Addr: cfg.Addr(),
|
||||
Handler: router,
|
||||
ReadHeaderTimeout: cfg.Server.ReadHeaderTimeout,
|
||||
ReadTimeout: cfg.Server.ReadTimeout,
|
||||
}
|
||||
|
||||
errc := make(chan error, 1)
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
// Command s3init bootstraps the private attachment bucket. It is idempotent:
|
||||
// it runs before the API on every compose up, verifies the bucket and the
|
||||
// application identity, and exits successfully when everything is in place.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const bootstrapTimeout = 120 * time.Second
|
||||
|
||||
type settings struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
AdminAccessKey string
|
||||
AdminSecretKey string
|
||||
AppAccessKey string
|
||||
AppSecretKey string
|
||||
}
|
||||
|
||||
func main() {
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
|
||||
if err := run(log); err != nil {
|
||||
log.Error("s3 bootstrap failed", slog.Any("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(log *slog.Logger) error {
|
||||
cfg, err := loadSettings()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), bootstrapTimeout)
|
||||
defer cancel()
|
||||
|
||||
admin := newClient(cfg, cfg.AdminAccessKey, cfg.AdminSecretKey)
|
||||
app := newClient(cfg, cfg.AppAccessKey, cfg.AppSecretKey)
|
||||
|
||||
if err := waitForBucket(ctx, admin, cfg.Bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := probeAppIdentity(ctx, app, cfg.Bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := probeForbiddenPrefix(ctx, app, cfg.Bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("s3 bucket ready",
|
||||
slog.String("endpoint", cfg.Endpoint),
|
||||
slog.String("bucket", cfg.Bucket),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadSettings() (settings, error) {
|
||||
cfg := settings{
|
||||
Endpoint: strings.TrimSpace(os.Getenv("S3_ENDPOINT")),
|
||||
Region: strings.TrimSpace(os.Getenv("S3_REGION")),
|
||||
Bucket: strings.TrimSpace(os.Getenv("S3_BUCKET")),
|
||||
AdminAccessKey: strings.TrimSpace(os.Getenv("S3_ADMIN_ACCESS_KEY")),
|
||||
AdminSecretKey: strings.TrimSpace(os.Getenv("S3_ADMIN_SECRET_ACCESS_KEY")),
|
||||
AppAccessKey: strings.TrimSpace(os.Getenv("S3_ACCESS_KEY_ID")),
|
||||
AppSecretKey: strings.TrimSpace(os.Getenv("S3_SECRET_ACCESS_KEY")),
|
||||
}
|
||||
|
||||
switch {
|
||||
case cfg.Endpoint == "":
|
||||
return settings{}, errors.New("S3_ENDPOINT is required")
|
||||
case cfg.Region == "":
|
||||
return settings{}, errors.New("S3_REGION is required")
|
||||
case cfg.Bucket == "":
|
||||
return settings{}, errors.New("S3_BUCKET is required")
|
||||
case cfg.AdminAccessKey == "" || cfg.AdminSecretKey == "":
|
||||
return settings{}, errors.New("S3_ADMIN_ACCESS_KEY and S3_ADMIN_SECRET_ACCESS_KEY are required")
|
||||
case cfg.AppAccessKey == "" || cfg.AppSecretKey == "":
|
||||
return settings{}, errors.New("S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY are required")
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func newClient(cfg settings, accessKey, secretKey string) *s3.Client {
|
||||
return s3.New(s3.Options{
|
||||
Region: cfg.Region,
|
||||
BaseEndpoint: aws.String(cfg.Endpoint),
|
||||
UsePathStyle: true,
|
||||
Credentials: credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
|
||||
})
|
||||
}
|
||||
|
||||
func waitForBucket(ctx context.Context, client *s3.Client, bucket string) error {
|
||||
for {
|
||||
_, err := client.HeadBucket(ctx, &s3.HeadBucketInput{Bucket: aws.String(bucket)})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !isNotFound(err) {
|
||||
return fmt.Errorf("head bucket: %w", err)
|
||||
}
|
||||
|
||||
if _, err := client.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: aws.String(bucket)}); err == nil {
|
||||
return nil
|
||||
} else if isBucketExists(err) {
|
||||
return nil
|
||||
} else if !isNotFound(err) {
|
||||
return fmt.Errorf("create bucket: %w", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("wait for object store: %w", ctx.Err())
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func probeAppIdentity(ctx context.Context, client *s3.Client, bucket string) error {
|
||||
key := "staging/.bootstrap/" + uuid.NewString()
|
||||
|
||||
if _, err := client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
Body: bytes.NewReader([]byte("ok")),
|
||||
ContentLength: aws.Int64(2),
|
||||
ContentType: aws.String("text/plain"),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("application identity cannot write: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_, _ = client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
}()
|
||||
|
||||
if _, err := client.HeadObject(ctx, &s3.HeadObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("application identity cannot read: %w", err)
|
||||
}
|
||||
|
||||
if _, err := client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(bucket),
|
||||
Prefix: aws.String("staging/"),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("application identity cannot list: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func probeForbiddenPrefix(ctx context.Context, client *s3.Client, bucket string) error {
|
||||
key := "forbidden/" + uuid.NewString()
|
||||
|
||||
_, err := client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
Body: bytes.NewReader([]byte("must not be stored")),
|
||||
})
|
||||
if err == nil {
|
||||
_, _ = client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
|
||||
return errors.New("application identity can write outside the allowed prefixes; check infra/s3/s3.json")
|
||||
}
|
||||
|
||||
var apiErr smithy.APIError
|
||||
if !errors.As(err, &apiErr) || apiErr.ErrorCode() != "AccessDenied" {
|
||||
return fmt.Errorf("forbidden-prefix probe returned an unexpected error: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isNotFound(err error) bool {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "NotFound", "NoSuchBucket", "NoSuchKey":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isBucketExists(err error) bool {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "BucketAlreadyExists", "BucketAlreadyOwnedByYou":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
+73
-7
@@ -1,4 +1,4 @@
|
||||
# Lab3 stand: two API replicas behind a HAProxy load balancer.
|
||||
# Lab4 stand: two API replicas behind HAProxy, PostgreSQL, Redis and SeaweedFS S3.
|
||||
#
|
||||
# cp -n .env.example .env
|
||||
# docker compose --env-file .env up -d --build
|
||||
@@ -8,13 +8,13 @@
|
||||
# reachable from the host via `docker compose exec` / `docker compose logs`.
|
||||
# No container_name anywhere: replica hostnames stay the short container IDs,
|
||||
# which the application reports back as X-Instance-Id.
|
||||
name: mtusi-messenger-lab3
|
||||
name: mtusi-messenger-lab4
|
||||
|
||||
# The image is built once by the api service. migrate and s3-init only consume
|
||||
# it; duplicating `build` here makes Compose compile the same Go tree in parallel
|
||||
# and OOMs Docker Desktop's default VM.
|
||||
x-app: &app
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab3
|
||||
image: mtusi-messenger:lab4
|
||||
|
||||
services:
|
||||
postgres:
|
||||
@@ -67,6 +67,54 @@ services:
|
||||
memory: 192M
|
||||
restart: unless-stopped
|
||||
|
||||
# Private S3-compatible object store. Only the S3 API is published, on
|
||||
# loopback, so the presigned-upload track and evidence can reach it from the
|
||||
# host. master/volume/filer ports stay inside the compose network.
|
||||
seaweedfs:
|
||||
image: chrislusf/seaweedfs:3.80@sha256:1055999e08eed1789b0ae45d235126e4495e23d3fb9d6396293fd42539b1ae6a
|
||||
command:
|
||||
- server
|
||||
- -s3
|
||||
- -s3.config=/etc/seaweedfs/s3.json
|
||||
- -dir=/data
|
||||
- -master.volumeSizeLimitMB=64
|
||||
volumes:
|
||||
- ./infra/s3/s3.json:/etc/seaweedfs/s3.json:ro
|
||||
- seaweedfs-data:/data
|
||||
ports:
|
||||
- "127.0.0.1:${S3_PORT:-8333}:8333"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8333/healthz || exit 1"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
start_period: 60s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.00"
|
||||
memory: 768M
|
||||
restart: unless-stopped
|
||||
|
||||
# One-shot, idempotent bucket bootstrap and identity probe.
|
||||
s3-init:
|
||||
<<: *app
|
||||
entrypoint: ["/usr/local/bin/s3init"]
|
||||
environment:
|
||||
S3_ENDPOINT: http://seaweedfs:8333
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ADMIN_ACCESS_KEY: ${S3_ADMIN_ACCESS_KEY:-lab4admin}
|
||||
S3_ADMIN_SECRET_ACCESS_KEY: ${S3_ADMIN_SECRET_ACCESS_KEY:-lab4admin-secret}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-lab4app}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-lab4app-secret}
|
||||
depends_on:
|
||||
seaweedfs:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: "no"
|
||||
|
||||
# One-shot schema bootstrap; runs once to completion before any API replica.
|
||||
migrate:
|
||||
<<: *app
|
||||
@@ -95,6 +143,9 @@ services:
|
||||
|
||||
api:
|
||||
<<: *app
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
# Общий сетевой alias: LB резолвит одно имя в адреса всех реплик, поэтому
|
||||
# масштабирование и замена контейнеров не зависят от их номеров/ID.
|
||||
networks:
|
||||
@@ -117,6 +168,18 @@ services:
|
||||
REDIS_HOST: redis
|
||||
REDIS_PORT: "6379"
|
||||
CACHE_ENABLED: "true"
|
||||
S3_ENDPOINT: http://seaweedfs:8333
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:8333}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-lab4app}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-lab4app-secret}
|
||||
S3_USE_PATH_STYLE: "true"
|
||||
S3_PRESIGN_TTL: ${S3_PRESIGN_TTL:-5m}
|
||||
S3_SWEEP_INTERVAL: ${S3_SWEEP_INTERVAL:-1m}
|
||||
S3_SWEEP_GRACE: ${S3_SWEEP_GRACE:-10m}
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
# Размер пула соединений ОДНОЙ реплики; общий расход = реплики × значение.
|
||||
# Это один из общих ресурсов, который масштабирование расходует первым.
|
||||
POSTGRES_MAX_CONNS: "${POSTGRES_MAX_CONNS:-10}"
|
||||
@@ -125,7 +188,7 @@ services:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${API_CPU_LIMIT:-0.50}"
|
||||
memory: ${API_MEM_LIMIT:-256M}
|
||||
memory: ${API_MEM_LIMIT:-384M}
|
||||
# The app drains on SIGTERM within SERVER_SHUTDOWN_TIMEOUT; keep the grace
|
||||
# period above it so Docker does not SIGKILL an in-flight request.
|
||||
stop_grace_period: 15s
|
||||
@@ -136,6 +199,8 @@ services:
|
||||
condition: service_healthy
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"]
|
||||
interval: 10s
|
||||
@@ -171,3 +236,4 @@ services:
|
||||
volumes:
|
||||
postgres18-data:
|
||||
redis7-data:
|
||||
seaweedfs-data:
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
name: mtusi-messenger-lab2
|
||||
name: mtusi-messenger-lab4
|
||||
|
||||
x-app: &app
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab2
|
||||
image: mtusi-messenger:lab4
|
||||
|
||||
services:
|
||||
postgres:
|
||||
@@ -99,6 +99,6 @@ services:
|
||||
|
||||
volumes:
|
||||
postgres18-data:
|
||||
name: mtusi-messenger-lab2-postgres18-data
|
||||
name: mtusi-messenger-lab4-postgres18-data
|
||||
redis7-data:
|
||||
name: mtusi-messenger-lab2-redis7-data
|
||||
name: mtusi-messenger-lab4-redis7-data
|
||||
|
||||
@@ -3,6 +3,10 @@ module mtusi-messenger
|
||||
go 1.27.0
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.3
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.9
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.4
|
||||
github.com/aws/smithy-go v1.28.6
|
||||
github.com/go-faster/errors v0.8.0
|
||||
github.com/go-faster/jx v1.2.0
|
||||
github.com/go-playground/validator/v10 v10.30.5
|
||||
@@ -23,6 +27,14 @@ require (
|
||||
cel.dev/expr v0.25.2 // indirect
|
||||
filippo.io/edwards25519 v1.2.0 // indirect
|
||||
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.23 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.21 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.7 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.6 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.6 // indirect
|
||||
github.com/brunoga/deep v1.3.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
|
||||
|
||||
@@ -5,6 +5,30 @@ filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
|
||||
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.3 h1:HRoT/MkNi3bjO5xF4dCzLzvVmkvRGHj3/cLINDnJejk=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.3/go.mod h1:xSl+OSI5ZUw6bSMupWc218zE5Pl+65MCCu3ySWFY99A=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.23 h1:/fGPpKrXTjzkhsDXExBgNT2cGwxiF9rLrQXh8RlXMhI=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.23/go.mod h1:QWUDuRi9PFjBHIU2iH1Pkldbe7zqLfXJC4wWLbF8nuQ=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.9 h1:SNO55PnWhaDafsOcv16lCA0kdoFMl/YlegWoVsU02s0=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.9/go.mod h1:+2reOTRtdD3VSBqpRDNwrZIYy8yMCYa7oK3WKTJ/I1I=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.6 h1:CqL8NN3p4I0JTwwV5MEoatIl4Y2amZ2X9dRD+0hbRL0=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.6/go.mod h1:exdJ32hMqXl0Wyo6NwqEC5yIhtua3+PDaKE8P29m4qE=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.6 h1:FpBq24TG6WVSObTNhQOv7Q0088rounUUlwn3zlNItkM=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.6/go.mod h1:ONzdPVYC95oBOu0VWNEsKEntcoemP3xVKjT+qWml1s4=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.6 h1:Qmn60RWuk9+bd2ddvJJqwf3Cpac3VvGMOdbGemtUhX4=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.6/go.mod h1:h/YJhe4AVyQxEUMluph10iqV7mjajH7A9L+BHF5ppis=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.21 h1:e6YW5uME30TkrElR+4/xcY7NB6CCj2dcv7vtmGXhfhQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.21/go.mod h1:obpHAMP693+f3/A8PkLhIqfK1C+NWWn8zXzqD3OH520=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.7 h1:CwvFlKw0uUScfgOMflXSRZa+ayXgOf6wk7lhyMemQYU=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.7/go.mod h1:4SbD8BB+p+QEnG0aX6DoWwrtk9SufyYXRsUv0nve8LE=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.6 h1:LHHdobjo5le1sjJdolitV7uVvYg3wkaeM1Ju2fDgeZQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.6/go.mod h1:CBGB0evAiaMD3Lk4wPO7LQCcCZLuhzL5Qf/wfEUHQ5w=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.6 h1:mYViDr2azcgxOpRrObZOl631bvavqILoE/L+Kxgbwys=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.6/go.mod h1:OXVdAo9jNhggB+hJRGOyy7WolGTKB6Rg7IIulDeSzPM=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.4 h1:MrkWRB58Qw5TEkb51QVooVLjlM3IevO2iVXEz74B8pg=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.4/go.mod h1:yfIxaKRHasvfy6tNEm3/LpqKsupMzQjMaWj9Y0jMVbo=
|
||||
github.com/aws/smithy-go v1.28.6 h1:Pj+/sUviNYjow8ELRtNwKHZ9ECeF+7pATX3+shjLIrw=
|
||||
github.com/aws/smithy-go v1.28.6/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/brunoga/deep v1.3.1 h1:bSrL6FhAZa6JlVv4vsi7Hg8SLwroDb1kgDERRVipBCo=
|
||||
github.com/brunoga/deep v1.3.1/go.mod h1:GDV6dnXqn80ezsLSZ5Wlv1PdKAWAO4L5PnKYtv2dgaI=
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# SeaweedFS identities
|
||||
|
||||
`infra/s3/s3.json` is mounted read-only into the `seaweedfs` container and is
|
||||
the single source of truth for S3 credentials and permissions.
|
||||
|
||||
- `admin` is used only by the one-shot `s3-init` service (bucket creation and
|
||||
probing). It is never given to the API.
|
||||
- `app` is used by the API and by presigned uploads. It can read/write only
|
||||
`messenger-attachments/attachments/*` and `messenger-attachments/staging/*`,
|
||||
and list the bucket for the orphan sweeper.
|
||||
|
||||
The credentials are synthetic values for the local stand. `.env.example`
|
||||
carries the same values for the API and `scripts/check_s3_creds.py` fails the
|
||||
offline check if they drift. Replace them in `.env` for any non-local run.
|
||||
|
||||
Verify manually:
|
||||
|
||||
```sh
|
||||
# anonymous requests are denied
|
||||
docker compose exec -T seaweedfs \
|
||||
wget -qO- http://127.0.0.1:8333/messenger-attachments/attachments/missing
|
||||
|
||||
# the app identity cannot write outside its prefixes
|
||||
docker compose run --rm s3-init # probes this and exits non-zero on violation
|
||||
```
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"identities": [
|
||||
{
|
||||
"name": "admin",
|
||||
"credentials": [
|
||||
{"accessKey": "lab4admin", "secretKey": "lab4admin-secret"}
|
||||
],
|
||||
"actions": ["Admin", "Read", "Write", "List", "Tagging"]
|
||||
},
|
||||
{
|
||||
"name": "app",
|
||||
"credentials": [
|
||||
{"accessKey": "lab4app", "secretKey": "lab4app-secret"}
|
||||
],
|
||||
"actions": [
|
||||
"Read:messenger-attachments/attachments/*",
|
||||
"Write:messenger-attachments/attachments/*",
|
||||
"Read:messenger-attachments/staging/*",
|
||||
"Write:messenger-attachments/staging/*",
|
||||
"List:messenger-attachments"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -36,6 +36,27 @@ type Config struct {
|
||||
Cache Cache `mapstructure:"cache"`
|
||||
Session Session `mapstructure:"session"`
|
||||
Idempotency Idempotency `mapstructure:"idempotency"`
|
||||
S3 S3 `mapstructure:"s3"`
|
||||
}
|
||||
|
||||
// S3 describes the object store and attachment limits. Access keys are checked
|
||||
// by the object-store constructor rather than the global validator: cmd/migrate
|
||||
// and unit tests load this config without any S3 credentials.
|
||||
type S3 struct {
|
||||
Endpoint string `mapstructure:"endpoint" validate:"required,url"`
|
||||
PublicEndpoint string `mapstructure:"public_endpoint" validate:"omitempty,url"`
|
||||
Region string `mapstructure:"region" validate:"required"`
|
||||
Bucket string `mapstructure:"bucket" validate:"required"`
|
||||
AccessKeyID string `mapstructure:"access_key_id"`
|
||||
SecretAccessKey string `mapstructure:"secret_access_key"`
|
||||
UsePathStyle bool `mapstructure:"use_path_style"`
|
||||
Timeout time.Duration `mapstructure:"timeout" validate:"required,gt=0"`
|
||||
|
||||
MaxUploadBytes int64 `mapstructure:"max_upload_bytes" validate:"required,gt=0,lte=10485760"`
|
||||
MaxImagePixels int64 `mapstructure:"max_image_pixels" validate:"required,gt=0,lte=40000000"`
|
||||
PresignTTL time.Duration `mapstructure:"presign_ttl" validate:"required,gt=0"`
|
||||
SweepInterval time.Duration `mapstructure:"sweep_interval" validate:"required,gt=0"`
|
||||
SweepGrace time.Duration `mapstructure:"sweep_grace" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
@@ -72,6 +93,7 @@ type Server struct {
|
||||
Host string `mapstructure:"host" validate:"required,hostname|ip"`
|
||||
Port int `mapstructure:"port" validate:"required,min=1,max=65535"`
|
||||
ReadHeaderTimeout time.Duration `mapstructure:"read_header_timeout" validate:"required,gt=0"`
|
||||
ReadTimeout time.Duration `mapstructure:"read_timeout" validate:"required,gt=0"`
|
||||
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout" validate:"required,gt=0"`
|
||||
// MaxBodyBytes bounds a request body before the transport decodes it; uploads
|
||||
// of a later lab raise it explicitly (contract 4: 10 MiB attachments).
|
||||
@@ -123,8 +145,22 @@ var defaults = map[string]any{
|
||||
"server.host": "0.0.0.0",
|
||||
"server.port": 8080,
|
||||
"server.read_header_timeout": "10s",
|
||||
"server.read_timeout": "60s",
|
||||
"server.shutdown_timeout": "10s",
|
||||
"server.max_body_bytes": 262144,
|
||||
"s3.endpoint": "http://localhost:8333",
|
||||
"s3.public_endpoint": "http://localhost:8333",
|
||||
"s3.region": "us-east-1",
|
||||
"s3.bucket": "messenger-attachments",
|
||||
"s3.access_key_id": "",
|
||||
"s3.secret_access_key": "",
|
||||
"s3.use_path_style": true,
|
||||
"s3.timeout": "30s",
|
||||
"s3.max_upload_bytes": 10485760,
|
||||
"s3.max_image_pixels": 40000000,
|
||||
"s3.presign_ttl": "5m",
|
||||
"s3.sweep_interval": "1m",
|
||||
"s3.sweep_grace": "10m",
|
||||
"log.level": "info",
|
||||
"log.format": "json",
|
||||
"session.ttl_seconds": 3600,
|
||||
@@ -161,6 +197,17 @@ func Load() (Config, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// The skeleton suggests MAX_UPLOAD_BYTES / MAX_IMAGE_PIXELS. Keep those
|
||||
// names while storing the values under the S3 section.
|
||||
for key, env := range map[string]string{
|
||||
"s3.max_upload_bytes": "MAX_UPLOAD_BYTES",
|
||||
"s3.max_image_pixels": "MAX_IMAGE_PIXELS",
|
||||
} {
|
||||
if err := v.BindEnv(key, env); err != nil {
|
||||
return Config{}, fmt.Errorf("bind env %q: %w", env, err)
|
||||
}
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return Config{}, fmt.Errorf("unmarshal config: %w", err)
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// AttachmentStatus is the lifecycle of an attachment. Only ready attachments
|
||||
// are exposed to members; pending rows exist while the object is being written,
|
||||
// failed rows are reserved for later labs.
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentPending AttachmentStatus = "pending"
|
||||
AttachmentReady AttachmentStatus = "ready"
|
||||
AttachmentFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
func (s AttachmentStatus) Valid() bool {
|
||||
switch s {
|
||||
case AttachmentPending, AttachmentReady, AttachmentFailed:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Attachment describes the original object. ObjectKey is internal: it never
|
||||
// leaves the service and must not be mapped into an API response.
|
||||
type Attachment struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
Status AttachmentStatus
|
||||
ErrorCode *string
|
||||
ObjectKey string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// AttachmentDraft is a fully validated attachment awaiting persistence.
|
||||
type AttachmentDraft struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
}
|
||||
|
||||
// AttachmentState is the minimal projection used while validating message
|
||||
// attachments in a transaction.
|
||||
type AttachmentState struct {
|
||||
ID uuid.UUID
|
||||
Status AttachmentStatus
|
||||
}
|
||||
|
||||
// AttachmentKey is the minimal projection used by the orphan sweeper.
|
||||
type AttachmentKey struct {
|
||||
ID uuid.UUID
|
||||
ObjectKey string
|
||||
Status AttachmentStatus
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// PresignRequest is the validated client intent for a direct upload.
|
||||
type PresignRequest struct {
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
}
|
||||
|
||||
// PresignResult is returned by the server and never contains credentials.
|
||||
type PresignResult struct {
|
||||
AttachmentID uuid.UUID
|
||||
UploadURL string
|
||||
ExpiresAt time.Time
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
}
|
||||
@@ -3,13 +3,16 @@ package domain
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrUsernameTaken = errors.New("username already taken")
|
||||
ErrUnknownMember = errors.New("member does not exist")
|
||||
ErrValidation = errors.New("validation failed")
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrIdempotency = errors.New("idempotency key already used with another payload")
|
||||
ErrUnauthenticated = errors.New("unauthenticated")
|
||||
ErrUnavailable = errors.New("temporarily unavailable")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrUsernameTaken = errors.New("username already taken")
|
||||
ErrUnknownMember = errors.New("member does not exist")
|
||||
ErrValidation = errors.New("validation failed")
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrIdempotency = errors.New("idempotency key already used with another payload")
|
||||
ErrUnauthenticated = errors.New("unauthenticated")
|
||||
ErrUnavailable = errors.New("temporarily unavailable")
|
||||
ErrTooLarge = errors.New("payload is too large")
|
||||
ErrUnsupportedMedia = errors.New("unsupported media type")
|
||||
ErrIncomplete = errors.New("object is not fully uploaded")
|
||||
)
|
||||
|
||||
+37
-11
@@ -3,17 +3,22 @@ package domain
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// MaxMessageAttachments is the contract limit for attachment_ids on a message.
|
||||
const MaxMessageAttachments = 10
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// MessageIdempotency is the client Idempotency-Key together with the fingerprint
|
||||
@@ -25,15 +30,36 @@ type MessageIdempotency struct {
|
||||
}
|
||||
|
||||
type MessageDraft struct {
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
Idempotency MessageIdempotency
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
Idempotency MessageIdempotency
|
||||
}
|
||||
|
||||
// Validate enforces the contract rule that a message carries either non-blank
|
||||
// text or at least one attachment, never neither.
|
||||
func (d MessageDraft) Validate() error {
|
||||
if err := validateNotBlank("text", d.Text); err != nil {
|
||||
return err
|
||||
if strings.TrimSpace(d.Text) == "" && len(d.AttachmentIDs) == 0 {
|
||||
return fmt.Errorf("message has neither text nor attachments: %w", ErrValidation)
|
||||
}
|
||||
|
||||
if len(d.AttachmentIDs) > MaxMessageAttachments {
|
||||
return fmt.Errorf("too many attachments: %w", ErrValidation)
|
||||
}
|
||||
|
||||
seen := make(map[uuid.UUID]struct{}, len(d.AttachmentIDs))
|
||||
|
||||
for _, id := range d.AttachmentIDs {
|
||||
if id == uuid.Nil {
|
||||
return fmt.Errorf("attachment id is empty: %w", ErrValidation)
|
||||
}
|
||||
|
||||
if _, ok := seen[id]; ok {
|
||||
return fmt.Errorf("duplicate attachment id %s: %w", id, ErrValidation)
|
||||
}
|
||||
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
|
||||
if d.Idempotency.Key == "" {
|
||||
|
||||
@@ -0,0 +1,365 @@
|
||||
// Package imagecheck validates PNG and JPEG structure without decoding the
|
||||
// raster. A full decode of a 40-megapixel image would allocate hundreds of
|
||||
// megabytes; attachment uploads must stay bounded. The parser verifies chunk
|
||||
// CRCs and the JPEG marker/scan structure, which catches truncation and
|
||||
// corruption while using constant memory.
|
||||
package imagecheck
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnsupported = errors.New("unsupported image format")
|
||||
ErrCorrupt = errors.New("corrupt image")
|
||||
ErrTooLarge = errors.New("image has too many pixels")
|
||||
)
|
||||
|
||||
const (
|
||||
pngSignature = "\x89PNG\r\n\x1a\n"
|
||||
maxChunkSize = 1 << 30
|
||||
)
|
||||
|
||||
// Info is the validated header. ContentType is one of image/png or image/jpeg.
|
||||
type Info struct {
|
||||
ContentType string
|
||||
Width int64
|
||||
Height int64
|
||||
}
|
||||
|
||||
func ValidateFile(path string, maxPixels int64) (Info, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return Info{}, fmt.Errorf("open upload: %w", err)
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
return Validate(file, maxPixels)
|
||||
}
|
||||
|
||||
// Validate consumes the reader to EOF (so a caller hashing the stream gets all
|
||||
// bytes) and returns the detected format and dimensions.
|
||||
func Validate(r io.Reader, maxPixels int64) (Info, error) {
|
||||
reader := bufio.NewReaderSize(r, 64<<10)
|
||||
|
||||
header, err := reader.Peek(8)
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
return Info{}, fmt.Errorf("%w: image is empty or truncated", ErrUnsupported)
|
||||
}
|
||||
|
||||
return Info{}, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case string(header[:8]) == pngSignature:
|
||||
return validatePNG(reader, maxPixels)
|
||||
case header[0] == 0xFF && header[1] == 0xD8 && header[2] == 0xFF:
|
||||
return validateJPEG(reader, maxPixels)
|
||||
default:
|
||||
return Info{}, fmt.Errorf("%w: unrecognised signature", ErrUnsupported)
|
||||
}
|
||||
}
|
||||
|
||||
func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
if _, err := reader.Discard(len(pngSignature)); err != nil {
|
||||
return Info{}, corrupt("png signature", err)
|
||||
}
|
||||
|
||||
var (
|
||||
info Info
|
||||
seen = make(map[string]bool, 4)
|
||||
first = true
|
||||
hasIDAT bool
|
||||
)
|
||||
|
||||
info.ContentType = "image/png"
|
||||
|
||||
for {
|
||||
var lengthBytes [4]byte
|
||||
if _, err := io.ReadFull(reader, lengthBytes[:]); err != nil {
|
||||
return Info{}, corrupt("png chunk length", err)
|
||||
}
|
||||
|
||||
length := int64(binary.BigEndian.Uint32(lengthBytes[:]))
|
||||
if length < 0 || length > maxChunkSize {
|
||||
return Info{}, corruptf("png chunk length %d", length)
|
||||
}
|
||||
|
||||
var typeBytes [4]byte
|
||||
if _, err := io.ReadFull(reader, typeBytes[:]); err != nil {
|
||||
return Info{}, corrupt("png chunk type", err)
|
||||
}
|
||||
|
||||
chunkType := string(typeBytes[:])
|
||||
checksum := crc32.NewIEEE()
|
||||
_, _ = checksum.Write(typeBytes[:])
|
||||
|
||||
if first && chunkType != "IHDR" {
|
||||
return Info{}, corruptf("png first chunk is %q, want IHDR", chunkType)
|
||||
}
|
||||
|
||||
first = false
|
||||
|
||||
if err := checkCriticalChunk(chunkType, seen); err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
|
||||
data, err := readChunkData(reader, checksum, length)
|
||||
if err != nil {
|
||||
return Info{}, corrupt("png chunk data", err)
|
||||
}
|
||||
|
||||
var crcBytes [4]byte
|
||||
if _, err := io.ReadFull(reader, crcBytes[:]); err != nil {
|
||||
return Info{}, corrupt("png chunk crc", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(crcBytes[:]) != checksum.Sum32() {
|
||||
return Info{}, corruptf("png chunk %q crc mismatch", chunkType)
|
||||
}
|
||||
|
||||
switch chunkType {
|
||||
case "IHDR":
|
||||
if length != 13 {
|
||||
return Info{}, corruptf("png IHDR length %d", length)
|
||||
}
|
||||
|
||||
info.Width = int64(binary.BigEndian.Uint32(data[0:4]))
|
||||
info.Height = int64(binary.BigEndian.Uint32(data[4:8]))
|
||||
|
||||
if info.Width <= 0 || info.Height <= 0 {
|
||||
return Info{}, corruptf("png dimensions %dx%d", info.Width, info.Height)
|
||||
}
|
||||
|
||||
// Short-circuit before multiplying: a malicious IHDR can contain
|
||||
// 32-bit dimensions whose product overflows int64.
|
||||
if maxPixels > 0 && (info.Width > maxPixels || info.Height > maxPixels || info.Width*info.Height > maxPixels) {
|
||||
return Info{}, fmt.Errorf("%w: %dx%d", ErrTooLarge, info.Width, info.Height)
|
||||
}
|
||||
case "IDAT":
|
||||
hasIDAT = true
|
||||
case "IEND":
|
||||
if length != 0 {
|
||||
return Info{}, corruptf("png IEND length %d", length)
|
||||
}
|
||||
|
||||
if !hasIDAT {
|
||||
return Info{}, corruptf("png has no IDAT chunk")
|
||||
}
|
||||
|
||||
if _, err := io.Copy(io.Discard, reader); err != nil {
|
||||
return Info{}, corrupt("png trailing data", err)
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkCriticalChunk(chunkType string, seen map[string]bool) error {
|
||||
if chunkType == "IHDR" || chunkType == "PLTE" || chunkType == "IDAT" || chunkType == "IEND" {
|
||||
seen[chunkType] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
if chunkType[0] >= 'A' && chunkType[0] <= 'Z' {
|
||||
return corruptf("png unknown critical chunk %q", chunkType)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readChunkData(reader io.Reader, checksum io.Writer, length int64) ([]byte, error) {
|
||||
if length <= 64<<10 {
|
||||
data := make([]byte, length)
|
||||
if _, err := io.ReadFull(reader, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, _ = checksum.Write(data)
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
if _, err := io.CopyN(checksum, reader, length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func validateJPEG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
if _, err := reader.Discard(2); err != nil {
|
||||
return Info{}, corrupt("jpeg SOI", err)
|
||||
}
|
||||
|
||||
var (
|
||||
info Info
|
||||
hasSOF bool
|
||||
hasSOS bool
|
||||
pending byte
|
||||
markerErr error
|
||||
)
|
||||
|
||||
info.ContentType = "image/jpeg"
|
||||
|
||||
for {
|
||||
marker := pending
|
||||
pending = 0
|
||||
|
||||
if marker == 0 {
|
||||
marker, markerErr = nextMarker(reader)
|
||||
if markerErr != nil {
|
||||
return Info{}, corrupt("jpeg marker", markerErr)
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case marker == 0xD9: // EOI
|
||||
if !hasSOF || !hasSOS {
|
||||
return Info{}, corruptf("jpeg missing SOF or SOS")
|
||||
}
|
||||
|
||||
if _, err := io.Copy(io.Discard, reader); err != nil {
|
||||
return Info{}, corrupt("jpeg trailing data", err)
|
||||
}
|
||||
|
||||
return info, nil
|
||||
case isStandalone(marker):
|
||||
continue
|
||||
}
|
||||
|
||||
length, err := readUint16(reader)
|
||||
if err != nil {
|
||||
return Info{}, corrupt("jpeg segment length", err)
|
||||
}
|
||||
|
||||
if length < 2 {
|
||||
return Info{}, corruptf("jpeg segment %02x length %d", marker, length)
|
||||
}
|
||||
|
||||
data := make([]byte, int(length)-2)
|
||||
if _, err := io.ReadFull(reader, data); err != nil {
|
||||
return Info{}, corrupt("jpeg segment data", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case isSOF(marker):
|
||||
if len(data) < 5 {
|
||||
return Info{}, corruptf("jpeg SOF %02x is too short", marker)
|
||||
}
|
||||
|
||||
info.Height = int64(binary.BigEndian.Uint16(data[1:3]))
|
||||
info.Width = int64(binary.BigEndian.Uint16(data[3:5]))
|
||||
|
||||
if info.Width <= 0 || info.Height <= 0 {
|
||||
return Info{}, corruptf("jpeg dimensions %dx%d", info.Width, info.Height)
|
||||
}
|
||||
|
||||
hasSOF = true
|
||||
|
||||
if maxPixels > 0 && info.Width*info.Height > maxPixels {
|
||||
return Info{}, fmt.Errorf("%w: %dx%d", ErrTooLarge, info.Width, info.Height)
|
||||
}
|
||||
case marker == 0xDA: // SOS
|
||||
hasSOS = true
|
||||
|
||||
next, err := scanEntropy(reader)
|
||||
if err != nil {
|
||||
return Info{}, corrupt("jpeg scan", err)
|
||||
}
|
||||
|
||||
pending = next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nextMarker(reader *bufio.Reader) (byte, error) {
|
||||
for {
|
||||
value, err := reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if value != 0xFF {
|
||||
return 0, fmt.Errorf("unexpected byte %02x before marker", value)
|
||||
}
|
||||
|
||||
value, err = reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if value == 0xFF {
|
||||
continue
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
// scanEntropy consumes entropy-coded data after SOS and returns the next
|
||||
// non-RST marker (0xFF prefix consumed).
|
||||
func scanEntropy(reader *bufio.Reader) (byte, error) {
|
||||
for {
|
||||
value, err := reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if value != 0xFF {
|
||||
continue
|
||||
}
|
||||
|
||||
value, err = reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case value == 0x00:
|
||||
continue
|
||||
case value >= 0xD0 && value <= 0xD7:
|
||||
continue
|
||||
default:
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readUint16(reader io.Reader) (uint16, error) {
|
||||
var raw [2]byte
|
||||
if _, err := io.ReadFull(reader, raw[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return binary.BigEndian.Uint16(raw[:]), nil
|
||||
}
|
||||
|
||||
func isSOF(marker byte) bool {
|
||||
switch marker {
|
||||
case 0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isStandalone(marker byte) bool {
|
||||
return marker == 0x01 || (marker >= 0xD0 && marker <= 0xD7)
|
||||
}
|
||||
|
||||
func corrupt(context string, err error) error {
|
||||
return fmt.Errorf("%w: %s: %w", ErrCorrupt, context, err)
|
||||
}
|
||||
|
||||
func corruptf(format string, args ...any) error {
|
||||
return fmt.Errorf("%w: %s", ErrCorrupt, fmt.Sprintf(format, args...))
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package imagecheck_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/imagecheck"
|
||||
)
|
||||
|
||||
func encodePNG(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
|
||||
source := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for x := range width {
|
||||
for y := range height {
|
||||
source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255})
|
||||
}
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
require.NoError(t, png.Encode(&buffer, source))
|
||||
|
||||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
func encodeJPEG(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
|
||||
source := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for x := range width {
|
||||
for y := range height {
|
||||
source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 64, A: 255})
|
||||
}
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
require.NoError(t, jpeg.Encode(&buffer, source, &jpeg.Options{Quality: 90}))
|
||||
|
||||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
func TestValidatePNG(t *testing.T) {
|
||||
info, err := imagecheck.Validate(bytes.NewReader(encodePNG(t, 32, 20)), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "image/png", info.ContentType)
|
||||
require.Equal(t, int64(32), info.Width)
|
||||
require.Equal(t, int64(20), info.Height)
|
||||
}
|
||||
|
||||
func TestValidateJPEG(t *testing.T) {
|
||||
info, err := imagecheck.Validate(bytes.NewReader(encodeJPEG(t, 24, 16)), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "image/jpeg", info.ContentType)
|
||||
require.Equal(t, int64(24), info.Width)
|
||||
require.Equal(t, int64(16), info.Height)
|
||||
}
|
||||
|
||||
func TestValidateRejectsUnsupported(t *testing.T) {
|
||||
_, err := imagecheck.Validate(bytes.NewReader([]byte("not an image at all")), 40_000_000)
|
||||
require.ErrorIs(t, err, imagecheck.ErrUnsupported)
|
||||
|
||||
_, err = imagecheck.Validate(bytes.NewReader(nil), 40_000_000)
|
||||
require.ErrorIs(t, err, imagecheck.ErrUnsupported)
|
||||
}
|
||||
|
||||
func TestValidateRejectsTruncatedPNG(t *testing.T) {
|
||||
data := encodePNG(t, 64, 64)
|
||||
_, err := imagecheck.Validate(bytes.NewReader(data[:len(data)/2]), 40_000_000)
|
||||
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
|
||||
}
|
||||
|
||||
func TestValidateRejectsPNGCRCMismatch(t *testing.T) {
|
||||
data := encodePNG(t, 64, 64)
|
||||
index := bytes.Index(data, []byte("IDAT"))
|
||||
require.Positive(t, index)
|
||||
data[index+8] ^= 0xFF // first IDAT payload byte; CRC no longer matches
|
||||
|
||||
_, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000)
|
||||
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
|
||||
}
|
||||
|
||||
func TestValidateRejectsTruncatedJPEG(t *testing.T) {
|
||||
data := encodeJPEG(t, 64, 64)
|
||||
_, err := imagecheck.Validate(bytes.NewReader(data[:len(data)-32]), 40_000_000)
|
||||
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
|
||||
}
|
||||
|
||||
func TestValidateRejectsTooManyPixels(t *testing.T) {
|
||||
_, err := imagecheck.Validate(bytes.NewReader(encodePNG(t, 100, 100)), 9_999)
|
||||
require.ErrorIs(t, err, imagecheck.ErrTooLarge)
|
||||
|
||||
_, err = imagecheck.Validate(bytes.NewReader(encodeJPEG(t, 100, 100)), 9_999)
|
||||
require.ErrorIs(t, err, imagecheck.ErrTooLarge)
|
||||
}
|
||||
|
||||
func TestValidateAllowsTrailingBytes(t *testing.T) {
|
||||
pngData := append(encodePNG(t, 8, 8), []byte("trailing")...)
|
||||
_, err := imagecheck.Validate(bytes.NewReader(pngData), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
|
||||
jpegData := append(encodeJPEG(t, 8, 8), []byte("trailing")...)
|
||||
_, err = imagecheck.Validate(bytes.NewReader(jpegData), 40_000_000)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Package objectstore defines the S3-compatible port used by attachments.
|
||||
// Metadata never belongs here: PostgreSQL is the source of truth, this package
|
||||
// only moves bytes and exposes object timestamps for the orphan sweeper.
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Object is the provider-neutral view of one stored object.
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
ContentType string
|
||||
LastModified time.Time
|
||||
Metadata map[string]string
|
||||
}
|
||||
|
||||
// Store is implemented by S3-compatible backends. Put requires a seekable body
|
||||
// because AWS SigV4 may need to rewind the payload; the proxy upload path always
|
||||
// passes an *os.File.
|
||||
type Store interface {
|
||||
Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string, metadata map[string]string) error
|
||||
Open(ctx context.Context, key string) (io.ReadCloser, Object, error)
|
||||
Head(ctx context.Context, key string) (Object, error)
|
||||
Copy(ctx context.Context, src, dst, contentType string, metadata map[string]string) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
List(ctx context.Context, prefix string) ([]Object, error)
|
||||
PresignPut(ctx context.Context, key string, ttl time.Duration) (string, time.Time, error)
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/aws/smithy-go"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
// Config is the S3 connection subset. Limits belong to the attachment service.
|
||||
type Config struct {
|
||||
Endpoint string
|
||||
PublicEndpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
AccessKeyID string
|
||||
SecretAccessKey string
|
||||
UsePathStyle bool
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// S3 is an object store backed by an S3-compatible server (SeaweedFS in the
|
||||
// lab stand, MinIO/AWS in other deployments).
|
||||
type S3 struct {
|
||||
bucket string
|
||||
internal *s3.Client
|
||||
public *s3.Client
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
var _ Store = (*S3)(nil)
|
||||
|
||||
func New(cfg Config) (*S3, error) {
|
||||
if cfg.Endpoint == "" || cfg.Region == "" || cfg.Bucket == "" {
|
||||
return nil, errors.New("object store: endpoint, region and bucket are required")
|
||||
}
|
||||
|
||||
if cfg.AccessKeyID == "" || cfg.SecretAccessKey == "" {
|
||||
return nil, errors.New("object store: access key id and secret access key are required")
|
||||
}
|
||||
|
||||
if cfg.PublicEndpoint == "" {
|
||||
cfg.PublicEndpoint = cfg.Endpoint
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
TLSHandshakeTimeout: 3 * time.Second,
|
||||
ResponseHeaderTimeout: 15 * time.Second,
|
||||
MaxIdleConnsPerHost: 16,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
},
|
||||
// Downloads stream through the response body; an overall client
|
||||
// timeout would abort them.
|
||||
}
|
||||
|
||||
newClient := func(endpoint string) *s3.Client {
|
||||
return s3.New(s3.Options{
|
||||
Region: cfg.Region,
|
||||
BaseEndpoint: aws.String(endpoint),
|
||||
UsePathStyle: cfg.UsePathStyle,
|
||||
Credentials: credentials.NewStaticCredentialsProvider(cfg.AccessKeyID, cfg.SecretAccessKey, ""),
|
||||
HTTPClient: httpClient,
|
||||
})
|
||||
}
|
||||
|
||||
st := &S3{
|
||||
bucket: cfg.Bucket,
|
||||
internal: newClient(cfg.Endpoint),
|
||||
public: newClient(cfg.PublicEndpoint),
|
||||
timeout: cfg.Timeout,
|
||||
}
|
||||
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func (s *S3) Put(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
body io.ReadSeeker,
|
||||
size int64,
|
||||
contentType string,
|
||||
metadata map[string]string,
|
||||
) error {
|
||||
ctx, cancel := s.operationContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.internal.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
Body: body,
|
||||
ContentLength: aws.Int64(size),
|
||||
ContentType: aws.String(contentType),
|
||||
Metadata: metadata,
|
||||
})
|
||||
if err != nil {
|
||||
return mapObjectError("put object", key, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) Open(ctx context.Context, key string) (io.ReadCloser, Object, error) {
|
||||
output, err := s.internal.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, Object{}, mapObjectError("open object", key, err)
|
||||
}
|
||||
|
||||
object := Object{
|
||||
Key: key,
|
||||
Size: aws.ToInt64(output.ContentLength),
|
||||
ContentType: aws.ToString(output.ContentType),
|
||||
LastModified: aws.ToTime(output.LastModified),
|
||||
Metadata: output.Metadata,
|
||||
}
|
||||
|
||||
return output.Body, object, nil
|
||||
}
|
||||
|
||||
func (s *S3) Head(ctx context.Context, key string) (Object, error) {
|
||||
output, err := s.internal.HeadObject(ctx, &s3.HeadObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
return Object{}, mapObjectError("head object", key, err)
|
||||
}
|
||||
|
||||
return Object{
|
||||
Key: key,
|
||||
Size: aws.ToInt64(output.ContentLength),
|
||||
ContentType: aws.ToString(output.ContentType),
|
||||
LastModified: aws.ToTime(output.LastModified),
|
||||
Metadata: output.Metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *S3) Copy(ctx context.Context, src, dst, contentType string, metadata map[string]string) error {
|
||||
ctx, cancel := s.operationContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.internal.CopyObject(ctx, &s3.CopyObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(dst),
|
||||
CopySource: aws.String(s.bucket + "/" + src),
|
||||
MetadataDirective: types.MetadataDirectiveReplace,
|
||||
ContentType: aws.String(contentType),
|
||||
Metadata: metadata,
|
||||
})
|
||||
if err != nil {
|
||||
return mapObjectError("copy object", src+" -> "+dst, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) Delete(ctx context.Context, key string) error {
|
||||
ctx, cancel := s.operationContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.internal.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
return mapObjectError("delete object", key, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) List(ctx context.Context, prefix string) ([]Object, error) {
|
||||
ctx, cancel := s.operationContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
objects []Object
|
||||
token *string
|
||||
)
|
||||
|
||||
for {
|
||||
output, err := s.internal.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Prefix: aws.String(prefix),
|
||||
ContinuationToken: token,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, mapObjectError("list objects", prefix, err)
|
||||
}
|
||||
|
||||
for _, item := range output.Contents {
|
||||
objects = append(objects, Object{
|
||||
Key: aws.ToString(item.Key),
|
||||
Size: aws.ToInt64(item.Size),
|
||||
LastModified: aws.ToTime(item.LastModified),
|
||||
})
|
||||
}
|
||||
|
||||
if !aws.ToBool(output.IsTruncated) {
|
||||
break
|
||||
}
|
||||
|
||||
token = output.NextContinuationToken
|
||||
if token == nil || *token == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
// PresignPut signs only the bucket, key and host. Deliberately no Content-Type
|
||||
// or checksum: adding them to the signed headers forces every client to send
|
||||
// exactly those headers, and SeaweedFS does not validate checksums anyway.
|
||||
// Completion verifies the actual bytes instead.
|
||||
func (s *S3) PresignPut(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
ttl time.Duration,
|
||||
) (string, time.Time, error) {
|
||||
expiresAt := time.Now().UTC().Add(ttl)
|
||||
|
||||
presigned, err := s3.NewPresignClient(s.public).PresignPutObject(
|
||||
ctx,
|
||||
&s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
},
|
||||
func(options *s3.PresignOptions) {
|
||||
options.Expires = ttl
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", time.Time{}, mapObjectError("presign put", key, err)
|
||||
}
|
||||
|
||||
return presigned.URL, expiresAt, nil
|
||||
}
|
||||
|
||||
func (s *S3) operationContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if s.timeout <= 0 {
|
||||
return context.WithCancel(ctx)
|
||||
}
|
||||
|
||||
return context.WithTimeout(ctx, s.timeout)
|
||||
}
|
||||
|
||||
func mapObjectError(operation, subject string, err error) error {
|
||||
var apiErr smithy.APIError
|
||||
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "NotFound", "NoSuchKey", "NoSuchBucket":
|
||||
return fmt.Errorf("%s %q: %w", operation, subject, domain.ErrNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return fmt.Errorf("%s %q: %w", operation, subject, err)
|
||||
}
|
||||
|
||||
if strings.Contains(strings.ToLower(err.Error()), "no such key") {
|
||||
return fmt.Errorf("%s %q: %w", operation, subject, domain.ErrNotFound)
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: %s %q: %w", domain.ErrUnavailable, operation, subject, err)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
// Code generated by mockery; DO NOT EDIT.
|
||||
// github.com/vektra/mockery
|
||||
// template: testify
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NewMockLockStore creates a new instance of MockLockStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockLockStore(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockLockStore {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
|
||||
mock := &MockLockStore{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
mock.AssertExpectations(t)
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockLockStore is an autogenerated mock type for the LockStore type
|
||||
type MockLockStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockLockStore_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockLockStore) EXPECT() *MockLockStore_Expecter {
|
||||
return &MockLockStore_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// AcquireSweepLock provides a mock function for the type MockLockStore
|
||||
func (_mock *MockLockStore) AcquireSweepLock(ctx context.Context) (func(), bool, error) {
|
||||
ret := _mock.Called(ctx)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for AcquireSweepLock")
|
||||
}
|
||||
|
||||
var r0 func()
|
||||
var r1 bool
|
||||
var r2 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context) (func(), bool, error)); ok {
|
||||
return returnFunc(ctx)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context) func()); ok {
|
||||
r0 = returnFunc(ctx)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(func())
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context) bool); ok {
|
||||
r1 = returnFunc(ctx)
|
||||
} else {
|
||||
r1 = ret.Get(1).(bool)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(2).(func(context.Context) error); ok {
|
||||
r2 = returnFunc(ctx)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// MockLockStore_AcquireSweepLock_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'AcquireSweepLock'
|
||||
type MockLockStore_AcquireSweepLock_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// AcquireSweepLock is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
func (_e *MockLockStore_Expecter) AcquireSweepLock(ctx any) *MockLockStore_AcquireSweepLock_Call {
|
||||
return &MockLockStore_AcquireSweepLock_Call{Call: _e.mock.On("AcquireSweepLock", ctx)}
|
||||
}
|
||||
|
||||
func (_c *MockLockStore_AcquireSweepLock_Call) Run(run func(ctx context.Context)) *MockLockStore_AcquireSweepLock_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockLockStore_AcquireSweepLock_Call) Return(release func(), acquired bool, err error) *MockLockStore_AcquireSweepLock_Call {
|
||||
_c.Call.Return(release, acquired, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockLockStore_AcquireSweepLock_Call) RunAndReturn(run func(ctx context.Context) (func(), bool, error)) *MockLockStore_AcquireSweepLock_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
@@ -0,0 +1,683 @@
|
||||
// Code generated by mockery; DO NOT EDIT.
|
||||
// github.com/vektra/mockery
|
||||
// template: testify
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mtusi-messenger/internal/domain"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NewMockRepository creates a new instance of MockRepository. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockRepository(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockRepository {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
|
||||
mock := &MockRepository{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
mock.AssertExpectations(t)
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockRepository is an autogenerated mock type for the Repository type
|
||||
type MockRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockRepository_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockRepository) EXPECT() *MockRepository_Expecter {
|
||||
return &MockRepository_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// Create provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) Create(ctx context.Context, draft domain.AttachmentDraft) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, draft)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Create")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.AttachmentDraft) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, draft)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.AttachmentDraft) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, domain.AttachmentDraft) error); ok {
|
||||
r1 = returnFunc(ctx, draft)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create'
|
||||
type MockRepository_Create_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Create is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - draft domain.AttachmentDraft
|
||||
func (_e *MockRepository_Expecter) Create(ctx any, draft any) *MockRepository_Create_Call {
|
||||
return &MockRepository_Create_Call{Call: _e.mock.On("Create", ctx, draft)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Create_Call) Run(run func(ctx context.Context, draft domain.AttachmentDraft)) *MockRepository_Create_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 domain.AttachmentDraft
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(domain.AttachmentDraft)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Create_Call) Return(attachment domain.Attachment, err error) *MockRepository_Create_Call {
|
||||
_c.Call.Return(attachment, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_Create_Call) RunAndReturn(run func(ctx context.Context, draft domain.AttachmentDraft) (domain.Attachment, error)) *MockRepository_Create_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// DeletePending provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) DeletePending(ctx context.Context, id uuid.UUID) error {
|
||||
ret := _mock.Called(ctx, id)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for DeletePending")
|
||||
}
|
||||
|
||||
var r0 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID) error); ok {
|
||||
r0 = returnFunc(ctx, id)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
return r0
|
||||
}
|
||||
|
||||
// MockRepository_DeletePending_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeletePending'
|
||||
type MockRepository_DeletePending_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// DeletePending is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - id uuid.UUID
|
||||
func (_e *MockRepository_Expecter) DeletePending(ctx any, id any) *MockRepository_DeletePending_Call {
|
||||
return &MockRepository_DeletePending_Call{Call: _e.mock.On("DeletePending", ctx, id)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_DeletePending_Call) Run(run func(ctx context.Context, id uuid.UUID)) *MockRepository_DeletePending_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_DeletePending_Call) Return(err error) *MockRepository_DeletePending_Call {
|
||||
_c.Call.Return(err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_DeletePending_Call) RunAndReturn(run func(ctx context.Context, id uuid.UUID) error) *MockRepository_DeletePending_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// GetForMember provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) GetForMember(ctx context.Context, userID uuid.UUID, id uuid.UUID) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, userID, id)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetForMember")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, userID, id)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, userID, id)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, userID, id)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_GetForMember_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetForMember'
|
||||
type MockRepository_GetForMember_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// GetForMember is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - userID uuid.UUID
|
||||
// - id uuid.UUID
|
||||
func (_e *MockRepository_Expecter) GetForMember(ctx any, userID any, id any) *MockRepository_GetForMember_Call {
|
||||
return &MockRepository_GetForMember_Call{Call: _e.mock.On("GetForMember", ctx, userID, id)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_GetForMember_Call) Run(run func(ctx context.Context, userID uuid.UUID, id uuid.UUID)) *MockRepository_GetForMember_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_GetForMember_Call) Return(attachment domain.Attachment, err error) *MockRepository_GetForMember_Call {
|
||||
_c.Call.Return(attachment, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_GetForMember_Call) RunAndReturn(run func(ctx context.Context, userID uuid.UUID, id uuid.UUID) (domain.Attachment, error)) *MockRepository_GetForMember_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// GetForOwner provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) GetForOwner(ctx context.Context, ownerID uuid.UUID, id uuid.UUID) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, ownerID, id)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetForOwner")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, ownerID, id)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, ownerID, id)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, ownerID, id)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_GetForOwner_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetForOwner'
|
||||
type MockRepository_GetForOwner_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// GetForOwner is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - ownerID uuid.UUID
|
||||
// - id uuid.UUID
|
||||
func (_e *MockRepository_Expecter) GetForOwner(ctx any, ownerID any, id any) *MockRepository_GetForOwner_Call {
|
||||
return &MockRepository_GetForOwner_Call{Call: _e.mock.On("GetForOwner", ctx, ownerID, id)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_GetForOwner_Call) Run(run func(ctx context.Context, ownerID uuid.UUID, id uuid.UUID)) *MockRepository_GetForOwner_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_GetForOwner_Call) Return(attachment domain.Attachment, err error) *MockRepository_GetForOwner_Call {
|
||||
_c.Call.Return(attachment, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_GetForOwner_Call) RunAndReturn(run func(ctx context.Context, ownerID uuid.UUID, id uuid.UUID) (domain.Attachment, error)) *MockRepository_GetForOwner_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// IsChatMember provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) IsChatMember(ctx context.Context, chatID uuid.UUID, userID uuid.UUID) (bool, error) {
|
||||
ret := _mock.Called(ctx, chatID, userID)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for IsChatMember")
|
||||
}
|
||||
|
||||
var r0 bool
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (bool, error)); ok {
|
||||
return returnFunc(ctx, chatID, userID)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) bool); ok {
|
||||
r0 = returnFunc(ctx, chatID, userID)
|
||||
} else {
|
||||
r0 = ret.Get(0).(bool)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, chatID, userID)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_IsChatMember_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsChatMember'
|
||||
type MockRepository_IsChatMember_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// IsChatMember is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - chatID uuid.UUID
|
||||
// - userID uuid.UUID
|
||||
func (_e *MockRepository_Expecter) IsChatMember(ctx any, chatID any, userID any) *MockRepository_IsChatMember_Call {
|
||||
return &MockRepository_IsChatMember_Call{Call: _e.mock.On("IsChatMember", ctx, chatID, userID)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_IsChatMember_Call) Run(run func(ctx context.Context, chatID uuid.UUID, userID uuid.UUID)) *MockRepository_IsChatMember_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_IsChatMember_Call) Return(b bool, err error) *MockRepository_IsChatMember_Call {
|
||||
_c.Call.Return(b, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_IsChatMember_Call) RunAndReturn(run func(ctx context.Context, chatID uuid.UUID, userID uuid.UUID) (bool, error)) *MockRepository_IsChatMember_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// ListKeys provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) ListKeys(ctx context.Context, limit int32, offset int32) ([]domain.AttachmentKey, error) {
|
||||
ret := _mock.Called(ctx, limit, offset)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for ListKeys")
|
||||
}
|
||||
|
||||
var r0 []domain.AttachmentKey
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, int32, int32) ([]domain.AttachmentKey, error)); ok {
|
||||
return returnFunc(ctx, limit, offset)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, int32, int32) []domain.AttachmentKey); ok {
|
||||
r0 = returnFunc(ctx, limit, offset)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]domain.AttachmentKey)
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, int32, int32) error); ok {
|
||||
r1 = returnFunc(ctx, limit, offset)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_ListKeys_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListKeys'
|
||||
type MockRepository_ListKeys_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// ListKeys is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - limit int32
|
||||
// - offset int32
|
||||
func (_e *MockRepository_Expecter) ListKeys(ctx any, limit any, offset any) *MockRepository_ListKeys_Call {
|
||||
return &MockRepository_ListKeys_Call{Call: _e.mock.On("ListKeys", ctx, limit, offset)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListKeys_Call) Run(run func(ctx context.Context, limit int32, offset int32)) *MockRepository_ListKeys_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 int32
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(int32)
|
||||
}
|
||||
var arg2 int32
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(int32)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListKeys_Call) Return(attachmentKeys []domain.AttachmentKey, err error) *MockRepository_ListKeys_Call {
|
||||
_c.Call.Return(attachmentKeys, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListKeys_Call) RunAndReturn(run func(ctx context.Context, limit int32, offset int32) ([]domain.AttachmentKey, error)) *MockRepository_ListKeys_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// ListStalePending provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) ListStalePending(ctx context.Context, createdBefore time.Time, limit int32) ([]domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, createdBefore, limit)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for ListStalePending")
|
||||
}
|
||||
|
||||
var r0 []domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, time.Time, int32) ([]domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, createdBefore, limit)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, time.Time, int32) []domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, createdBefore, limit)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]domain.Attachment)
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, time.Time, int32) error); ok {
|
||||
r1 = returnFunc(ctx, createdBefore, limit)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_ListStalePending_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListStalePending'
|
||||
type MockRepository_ListStalePending_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// ListStalePending is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - createdBefore time.Time
|
||||
// - limit int32
|
||||
func (_e *MockRepository_Expecter) ListStalePending(ctx any, createdBefore any, limit any) *MockRepository_ListStalePending_Call {
|
||||
return &MockRepository_ListStalePending_Call{Call: _e.mock.On("ListStalePending", ctx, createdBefore, limit)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListStalePending_Call) Run(run func(ctx context.Context, createdBefore time.Time, limit int32)) *MockRepository_ListStalePending_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 time.Time
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(time.Time)
|
||||
}
|
||||
var arg2 int32
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(int32)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListStalePending_Call) Return(attachments []domain.Attachment, err error) *MockRepository_ListStalePending_Call {
|
||||
_c.Call.Return(attachments, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListStalePending_Call) RunAndReturn(run func(ctx context.Context, createdBefore time.Time, limit int32) ([]domain.Attachment, error)) *MockRepository_ListStalePending_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// ListStates provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) ListStates(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error) {
|
||||
ret := _mock.Called(ctx, chatID, ids)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for ListStates")
|
||||
}
|
||||
|
||||
var r0 []domain.AttachmentState
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, []uuid.UUID) ([]domain.AttachmentState, error)); ok {
|
||||
return returnFunc(ctx, chatID, ids)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, []uuid.UUID) []domain.AttachmentState); ok {
|
||||
r0 = returnFunc(ctx, chatID, ids)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]domain.AttachmentState)
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, []uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, chatID, ids)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_ListStates_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListStates'
|
||||
type MockRepository_ListStates_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// ListStates is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - chatID uuid.UUID
|
||||
// - ids []uuid.UUID
|
||||
func (_e *MockRepository_Expecter) ListStates(ctx any, chatID any, ids any) *MockRepository_ListStates_Call {
|
||||
return &MockRepository_ListStates_Call{Call: _e.mock.On("ListStates", ctx, chatID, ids)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListStates_Call) Run(run func(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID)) *MockRepository_ListStates_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 []uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].([]uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListStates_Call) Return(attachmentStates []domain.AttachmentState, err error) *MockRepository_ListStates_Call {
|
||||
_c.Call.Return(attachmentStates, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_ListStates_Call) RunAndReturn(run func(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error)) *MockRepository_ListStates_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// MarkReady provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) MarkReady(ctx context.Context, id uuid.UUID, objectKey string) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, id, objectKey)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for MarkReady")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, string) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, id, objectKey)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, string) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, id, objectKey)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, string) error); ok {
|
||||
r1 = returnFunc(ctx, id, objectKey)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_MarkReady_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'MarkReady'
|
||||
type MockRepository_MarkReady_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// MarkReady is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - id uuid.UUID
|
||||
// - objectKey string
|
||||
func (_e *MockRepository_Expecter) MarkReady(ctx any, id any, objectKey any) *MockRepository_MarkReady_Call {
|
||||
return &MockRepository_MarkReady_Call{Call: _e.mock.On("MarkReady", ctx, id, objectKey)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_MarkReady_Call) Run(run func(ctx context.Context, id uuid.UUID, objectKey string)) *MockRepository_MarkReady_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 string
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(string)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_MarkReady_Call) Return(attachment domain.Attachment, err error) *MockRepository_MarkReady_Call {
|
||||
_c.Call.Return(attachment, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_MarkReady_Call) RunAndReturn(run func(ctx context.Context, id uuid.UUID, objectKey string) (domain.Attachment, error)) *MockRepository_MarkReady_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
@@ -0,0 +1,487 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/imagecheck"
|
||||
"mtusi-messenger/internal/platform/objectstore"
|
||||
)
|
||||
|
||||
const (
|
||||
contentTypePNG = "image/png"
|
||||
contentTypeJPEG = "image/jpeg"
|
||||
|
||||
objectPrefixAttachments = "attachments/"
|
||||
objectPrefixStaging = "staging/"
|
||||
objectPrefixPresign = "staging/presign/"
|
||||
)
|
||||
|
||||
var sha256Pattern = regexp.MustCompile(`^[0-9a-f]{64}$`)
|
||||
|
||||
// Repository is the persistence port. It is consumer-side so the service can be
|
||||
// tested without PostgreSQL.
|
||||
type Repository interface {
|
||||
IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error)
|
||||
Create(ctx context.Context, draft domain.AttachmentDraft) (domain.Attachment, error)
|
||||
MarkReady(ctx context.Context, id uuid.UUID, objectKey string) (domain.Attachment, error)
|
||||
DeletePending(ctx context.Context, id uuid.UUID) error
|
||||
GetForMember(ctx context.Context, userID, id uuid.UUID) (domain.Attachment, error)
|
||||
GetForOwner(ctx context.Context, ownerID, id uuid.UUID) (domain.Attachment, error)
|
||||
ListStates(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error)
|
||||
ListStalePending(ctx context.Context, createdBefore time.Time, limit int32) ([]domain.Attachment, error)
|
||||
ListKeys(ctx context.Context, limit, offset int32) ([]domain.AttachmentKey, error)
|
||||
}
|
||||
|
||||
// Config carries the attachment limits. They are validated by config.Load.
|
||||
type Config struct {
|
||||
MaxUploadBytes int64
|
||||
MaxImagePixels int64
|
||||
PresignTTL time.Duration
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
store objectstore.Store
|
||||
config Config
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewService(repo Repository, store objectstore.Store, config Config, log *slog.Logger) *Service {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
|
||||
return &Service{repo: repo, store: store, config: config, log: log}
|
||||
}
|
||||
|
||||
// UploadInput is one raw-body upload. DeclaredContentType comes from the HTTP
|
||||
// Content-Type header and is cross-checked against the detected format.
|
||||
type UploadInput struct {
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
Filename string
|
||||
DeclaredContentType string
|
||||
Body io.Reader
|
||||
}
|
||||
|
||||
func (s *Service) Upload(ctx context.Context, input UploadInput) (domain.Attachment, error) {
|
||||
if err := ValidateFilename(input.Filename); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
member, err := s.repo.IsChatMember(ctx, input.ChatID, input.OwnerID)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
if !member {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: chat is not visible", domain.ErrNotFound)
|
||||
}
|
||||
|
||||
file, size, sum, err := s.spool(input.Body)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
defer func() { _ = file.Close() }()
|
||||
defer func() { _ = os.Remove(file.Name()) }()
|
||||
|
||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("rewind upload: %w", err)
|
||||
}
|
||||
|
||||
info, err := imagecheck.Validate(file, s.config.MaxImagePixels)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, mapImageError(err)
|
||||
}
|
||||
|
||||
if info.ContentType != input.DeclaredContentType {
|
||||
return domain.Attachment{}, fmt.Errorf(
|
||||
"%w: declared %q, detected %q",
|
||||
domain.ErrUnsupportedMedia, input.DeclaredContentType, info.ContentType,
|
||||
)
|
||||
}
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("generate attachment id: %w", err)
|
||||
}
|
||||
|
||||
objectKey := objectPrefixAttachments + input.ChatID.String() + "/" + id.String()
|
||||
|
||||
if _, err := s.repo.Create(ctx, domain.AttachmentDraft{
|
||||
ID: id,
|
||||
ChatID: input.ChatID,
|
||||
OwnerID: input.OwnerID,
|
||||
ObjectKey: objectKey,
|
||||
Filename: input.Filename,
|
||||
ContentType: info.ContentType,
|
||||
SizeBytes: size,
|
||||
SHA256: sum,
|
||||
}); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("rewind upload: %w", err)
|
||||
}
|
||||
|
||||
if err := s.store.Put(ctx, objectKey, file, size, info.ContentType, map[string]string{"sha256": sum}); err != nil {
|
||||
s.cleanupPending(ctx, id, "")
|
||||
|
||||
return domain.Attachment{}, unavailable("store attachment", err)
|
||||
}
|
||||
|
||||
ready, err := s.repo.MarkReady(ctx, id, objectKey)
|
||||
if err != nil {
|
||||
// The object and the pending row remain; the sweeper either finalises
|
||||
// the row or removes both after the grace period. The client must not
|
||||
// receive 201 until metadata is committed.
|
||||
return domain.Attachment{}, unavailable("commit attachment", err)
|
||||
}
|
||||
|
||||
return ready, nil
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, error) {
|
||||
attachment, err := s.repo.GetForMember(ctx, callerID, id)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
// Contract 1.4.0 declares Attachment.status as the single value "ready".
|
||||
// Pending/failed rows therefore cannot be represented in a 200 response;
|
||||
// they are reported as 409 until lab5 extends the enum.
|
||||
if attachment.Status != domain.AttachmentReady {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: attachment is %s", domain.ErrConflict, attachment.Status)
|
||||
}
|
||||
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
// Open returns metadata and the original stream. Only ready attachments have
|
||||
// bytes; pending/failed return 409 on the content endpoint.
|
||||
func (s *Service) Open(
|
||||
ctx context.Context,
|
||||
callerID, id uuid.UUID,
|
||||
) (domain.Attachment, io.ReadCloser, error) {
|
||||
attachment, err := s.repo.GetForMember(ctx, callerID, id)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, nil, err
|
||||
}
|
||||
|
||||
if attachment.Status != domain.AttachmentReady {
|
||||
return domain.Attachment{}, nil, fmt.Errorf("%w: attachment is %s", domain.ErrConflict, attachment.Status)
|
||||
}
|
||||
|
||||
body, _, err := s.store.Open(ctx, attachment.ObjectKey)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, nil, err
|
||||
}
|
||||
|
||||
return attachment, body, nil
|
||||
}
|
||||
|
||||
func (s *Service) Presign(ctx context.Context, request domain.PresignRequest) (domain.PresignResult, error) {
|
||||
if err := ValidateFilename(request.Filename); err != nil {
|
||||
return domain.PresignResult{}, err
|
||||
}
|
||||
|
||||
if request.ContentType != contentTypePNG && request.ContentType != contentTypeJPEG {
|
||||
return domain.PresignResult{}, fmt.Errorf("%w: content type %q", domain.ErrUnsupportedMedia, request.ContentType)
|
||||
}
|
||||
|
||||
if request.SizeBytes < 1 || request.SizeBytes > s.config.MaxUploadBytes {
|
||||
return domain.PresignResult{}, fmt.Errorf("%w: size %d", domain.ErrTooLarge, request.SizeBytes)
|
||||
}
|
||||
|
||||
if !sha256Pattern.MatchString(request.SHA256) {
|
||||
return domain.PresignResult{}, fmt.Errorf("%w: sha256 must be 64 lowercase hex characters", domain.ErrValidation)
|
||||
}
|
||||
|
||||
member, err := s.repo.IsChatMember(ctx, request.ChatID, request.OwnerID)
|
||||
if err != nil {
|
||||
return domain.PresignResult{}, err
|
||||
}
|
||||
|
||||
if !member {
|
||||
return domain.PresignResult{}, fmt.Errorf("%w: chat is not visible", domain.ErrNotFound)
|
||||
}
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return domain.PresignResult{}, fmt.Errorf("generate attachment id: %w", err)
|
||||
}
|
||||
|
||||
stagingKey := objectPrefixPresign + request.ChatID.String() + "/" + id.String()
|
||||
|
||||
if _, err := s.repo.Create(ctx, domain.AttachmentDraft{
|
||||
ID: id,
|
||||
ChatID: request.ChatID,
|
||||
OwnerID: request.OwnerID,
|
||||
ObjectKey: stagingKey,
|
||||
Filename: request.Filename,
|
||||
ContentType: request.ContentType,
|
||||
SizeBytes: request.SizeBytes,
|
||||
SHA256: request.SHA256,
|
||||
}); err != nil {
|
||||
return domain.PresignResult{}, err
|
||||
}
|
||||
|
||||
url, expiresAt, err := s.store.PresignPut(ctx, stagingKey, s.config.PresignTTL)
|
||||
if err != nil {
|
||||
s.cleanupPending(ctx, id, "")
|
||||
|
||||
return domain.PresignResult{}, unavailable("presign attachment", err)
|
||||
}
|
||||
|
||||
return domain.PresignResult{
|
||||
AttachmentID: id,
|
||||
UploadURL: url,
|
||||
ExpiresAt: expiresAt,
|
||||
SizeBytes: request.SizeBytes,
|
||||
SHA256: request.SHA256,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Complete(
|
||||
ctx context.Context,
|
||||
ownerID, id uuid.UUID,
|
||||
requestSHA string,
|
||||
) (domain.Attachment, error) {
|
||||
if !sha256Pattern.MatchString(requestSHA) {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: sha256 must be 64 lowercase hex characters", domain.ErrValidation)
|
||||
}
|
||||
|
||||
attachment, err := s.repo.GetForOwner(ctx, ownerID, id)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
switch attachment.Status {
|
||||
case domain.AttachmentReady:
|
||||
if attachment.SHA256 != requestSHA {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: attachment already completed with another checksum", domain.ErrConflict)
|
||||
}
|
||||
|
||||
return attachment, nil
|
||||
case domain.AttachmentFailed:
|
||||
return domain.Attachment{}, fmt.Errorf("%w: attachment is failed", domain.ErrConflict)
|
||||
case domain.AttachmentPending:
|
||||
default:
|
||||
return domain.Attachment{}, fmt.Errorf("%w: unknown attachment status", domain.ErrConflict)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(attachment.ObjectKey, objectPrefixPresign) {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: attachment is not a presigned upload", domain.ErrConflict)
|
||||
}
|
||||
|
||||
head, err := s.store.Head(ctx, attachment.ObjectKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: object was not uploaded", domain.ErrIncomplete)
|
||||
}
|
||||
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
if head.Size != attachment.SizeBytes {
|
||||
s.cleanupPending(ctx, id, attachment.ObjectKey)
|
||||
|
||||
return domain.Attachment{}, fmt.Errorf(
|
||||
"%w: uploaded %d bytes, declared %d",
|
||||
domain.ErrValidation, head.Size, attachment.SizeBytes,
|
||||
)
|
||||
}
|
||||
|
||||
body, _, err := s.store.Open(ctx, attachment.ObjectKey)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
|
||||
hasher := sha256.New()
|
||||
info, validateErr := imagecheck.Validate(io.TeeReader(body, hasher), s.config.MaxImagePixels)
|
||||
closeErr := body.Close()
|
||||
|
||||
if validateErr != nil {
|
||||
s.cleanupPending(ctx, id, attachment.ObjectKey)
|
||||
|
||||
return domain.Attachment{}, mapImageError(validateErr)
|
||||
}
|
||||
|
||||
if closeErr != nil {
|
||||
return domain.Attachment{}, unavailable("read uploaded object", closeErr)
|
||||
}
|
||||
|
||||
if info.ContentType != attachment.ContentType {
|
||||
s.cleanupPending(ctx, id, attachment.ObjectKey)
|
||||
|
||||
return domain.Attachment{}, fmt.Errorf(
|
||||
"%w: declared %q, detected %q",
|
||||
domain.ErrUnsupportedMedia, attachment.ContentType, info.ContentType,
|
||||
)
|
||||
}
|
||||
|
||||
sum := hex.EncodeToString(hasher.Sum(nil))
|
||||
if sum != attachment.SHA256 {
|
||||
s.cleanupPending(ctx, id, attachment.ObjectKey)
|
||||
|
||||
return domain.Attachment{}, fmt.Errorf("%w: uploaded object checksum mismatch", domain.ErrValidation)
|
||||
}
|
||||
|
||||
if requestSHA != attachment.SHA256 {
|
||||
s.cleanupPending(ctx, id, attachment.ObjectKey)
|
||||
|
||||
return domain.Attachment{}, fmt.Errorf("%w: completion checksum mismatch", domain.ErrConflict)
|
||||
}
|
||||
|
||||
finalKey := objectPrefixAttachments + attachment.ChatID.String() + "/" + id.String()
|
||||
|
||||
if err := s.store.Copy(ctx, attachment.ObjectKey, finalKey, attachment.ContentType, map[string]string{"sha256": sum}); err != nil {
|
||||
return domain.Attachment{}, unavailable("publish attachment", err)
|
||||
}
|
||||
|
||||
ready, err := s.repo.MarkReady(ctx, id, finalKey)
|
||||
if err != nil {
|
||||
// A concurrent completion may have won. Re-read instead of reporting a
|
||||
// spurious failure.
|
||||
current, getErr := s.repo.GetForOwner(ctx, ownerID, id)
|
||||
if getErr == nil && current.Status == domain.AttachmentReady && current.SHA256 == requestSHA {
|
||||
return current, nil
|
||||
}
|
||||
|
||||
return domain.Attachment{}, unavailable("commit attachment", err)
|
||||
}
|
||||
|
||||
if err := s.store.Delete(ctx, attachment.ObjectKey); err != nil {
|
||||
// The final object is already committed; a stale staging object is an
|
||||
// orphan and the sweeper removes it after the grace period.
|
||||
s.log.WarnContext(ctx, "staging object cleanup failed",
|
||||
slog.String("attachment_id", id.String()),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
}
|
||||
|
||||
return ready, nil
|
||||
}
|
||||
|
||||
func (s *Service) spool(body io.Reader) (*os.File, int64, string, error) {
|
||||
file, err := os.CreateTemp("", "lab4-upload-*")
|
||||
if err != nil {
|
||||
return nil, 0, "", fmt.Errorf("create upload spool: %w", err)
|
||||
}
|
||||
|
||||
cleanup := func() {
|
||||
_ = file.Close()
|
||||
_ = os.Remove(file.Name())
|
||||
}
|
||||
|
||||
hasher := sha256.New()
|
||||
limited := io.LimitReader(body, s.config.MaxUploadBytes+1)
|
||||
buffer := make([]byte, 32<<10)
|
||||
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hasher), limited, buffer)
|
||||
if err != nil {
|
||||
cleanup()
|
||||
|
||||
var maxBytes *http.MaxBytesError
|
||||
if errors.As(err, &maxBytes) {
|
||||
return nil, 0, "", fmt.Errorf("%w: request body exceeds %d bytes", domain.ErrTooLarge, s.config.MaxUploadBytes)
|
||||
}
|
||||
|
||||
return nil, 0, "", fmt.Errorf("%w: read upload: %w", domain.ErrValidation, err)
|
||||
}
|
||||
|
||||
if size > s.config.MaxUploadBytes {
|
||||
cleanup()
|
||||
|
||||
return nil, 0, "", fmt.Errorf("%w: upload exceeds %d bytes", domain.ErrTooLarge, s.config.MaxUploadBytes)
|
||||
}
|
||||
|
||||
if size == 0 {
|
||||
cleanup()
|
||||
|
||||
return nil, 0, "", fmt.Errorf("%w: empty image", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
|
||||
return file, size, hex.EncodeToString(hasher.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func (s *Service) cleanupPending(ctx context.Context, id uuid.UUID, objectKey string) {
|
||||
if objectKey != "" {
|
||||
if err := s.store.Delete(ctx, objectKey); err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||
s.log.WarnContext(ctx, "object cleanup failed",
|
||||
slog.String("attachment_id", id.String()),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.repo.DeletePending(ctx, id); err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||
s.log.WarnContext(ctx, "pending attachment cleanup failed",
|
||||
slog.String("attachment_id", id.String()),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateFilename implements the contract rule: 1-255 Unicode code points,
|
||||
// no path separators, NUL or control characters. The name is display-only; the
|
||||
// S3 key is always generated.
|
||||
func ValidateFilename(name string) error {
|
||||
if !utf8.ValidString(name) {
|
||||
return fmt.Errorf("%w: filename is not valid UTF-8", domain.ErrValidation)
|
||||
}
|
||||
|
||||
length := utf8.RuneCountInString(name)
|
||||
if length < 1 || length > 255 {
|
||||
return fmt.Errorf("%w: filename length is %d, want 1-255 code points", domain.ErrValidation, length)
|
||||
}
|
||||
|
||||
if strings.ContainsAny(name, "/\\\x00") {
|
||||
return fmt.Errorf("%w: filename contains a path separator or NUL", domain.ErrValidation)
|
||||
}
|
||||
|
||||
for _, r := range name {
|
||||
if unicode.IsControl(r) {
|
||||
return fmt.Errorf("%w: filename contains a control character", domain.ErrValidation)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapImageError(err error) error {
|
||||
switch {
|
||||
case errors.Is(err, imagecheck.ErrTooLarge):
|
||||
return fmt.Errorf("%w: %w", domain.ErrTooLarge, err)
|
||||
case errors.Is(err, imagecheck.ErrUnsupported), errors.Is(err, imagecheck.ErrCorrupt):
|
||||
return fmt.Errorf("%w: %w", domain.ErrUnsupportedMedia, err)
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func unavailable(operation string, err error) error {
|
||||
if errors.Is(err, domain.ErrUnavailable) || errors.Is(err, domain.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: %s: %w", domain.ErrUnavailable, operation, err)
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
package attachment_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/platform/objectstore"
|
||||
"mtusi-messenger/internal/service/attachment"
|
||||
"mtusi-messenger/internal/service/attachment/mocks"
|
||||
)
|
||||
|
||||
type storedObject struct {
|
||||
data []byte
|
||||
contentType string
|
||||
metadata map[string]string
|
||||
lastModified time.Time
|
||||
}
|
||||
|
||||
type fakeStore struct {
|
||||
objects map[string]storedObject
|
||||
}
|
||||
|
||||
func newFakeStore() *fakeStore {
|
||||
return &fakeStore{objects: make(map[string]storedObject)}
|
||||
}
|
||||
|
||||
func (f *fakeStore) Put(
|
||||
_ context.Context,
|
||||
key string,
|
||||
body io.ReadSeeker,
|
||||
_ int64,
|
||||
contentType string,
|
||||
metadata map[string]string,
|
||||
) error {
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.objects[key] = storedObject{
|
||||
data: data,
|
||||
contentType: contentType,
|
||||
metadata: metadata,
|
||||
lastModified: time.Now().UTC(),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Open(_ context.Context, key string) (io.ReadCloser, objectstore.Object, error) {
|
||||
object, ok := f.objects[key]
|
||||
if !ok {
|
||||
return nil, objectstore.Object{}, domain.ErrNotFound
|
||||
}
|
||||
|
||||
return io.NopCloser(bytes.NewReader(object.data)), f.object(key, object), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Head(_ context.Context, key string) (objectstore.Object, error) {
|
||||
object, ok := f.objects[key]
|
||||
if !ok {
|
||||
return objectstore.Object{}, domain.ErrNotFound
|
||||
}
|
||||
|
||||
return f.object(key, object), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Copy(_ context.Context, src, dst, contentType string, metadata map[string]string) error {
|
||||
object, ok := f.objects[src]
|
||||
if !ok {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
|
||||
f.objects[dst] = storedObject{
|
||||
data: append([]byte(nil), object.data...),
|
||||
contentType: contentType,
|
||||
metadata: metadata,
|
||||
lastModified: time.Now().UTC(),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Delete(_ context.Context, key string) error {
|
||||
delete(f.objects, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) List(_ context.Context, prefix string) ([]objectstore.Object, error) {
|
||||
objects := make([]objectstore.Object, 0, len(f.objects))
|
||||
for key, object := range f.objects {
|
||||
if strings.HasPrefix(key, prefix) {
|
||||
objects = append(objects, f.object(key, object))
|
||||
}
|
||||
}
|
||||
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) PresignPut(_ context.Context, key string, ttl time.Duration) (string, time.Time, error) {
|
||||
return "http://s3.example/" + key, time.Now().UTC().Add(ttl), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) object(key string, object storedObject) objectstore.Object {
|
||||
return objectstore.Object{
|
||||
Key: key,
|
||||
Size: int64(len(object.data)),
|
||||
ContentType: object.contentType,
|
||||
LastModified: object.lastModified,
|
||||
Metadata: object.metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func pngBytes(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
|
||||
source := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
for x := range width {
|
||||
for y := range height {
|
||||
source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 200, A: 255})
|
||||
}
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
require.NoError(t, png.Encode(&buffer, source))
|
||||
|
||||
return buffer.Bytes()
|
||||
}
|
||||
|
||||
func serviceConfig() attachment.Config {
|
||||
return attachment.Config{
|
||||
MaxUploadBytes: 1 << 20,
|
||||
MaxImagePixels: 1_000_000,
|
||||
PresignTTL: 5 * time.Minute,
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateFilename(t *testing.T) {
|
||||
valid := []string{"photo.png", "фото.png", "a b.jpeg", strings.Repeat("я", 255)}
|
||||
for _, name := range valid {
|
||||
require.NoError(t, attachment.ValidateFilename(name), name)
|
||||
}
|
||||
|
||||
invalid := []string{"", strings.Repeat("x", 256), "a/b.png", `a\b.png`, "a\x00b.png", "a\nb.png"}
|
||||
for _, name := range invalid {
|
||||
require.ErrorIs(t, attachment.ValidateFilename(name), domain.ErrValidation, name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadStoresOriginal(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
|
||||
chatID := uuid.New()
|
||||
ownerID := uuid.New()
|
||||
content := pngBytes(t, 16, 16)
|
||||
sum := sha256.Sum256(content)
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, chatID, ownerID).Return(true, nil).Once()
|
||||
repo.EXPECT().Create(mock.Anything, mock.MatchedBy(func(draft domain.AttachmentDraft) bool {
|
||||
return draft.ChatID == chatID && draft.OwnerID == ownerID &&
|
||||
draft.ContentType == "image/png" && draft.SizeBytes == int64(len(content)) &&
|
||||
draft.SHA256 == hex.EncodeToString(sum[:]) && strings.HasPrefix(draft.ObjectKey, "attachments/")
|
||||
})).Return(domain.Attachment{Status: domain.AttachmentPending}, nil).Once()
|
||||
repo.EXPECT().MarkReady(mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{Status: domain.AttachmentReady, ContentType: "image/png", SizeBytes: int64(len(content))}, nil).
|
||||
Once()
|
||||
|
||||
result, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: chatID,
|
||||
OwnerID: ownerID,
|
||||
Filename: "photo.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: bytes.NewReader(content),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, domain.AttachmentReady, result.Status)
|
||||
require.Len(t, store.objects, 1)
|
||||
}
|
||||
|
||||
func TestUploadRejectsTooLargeBody(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), attachment.Config{
|
||||
MaxUploadBytes: 8,
|
||||
MaxImagePixels: 1_000_000,
|
||||
PresignTTL: time.Minute,
|
||||
}, slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
|
||||
_, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: uuid.New(),
|
||||
OwnerID: uuid.New(),
|
||||
Filename: "photo.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: bytes.NewReader(bytes.Repeat([]byte("x"), 9)),
|
||||
})
|
||||
|
||||
require.ErrorIs(t, err, domain.ErrTooLarge)
|
||||
}
|
||||
|
||||
func TestUploadRejectsMIMEMismatch(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().IsChatMember(mock.Anything, mock.Anything, mock.Anything).Return(true, nil).Once()
|
||||
|
||||
_, err := service.Upload(t.Context(), attachment.UploadInput{
|
||||
ChatID: uuid.New(),
|
||||
OwnerID: uuid.New(),
|
||||
Filename: "photo.jpg",
|
||||
DeclaredContentType: "image/jpeg",
|
||||
Body: bytes.NewReader(pngBytes(t, 8, 8)),
|
||||
})
|
||||
|
||||
require.ErrorIs(t, err, domain.ErrUnsupportedMedia)
|
||||
}
|
||||
|
||||
func TestGetRejectsNonReadyAttachment(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
service := attachment.NewService(repo, newFakeStore(), serviceConfig(), slog.Default())
|
||||
|
||||
repo.EXPECT().GetForMember(mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(domain.Attachment{Status: domain.AttachmentPending}, nil).Once()
|
||||
|
||||
_, err := service.Get(t.Context(), uuid.New(), uuid.New())
|
||||
require.ErrorIs(t, err, domain.ErrConflict)
|
||||
}
|
||||
|
||||
func TestCompletePublishesPresignedObject(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
|
||||
id := uuid.New()
|
||||
chatID := uuid.New()
|
||||
ownerID := uuid.New()
|
||||
content := pngBytes(t, 10, 10)
|
||||
sum := sha256.Sum256(content)
|
||||
digest := hex.EncodeToString(sum[:])
|
||||
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
|
||||
|
||||
store.objects[stagingKey] = storedObject{
|
||||
data: content,
|
||||
contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest},
|
||||
lastModified: time.Now().UTC(),
|
||||
}
|
||||
|
||||
pending := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)), SHA256: digest,
|
||||
Status: domain.AttachmentPending,
|
||||
}
|
||||
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
|
||||
repo.EXPECT().MarkReady(mock.Anything, id, mock.MatchedBy(func(key string) bool {
|
||||
return strings.HasPrefix(key, "attachments/")
|
||||
})).Return(domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ContentType: "image/png",
|
||||
SizeBytes: int64(len(content)), SHA256: digest, Status: domain.AttachmentReady,
|
||||
}, nil).Once()
|
||||
|
||||
completed, err := service.Complete(t.Context(), ownerID, id, digest)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, domain.AttachmentReady, completed.Status)
|
||||
require.NotContains(t, store.objects, stagingKey)
|
||||
require.Len(t, store.objects, 1)
|
||||
}
|
||||
|
||||
func TestCompleteRejectsChecksumMismatch(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
store := newFakeStore()
|
||||
service := attachment.NewService(repo, store, serviceConfig(), slog.Default())
|
||||
|
||||
id := uuid.New()
|
||||
chatID := uuid.New()
|
||||
ownerID := uuid.New()
|
||||
content := pngBytes(t, 10, 10)
|
||||
sum := sha256.Sum256(content)
|
||||
digest := hex.EncodeToString(sum[:])
|
||||
stagingKey := "staging/presign/" + chatID.String() + "/" + id.String()
|
||||
|
||||
store.objects[stagingKey] = storedObject{
|
||||
data: content,
|
||||
contentType: "image/png",
|
||||
metadata: map[string]string{"sha256": digest},
|
||||
}
|
||||
|
||||
pending := domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: stagingKey,
|
||||
ContentType: "image/png", SizeBytes: int64(len(content)) + 1, SHA256: digest,
|
||||
Status: domain.AttachmentPending,
|
||||
}
|
||||
|
||||
repo.EXPECT().GetForOwner(mock.Anything, ownerID, id).Return(pending, nil).Once()
|
||||
repo.EXPECT().DeletePending(mock.Anything, id).Return(nil).Once()
|
||||
|
||||
_, err := service.Complete(t.Context(), ownerID, id, digest)
|
||||
require.ErrorIs(t, err, domain.ErrValidation)
|
||||
require.NotContains(t, store.objects, stagingKey)
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package attachment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/platform/objectstore"
|
||||
)
|
||||
|
||||
// LockStore serialises sweepers across API replicas.
|
||||
type LockStore interface {
|
||||
AcquireSweepLock(ctx context.Context) (release func(), acquired bool, err error)
|
||||
}
|
||||
|
||||
type SweepStats struct {
|
||||
Recovered int
|
||||
RowsDeleted int
|
||||
ObjectsDeleted int
|
||||
}
|
||||
|
||||
type Sweeper struct {
|
||||
repo Repository
|
||||
store objectstore.Store
|
||||
locker LockStore
|
||||
grace time.Duration
|
||||
interval time.Duration
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewSweeper(
|
||||
repo Repository,
|
||||
store objectstore.Store,
|
||||
locker LockStore,
|
||||
grace, interval time.Duration,
|
||||
log *slog.Logger,
|
||||
) *Sweeper {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
|
||||
return &Sweeper{
|
||||
repo: repo,
|
||||
store: store,
|
||||
locker: locker,
|
||||
grace: grace,
|
||||
interval: interval,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
// Run sweeps immediately, then on every interval until the context is done.
|
||||
func (s *Sweeper) Run(ctx context.Context) {
|
||||
s.sweep(ctx)
|
||||
|
||||
ticker := time.NewTicker(s.interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
s.sweep(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sweeper) sweep(ctx context.Context) {
|
||||
stats, err := s.SweepOnce(ctx)
|
||||
if err != nil {
|
||||
s.log.WarnContext(ctx, "attachment sweep failed", slog.Any("error", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if stats.Recovered+stats.RowsDeleted+stats.ObjectsDeleted > 0 {
|
||||
s.log.InfoContext(ctx, "attachment sweep finished",
|
||||
slog.Int("recovered", stats.Recovered),
|
||||
slog.Int("rows_deleted", stats.RowsDeleted),
|
||||
slog.Int("objects_deleted", stats.ObjectsDeleted),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// SweepOnce reconciles PostgreSQL metadata with S3 objects. It never deletes a
|
||||
// ready row or an object referenced by any row. Only objects older than the
|
||||
// grace period are candidates, so an in-flight upload cannot be collected.
|
||||
func (s *Sweeper) SweepOnce(ctx context.Context) (SweepStats, error) {
|
||||
release, acquired, err := s.locker.AcquireSweepLock(ctx)
|
||||
if err != nil {
|
||||
return SweepStats{}, err
|
||||
}
|
||||
|
||||
if !acquired {
|
||||
return SweepStats{}, nil
|
||||
}
|
||||
|
||||
defer release()
|
||||
|
||||
before := time.Now().UTC().Add(-s.grace)
|
||||
|
||||
var stats SweepStats
|
||||
|
||||
stale, err := s.repo.ListStalePending(ctx, before, 100)
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
|
||||
for _, row := range stale {
|
||||
if row.Status != domain.AttachmentPending {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(row.ObjectKey, objectPrefixPresign) {
|
||||
if s.deleteObject(ctx, row.ObjectKey) {
|
||||
stats.ObjectsDeleted++
|
||||
}
|
||||
|
||||
if err := s.repo.DeletePending(ctx, row.ID); err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||
return stats, err
|
||||
}
|
||||
|
||||
stats.RowsDeleted++
|
||||
continue
|
||||
}
|
||||
|
||||
// A proxy upload writes directly to its final key. If the process died
|
||||
// between PutObject and MarkReady, the object is complete and can be
|
||||
// recovered instead of discarded.
|
||||
head, err := s.store.Head(ctx, row.ObjectKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
if err := s.repo.DeletePending(ctx, row.ID); err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||
return stats, err
|
||||
}
|
||||
|
||||
stats.RowsDeleted++
|
||||
continue
|
||||
}
|
||||
|
||||
s.log.WarnContext(ctx, "sweep head failed",
|
||||
slog.String("attachment_id", row.ID.String()),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if head.Size == row.SizeBytes && head.Metadata["sha256"] == row.SHA256 {
|
||||
if _, err := s.repo.MarkReady(ctx, row.ID, row.ObjectKey); err == nil {
|
||||
stats.Recovered++
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if s.deleteObject(ctx, row.ObjectKey) {
|
||||
stats.ObjectsDeleted++
|
||||
}
|
||||
|
||||
if err := s.repo.DeletePending(ctx, row.ID); err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||
return stats, err
|
||||
}
|
||||
|
||||
stats.RowsDeleted++
|
||||
}
|
||||
|
||||
referenced, err := s.referencedKeys(ctx)
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
|
||||
for _, prefix := range []string{objectPrefixAttachments, objectPrefixStaging} {
|
||||
objects, err := s.store.List(ctx, prefix)
|
||||
if err != nil {
|
||||
return stats, err
|
||||
}
|
||||
|
||||
for _, object := range objects {
|
||||
if object.LastModified.IsZero() || !object.LastModified.Before(before) {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := referenced[object.Key]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if s.deleteObject(ctx, object.Key) {
|
||||
stats.ObjectsDeleted++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (s *Sweeper) referencedKeys(ctx context.Context) (map[string]struct{}, error) {
|
||||
const pageSize = 1000
|
||||
|
||||
keys := make(map[string]struct{})
|
||||
|
||||
for offset := int32(0); ; offset += pageSize {
|
||||
rows, err := s.repo.ListKeys(ctx, pageSize, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
keys[row.ObjectKey] = struct{}{}
|
||||
}
|
||||
|
||||
if len(rows) < pageSize {
|
||||
return keys, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sweeper) deleteObject(ctx context.Context, key string) bool {
|
||||
err := s.store.Delete(ctx, key)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if !errors.Is(err, domain.ErrNotFound) {
|
||||
s.log.WarnContext(ctx, "sweep delete failed",
|
||||
slog.String("object_key", key),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package attachment_test
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/service/attachment"
|
||||
"mtusi-messenger/internal/service/attachment/mocks"
|
||||
)
|
||||
|
||||
func TestSweepReconcilesPendingAndOrphanObjects(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
locker := mocks.NewMockLockStore(t)
|
||||
store := newFakeStore()
|
||||
|
||||
stale := time.Now().UTC().Add(-time.Hour)
|
||||
|
||||
presign := domain.Attachment{
|
||||
ID: uuid.New(), ObjectKey: "staging/presign/chat/" + uuid.NewString(),
|
||||
Status: domain.AttachmentPending, CreatedAt: stale,
|
||||
}
|
||||
proxyRecoverable := domain.Attachment{
|
||||
ID: uuid.New(), ObjectKey: "attachments/chat/" + uuid.NewString(),
|
||||
SizeBytes: 3, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
Status: domain.AttachmentPending, CreatedAt: stale,
|
||||
}
|
||||
proxyMissing := domain.Attachment{
|
||||
ID: uuid.New(), ObjectKey: "attachments/chat/" + uuid.NewString(),
|
||||
Status: domain.AttachmentPending, CreatedAt: stale,
|
||||
}
|
||||
readyKey := "attachments/chat/ready"
|
||||
|
||||
store.objects[presign.ObjectKey] = storedObject{data: []byte("x"), lastModified: stale}
|
||||
store.objects[proxyRecoverable.ObjectKey] = storedObject{
|
||||
data: []byte("abc"),
|
||||
lastModified: stale,
|
||||
metadata: map[string]string{"sha256": proxyRecoverable.SHA256},
|
||||
}
|
||||
store.objects[readyKey] = storedObject{data: []byte("ready"), lastModified: stale}
|
||||
store.objects["attachments/chat/orphan"] = storedObject{data: []byte("orphan"), lastModified: stale}
|
||||
store.objects["staging/abandoned"] = storedObject{data: []byte("stale"), lastModified: stale}
|
||||
store.objects["staging/fresh"] = storedObject{data: []byte("fresh"), lastModified: time.Now().UTC()}
|
||||
|
||||
repo.EXPECT().ListStalePending(mock.Anything, mock.Anything, int32(100)).
|
||||
Return([]domain.Attachment{presign, proxyRecoverable, proxyMissing}, nil).Once()
|
||||
repo.EXPECT().DeletePending(mock.Anything, presign.ID).Return(nil).Once()
|
||||
repo.EXPECT().MarkReady(mock.Anything, proxyRecoverable.ID, proxyRecoverable.ObjectKey).
|
||||
Return(domain.Attachment{ID: proxyRecoverable.ID, Status: domain.AttachmentReady}, nil).Once()
|
||||
repo.EXPECT().DeletePending(mock.Anything, proxyMissing.ID).Return(nil).Once()
|
||||
repo.EXPECT().ListKeys(mock.Anything, int32(1000), int32(0)).
|
||||
Return([]domain.AttachmentKey{
|
||||
{ID: proxyRecoverable.ID, ObjectKey: proxyRecoverable.ObjectKey, Status: domain.AttachmentReady},
|
||||
{ID: uuid.New(), ObjectKey: readyKey, Status: domain.AttachmentReady},
|
||||
}, nil).Once()
|
||||
locker.EXPECT().AcquireSweepLock(mock.Anything).
|
||||
Return(func() {}, true, nil).Once()
|
||||
|
||||
sweeper := attachment.NewSweeper(repo, store, locker, time.Minute, time.Hour, slog.Default())
|
||||
|
||||
stats, err := sweeper.SweepOnce(t.Context())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, stats.Recovered)
|
||||
require.Equal(t, 2, stats.RowsDeleted)
|
||||
require.Equal(t, 3, stats.ObjectsDeleted)
|
||||
|
||||
require.NotContains(t, store.objects, presign.ObjectKey)
|
||||
require.NotContains(t, store.objects, "attachments/chat/orphan")
|
||||
require.NotContains(t, store.objects, "staging/abandoned")
|
||||
require.Contains(t, store.objects, proxyRecoverable.ObjectKey)
|
||||
require.Contains(t, store.objects, readyKey)
|
||||
require.Contains(t, store.objects, "staging/fresh")
|
||||
}
|
||||
|
||||
func TestSweepSkipsWhenAnotherReplicaHoldsTheLock(t *testing.T) {
|
||||
repo := mocks.NewMockRepository(t)
|
||||
locker := mocks.NewMockLockStore(t)
|
||||
sweeper := attachment.NewSweeper(repo, newFakeStore(), locker, time.Minute, time.Hour, slog.Default())
|
||||
|
||||
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(nil, false, nil).Once()
|
||||
|
||||
stats, err := sweeper.SweepOnce(t.Context())
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, stats)
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
// Code generated by mockery; DO NOT EDIT.
|
||||
// github.com/vektra/mockery
|
||||
// template: testify
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mtusi-messenger/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NewMockAttachmentChecker creates a new instance of MockAttachmentChecker. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockAttachmentChecker(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockAttachmentChecker {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
|
||||
mock := &MockAttachmentChecker{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
mock.AssertExpectations(t)
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockAttachmentChecker is an autogenerated mock type for the AttachmentChecker type
|
||||
type MockAttachmentChecker struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockAttachmentChecker_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockAttachmentChecker) EXPECT() *MockAttachmentChecker_Expecter {
|
||||
return &MockAttachmentChecker_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// ListStates provides a mock function for the type MockAttachmentChecker
|
||||
func (_mock *MockAttachmentChecker) ListStates(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error) {
|
||||
ret := _mock.Called(ctx, chatID, ids)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for ListStates")
|
||||
}
|
||||
|
||||
var r0 []domain.AttachmentState
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, []uuid.UUID) ([]domain.AttachmentState, error)); ok {
|
||||
return returnFunc(ctx, chatID, ids)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, []uuid.UUID) []domain.AttachmentState); ok {
|
||||
r0 = returnFunc(ctx, chatID, ids)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]domain.AttachmentState)
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, []uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, chatID, ids)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockAttachmentChecker_ListStates_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListStates'
|
||||
type MockAttachmentChecker_ListStates_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// ListStates is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - chatID uuid.UUID
|
||||
// - ids []uuid.UUID
|
||||
func (_e *MockAttachmentChecker_Expecter) ListStates(ctx any, chatID any, ids any) *MockAttachmentChecker_ListStates_Call {
|
||||
return &MockAttachmentChecker_ListStates_Call{Call: _e.mock.On("ListStates", ctx, chatID, ids)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentChecker_ListStates_Call) Run(run func(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID)) *MockAttachmentChecker_ListStates_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 []uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].([]uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentChecker_ListStates_Call) Return(attachmentStates []domain.AttachmentState, err error) *MockAttachmentChecker_ListStates_Call {
|
||||
_c.Call.Return(attachmentStates, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentChecker_ListStates_Call) RunAndReturn(run func(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error)) *MockAttachmentChecker_ListStates_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
@@ -114,6 +114,78 @@ func (_c *MockRepository_Create_Call) RunAndReturn(run func(ctx context.Context,
|
||||
return _c
|
||||
}
|
||||
|
||||
// IsChatMember provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) IsChatMember(ctx context.Context, chatID uuid.UUID, userID uuid.UUID) (bool, error) {
|
||||
ret := _mock.Called(ctx, chatID, userID)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for IsChatMember")
|
||||
}
|
||||
|
||||
var r0 bool
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (bool, error)); ok {
|
||||
return returnFunc(ctx, chatID, userID)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) bool); ok {
|
||||
r0 = returnFunc(ctx, chatID, userID)
|
||||
} else {
|
||||
r0 = ret.Get(0).(bool)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, chatID, userID)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_IsChatMember_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'IsChatMember'
|
||||
type MockRepository_IsChatMember_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// IsChatMember is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - chatID uuid.UUID
|
||||
// - userID uuid.UUID
|
||||
func (_e *MockRepository_Expecter) IsChatMember(ctx any, chatID any, userID any) *MockRepository_IsChatMember_Call {
|
||||
return &MockRepository_IsChatMember_Call{Call: _e.mock.On("IsChatMember", ctx, chatID, userID)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_IsChatMember_Call) Run(run func(ctx context.Context, chatID uuid.UUID, userID uuid.UUID)) *MockRepository_IsChatMember_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_IsChatMember_Call) Return(b bool, err error) *MockRepository_IsChatMember_Call {
|
||||
_c.Call.Return(b, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_IsChatMember_Call) RunAndReturn(run func(ctx context.Context, chatID uuid.UUID, userID uuid.UUID) (bool, error)) *MockRepository_IsChatMember_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// ListVisible provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) ListVisible(ctx context.Context, chatID uuid.UUID, callerID uuid.UUID, after *domain.Cursor, limit int) (domain.Page[domain.Message], error) {
|
||||
ret := _mock.Called(ctx, chatID, callerID, after, limit)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error)
|
||||
Create(ctx context.Context, draft domain.MessageDraft) (domain.Message, error)
|
||||
ListVisible(
|
||||
ctx context.Context,
|
||||
@@ -19,12 +20,20 @@ type Repository interface {
|
||||
) (domain.Page[domain.Message], error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
messages Repository
|
||||
// AttachmentChecker validates that requested attachments exist in the chat and
|
||||
// are ready. The contract returns 404 for unknown/foreign IDs and 409 for
|
||||
// attachments that cannot be linked.
|
||||
type AttachmentChecker interface {
|
||||
ListStates(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error)
|
||||
}
|
||||
|
||||
func NewService(messages Repository) *Service {
|
||||
return &Service{messages: messages}
|
||||
type Service struct {
|
||||
messages Repository
|
||||
attachments AttachmentChecker
|
||||
}
|
||||
|
||||
func NewService(messages Repository, attachments AttachmentChecker) *Service {
|
||||
return &Service{messages: messages, attachments: attachments}
|
||||
}
|
||||
|
||||
func (s *Service) Send(ctx context.Context, draft domain.MessageDraft) (domain.Message, error) {
|
||||
@@ -32,6 +41,39 @@ func (s *Service) Send(ctx context.Context, draft domain.MessageDraft) (domain.M
|
||||
return domain.Message{}, err
|
||||
}
|
||||
|
||||
member, err := s.messages.IsChatMember(ctx, draft.ChatID, draft.SenderID)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("check chat membership: %w", err)
|
||||
}
|
||||
|
||||
if !member {
|
||||
return domain.Message{}, fmt.Errorf("%w: chat is not visible", domain.ErrNotFound)
|
||||
}
|
||||
|
||||
if len(draft.AttachmentIDs) > 0 {
|
||||
if s.attachments == nil {
|
||||
return domain.Message{}, fmt.Errorf("attachment checker is not configured: %w", domain.ErrUnavailable)
|
||||
}
|
||||
|
||||
states, err := s.attachments.ListStates(ctx, draft.ChatID, draft.AttachmentIDs)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("check attachments: %w", err)
|
||||
}
|
||||
|
||||
if len(states) != len(draft.AttachmentIDs) {
|
||||
return domain.Message{}, fmt.Errorf("%w: attachment is not visible", domain.ErrNotFound)
|
||||
}
|
||||
|
||||
for _, state := range states {
|
||||
if state.Status != domain.AttachmentReady {
|
||||
return domain.Message{}, fmt.Errorf(
|
||||
"%w: attachment %s is %s",
|
||||
domain.ErrConflict, state.ID, state.Status,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sent, err := s.messages.Create(ctx, draft)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("create message: %w", err)
|
||||
|
||||
@@ -18,11 +18,12 @@ var errRepository = errors.New("repository is unavailable")
|
||||
type ServiceSuite struct {
|
||||
suite.Suite
|
||||
|
||||
messages *mocks.MockRepository
|
||||
service *message.Service
|
||||
caller uuid.UUID
|
||||
chatID uuid.UUID
|
||||
now time.Time
|
||||
messages *mocks.MockRepository
|
||||
attachments *mocks.MockAttachmentChecker
|
||||
service *message.Service
|
||||
caller uuid.UUID
|
||||
chatID uuid.UUID
|
||||
now time.Time
|
||||
}
|
||||
|
||||
func TestServiceSuite(t *testing.T) {
|
||||
@@ -32,7 +33,8 @@ func TestServiceSuite(t *testing.T) {
|
||||
|
||||
func (s *ServiceSuite) SetupTest() {
|
||||
s.messages = mocks.NewMockRepository(s.T())
|
||||
s.service = message.NewService(s.messages)
|
||||
s.attachments = mocks.NewMockAttachmentChecker(s.T())
|
||||
s.service = message.NewService(s.messages, s.attachments)
|
||||
s.caller = uuid.New()
|
||||
s.chatID = uuid.New()
|
||||
s.now = time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC)
|
||||
@@ -57,6 +59,7 @@ func (s *ServiceSuite) TestSendReturnsStoredMessage() {
|
||||
CreatedAt: s.now,
|
||||
}
|
||||
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.messages.EXPECT().Create(s.T().Context(), draft).Return(stored, nil).Once()
|
||||
|
||||
got, err := s.service.Send(s.T().Context(), draft)
|
||||
@@ -76,11 +79,11 @@ func (s *ServiceSuite) TestSendRejectsWhitespaceTextWithoutRepositoryCall() {
|
||||
func (s *ServiceSuite) TestSendPropagatesNotFoundForOutsider() {
|
||||
draft := s.draft()
|
||||
|
||||
s.messages.EXPECT().Create(s.T().Context(), draft).Return(domain.Message{}, domain.ErrNotFound).Once()
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(false, nil).Once()
|
||||
|
||||
_, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrNotFound,
|
||||
"доступ проверяет хранилище атомарно со вставкой")
|
||||
"посторонний не должен узнать о существовании чата")
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestHistoryPassesNilCursorOnFirstPage() {
|
||||
@@ -142,3 +145,80 @@ func (s *ServiceSuite) TestHistoryWrapsRepositoryFailure() {
|
||||
_, err := s.service.History(s.T().Context(), s.caller, s.chatID, nil, domain.DefaultPageLimit)
|
||||
s.Require().ErrorIs(err, errRepository)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendAcceptsReadyAttachment() {
|
||||
attachmentID := uuid.New()
|
||||
draft := s.draft()
|
||||
draft.AttachmentIDs = []uuid.UUID{attachmentID}
|
||||
stored := domain.Message{
|
||||
ID: uuid.New(), ChatID: draft.ChatID, SenderID: draft.SenderID,
|
||||
AttachmentIDs: []uuid.UUID{attachmentID}, CreatedAt: s.now,
|
||||
}
|
||||
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.attachments.EXPECT().
|
||||
ListStates(s.T().Context(), draft.ChatID, []uuid.UUID{attachmentID}).
|
||||
Return([]domain.AttachmentState{{ID: attachmentID, Status: domain.AttachmentReady}}, nil).
|
||||
Once()
|
||||
s.messages.EXPECT().Create(s.T().Context(), draft).Return(stored, nil).Once()
|
||||
|
||||
got, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().NoError(err)
|
||||
s.Equal([]uuid.UUID{attachmentID}, got.AttachmentIDs)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendAllowsAttachmentOnlyMessage() {
|
||||
attachmentID := uuid.New()
|
||||
draft := s.draft()
|
||||
draft.Text = ""
|
||||
draft.AttachmentIDs = []uuid.UUID{attachmentID}
|
||||
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.attachments.EXPECT().
|
||||
ListStates(s.T().Context(), draft.ChatID, []uuid.UUID{attachmentID}).
|
||||
Return([]domain.AttachmentState{{ID: attachmentID, Status: domain.AttachmentReady}}, nil).
|
||||
Once()
|
||||
s.messages.EXPECT().Create(s.T().Context(), draft).Return(domain.Message{AttachmentIDs: draft.AttachmentIDs}, nil).Once()
|
||||
|
||||
_, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendRejectsForeignAttachment() {
|
||||
attachmentID := uuid.New()
|
||||
draft := s.draft()
|
||||
draft.AttachmentIDs = []uuid.UUID{attachmentID}
|
||||
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.attachments.EXPECT().
|
||||
ListStates(s.T().Context(), draft.ChatID, []uuid.UUID{attachmentID}).
|
||||
Return([]domain.AttachmentState{}, nil).
|
||||
Once()
|
||||
|
||||
_, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrNotFound)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendRejectsPendingAttachment() {
|
||||
attachmentID := uuid.New()
|
||||
draft := s.draft()
|
||||
draft.AttachmentIDs = []uuid.UUID{attachmentID}
|
||||
|
||||
s.messages.EXPECT().IsChatMember(s.T().Context(), draft.ChatID, draft.SenderID).Return(true, nil).Once()
|
||||
s.attachments.EXPECT().
|
||||
ListStates(s.T().Context(), draft.ChatID, []uuid.UUID{attachmentID}).
|
||||
Return([]domain.AttachmentState{{ID: attachmentID, Status: domain.AttachmentPending}}, nil).
|
||||
Once()
|
||||
|
||||
_, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrConflict)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestSendRejectsDuplicateAttachments() {
|
||||
id := uuid.New()
|
||||
draft := s.draft()
|
||||
draft.AttachmentIDs = []uuid.UUID{id, id}
|
||||
|
||||
_, err := s.service.Send(s.T().Context(), draft)
|
||||
s.Require().ErrorIs(err, domain.ErrValidation)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/storage/postgres/internal/sqlcgen"
|
||||
)
|
||||
|
||||
type AttachmentRepository struct {
|
||||
store *Store
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
member, err := r.store.queries.IsChatMember(ctx, sqlcgen.IsChatMemberParams{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check chat membership: %w", mapError(err))
|
||||
}
|
||||
|
||||
return member, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) Create(ctx context.Context, draft domain.AttachmentDraft) (domain.Attachment, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
row, err := r.store.queries.CreateAttachment(ctx, sqlcgen.CreateAttachmentParams{
|
||||
ID: draft.ID,
|
||||
ChatID: draft.ChatID,
|
||||
OwnerID: draft.OwnerID,
|
||||
ObjectKey: draft.ObjectKey,
|
||||
Filename: draft.Filename,
|
||||
ContentType: draft.ContentType,
|
||||
SizeBytes: draft.SizeBytes,
|
||||
Sha256: draft.SHA256,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("create attachment: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAttachment(row), nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) MarkReady(
|
||||
ctx context.Context,
|
||||
id uuid.UUID,
|
||||
objectKey string,
|
||||
) (domain.Attachment, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
row, err := r.store.queries.MarkAttachmentReady(ctx, sqlcgen.MarkAttachmentReadyParams{
|
||||
ID: id,
|
||||
ObjectKey: objectKey,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("mark attachment ready: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAttachment(row), nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) DeletePending(ctx context.Context, id uuid.UUID) error {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
if err := r.store.queries.DeletePendingAttachment(ctx, id); err != nil {
|
||||
return fmt.Errorf("delete pending attachment: %w", mapError(err))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) GetForMember(
|
||||
ctx context.Context,
|
||||
userID, id uuid.UUID,
|
||||
) (domain.Attachment, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
row, err := r.store.queries.GetAttachmentForMember(ctx, sqlcgen.GetAttachmentForMemberParams{
|
||||
ID: id,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("get attachment for member: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAttachment(row), nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) GetForOwner(
|
||||
ctx context.Context,
|
||||
ownerID, id uuid.UUID,
|
||||
) (domain.Attachment, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
row, err := r.store.queries.GetAttachmentForOwner(ctx, sqlcgen.GetAttachmentForOwnerParams{
|
||||
ID: id,
|
||||
OwnerID: ownerID,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("get attachment for owner: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAttachment(row), nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) ListStates(
|
||||
ctx context.Context,
|
||||
chatID uuid.UUID,
|
||||
ids []uuid.UUID,
|
||||
) ([]domain.AttachmentState, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
rows, err := r.store.queries.ListAttachmentStatesForChat(ctx, sqlcgen.ListAttachmentStatesForChatParams{
|
||||
ChatID: chatID,
|
||||
Ids: ids,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list attachment states: %w", mapError(err))
|
||||
}
|
||||
|
||||
states := make([]domain.AttachmentState, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
states = append(states, domain.AttachmentState{
|
||||
ID: row.ID,
|
||||
Status: domain.AttachmentStatus(row.Status),
|
||||
})
|
||||
}
|
||||
|
||||
return states, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) ListStalePending(
|
||||
ctx context.Context,
|
||||
createdBefore time.Time,
|
||||
limit int32,
|
||||
) ([]domain.Attachment, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
rows, err := r.store.queries.ListStalePendingAttachments(ctx, sqlcgen.ListStalePendingAttachmentsParams{
|
||||
CreatedBefore: createdBefore,
|
||||
RowLimit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list stale attachments: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAll(rows, mapAttachment), nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepository) ListKeys(
|
||||
ctx context.Context,
|
||||
limit, offset int32,
|
||||
) ([]domain.AttachmentKey, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
rows, err := r.store.queries.ListAttachmentKeys(ctx, sqlcgen.ListAttachmentKeysParams{
|
||||
RowLimit: limit,
|
||||
RowOffset: offset,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list attachment keys: %w", mapError(err))
|
||||
}
|
||||
|
||||
keys := make([]domain.AttachmentKey, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
keys = append(keys, domain.AttachmentKey{
|
||||
ID: row.ID,
|
||||
ObjectKey: row.ObjectKey,
|
||||
Status: domain.AttachmentStatus(row.Status),
|
||||
CreatedAt: toUTCTime(row.CreatedAt),
|
||||
})
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func mapAttachment(row sqlcgen.Attachment) domain.Attachment {
|
||||
attachment := domain.Attachment{
|
||||
ID: row.ID,
|
||||
ChatID: row.ChatID,
|
||||
OwnerID: row.OwnerID,
|
||||
ObjectKey: row.ObjectKey,
|
||||
Filename: row.Filename,
|
||||
ContentType: row.ContentType,
|
||||
SizeBytes: row.SizeBytes,
|
||||
SHA256: row.Sha256,
|
||||
Status: domain.AttachmentStatus(row.Status),
|
||||
CreatedAt: toUTCTime(row.CreatedAt),
|
||||
UpdatedAt: toUTCTime(row.UpdatedAt),
|
||||
}
|
||||
|
||||
if row.ErrorCode.Valid {
|
||||
code := row.ErrorCode.String
|
||||
attachment.ErrorCode = &code
|
||||
}
|
||||
|
||||
return attachment
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
//go:build integration
|
||||
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
type AttachmentRepositorySuite struct {
|
||||
BaseSuite
|
||||
}
|
||||
|
||||
func TestAttachmentRepositorySuite(t *testing.T) {
|
||||
suite.Run(t, new(AttachmentRepositorySuite))
|
||||
}
|
||||
|
||||
func (s *AttachmentRepositorySuite) TestLifecycleAndMessageLinks() {
|
||||
alice := s.createUser("alice")
|
||||
bob := s.createUser("bob")
|
||||
eve := s.createUser("eve")
|
||||
chat := s.createChat(alice, bob)
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
s.Require().NoError(err)
|
||||
|
||||
objectKey := "attachments/" + chat.ID.String() + "/" + id.String()
|
||||
digest := strings.Repeat("a", 64)
|
||||
|
||||
created, err := s.store.Attachments().Create(s.T().Context(), domain.AttachmentDraft{
|
||||
ID: id,
|
||||
ChatID: chat.ID,
|
||||
OwnerID: alice.ID,
|
||||
ObjectKey: objectKey,
|
||||
Filename: "photo.png",
|
||||
ContentType: "image/png",
|
||||
SizeBytes: 123,
|
||||
SHA256: digest,
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
s.Equal(domain.AttachmentPending, created.Status)
|
||||
|
||||
visible, err := s.store.Attachments().GetForMember(s.T().Context(), bob.ID, id)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(alice.ID, visible.OwnerID)
|
||||
|
||||
_, err = s.store.Attachments().GetForMember(s.T().Context(), eve.ID, id)
|
||||
s.Require().ErrorIs(err, domain.ErrNotFound)
|
||||
|
||||
ready, err := s.store.Attachments().MarkReady(s.T().Context(), id, objectKey)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(domain.AttachmentReady, ready.Status)
|
||||
|
||||
states, err := s.store.Attachments().ListStates(
|
||||
s.T().Context(), chat.ID, []uuid.UUID{id},
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(states, 1)
|
||||
s.Equal(domain.AttachmentReady, states[0].Status)
|
||||
|
||||
key := uuid.NewString()
|
||||
draft := domain.MessageDraft{
|
||||
ChatID: chat.ID,
|
||||
SenderID: alice.ID,
|
||||
Text: "",
|
||||
AttachmentIDs: []uuid.UUID{id},
|
||||
Idempotency: domain.MessageIdempotency{
|
||||
Key: key,
|
||||
RequestHash: [32]byte{1},
|
||||
},
|
||||
}
|
||||
|
||||
message, err := s.store.Messages().Create(s.T().Context(), draft)
|
||||
s.Require().NoError(err)
|
||||
s.Equal([]uuid.UUID{id}, message.AttachmentIDs)
|
||||
|
||||
page, err := s.store.Messages().ListVisible(s.T().Context(), chat.ID, bob.ID, nil, 50)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Len(page.Items, 1)
|
||||
s.Equal([]uuid.UUID{id}, page.Items[0].AttachmentIDs)
|
||||
s.Empty(page.Items[0].Text)
|
||||
|
||||
replayed, err := s.store.Messages().Create(s.T().Context(), draft)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(message.ID, replayed.ID)
|
||||
s.Equal([]uuid.UUID{id}, replayed.AttachmentIDs)
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachments.sql
|
||||
|
||||
package sqlcgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const attachToMessage = `-- name: AttachToMessage :exec
|
||||
INSERT INTO message_attachments (message_id, attachment_id, position)
|
||||
VALUES ($1, $2, $3)
|
||||
`
|
||||
|
||||
type AttachToMessageParams struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
Position int16
|
||||
}
|
||||
|
||||
func (q *Queries) AttachToMessage(ctx context.Context, arg AttachToMessageParams) error {
|
||||
_, err := q.db.Exec(ctx, attachToMessage, arg.MessageID, arg.AttachmentID, arg.Position)
|
||||
return err
|
||||
}
|
||||
|
||||
const createAttachment = `-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256, status
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
'pending'
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateAttachmentParams struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, createAttachment,
|
||||
arg.ID,
|
||||
arg.ChatID,
|
||||
arg.OwnerID,
|
||||
arg.ObjectKey,
|
||||
arg.Filename,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deletePendingAttachment = `-- name: DeletePendingAttachment :exec
|
||||
DELETE FROM attachments
|
||||
WHERE id = $1
|
||||
AND status = 'pending'
|
||||
`
|
||||
|
||||
func (q *Queries) DeletePendingAttachment(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deletePendingAttachment, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const getAttachmentForMember = `-- name: GetAttachmentForMember :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = $1
|
||||
WHERE a.id = $2
|
||||
`
|
||||
|
||||
type GetAttachmentForMemberParams struct {
|
||||
UserID uuid.UUID
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentForMember(ctx context.Context, arg GetAttachmentForMemberParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentForMember, arg.UserID, arg.ID)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAttachmentForOwner = `-- name: GetAttachmentForOwner :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = a.owner_id
|
||||
WHERE a.id = $1
|
||||
AND a.owner_id = $2
|
||||
`
|
||||
|
||||
type GetAttachmentForOwnerParams struct {
|
||||
ID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetAttachmentForOwner(ctx context.Context, arg GetAttachmentForOwnerParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentForOwner, arg.ID, arg.OwnerID)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAttachmentKeys = `-- name: ListAttachmentKeys :many
|
||||
SELECT id, object_key, status, created_at
|
||||
FROM attachments
|
||||
ORDER BY id
|
||||
LIMIT $2 OFFSET $1
|
||||
`
|
||||
|
||||
type ListAttachmentKeysParams struct {
|
||||
RowOffset int32
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
type ListAttachmentKeysRow struct {
|
||||
ID uuid.UUID
|
||||
ObjectKey string
|
||||
Status string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentKeys(ctx context.Context, arg ListAttachmentKeysParams) ([]ListAttachmentKeysRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentKeys, arg.RowOffset, arg.RowLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAttachmentKeysRow{}
|
||||
for rows.Next() {
|
||||
var i ListAttachmentKeysRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ObjectKey,
|
||||
&i.Status,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listAttachmentStatesForChat = `-- name: ListAttachmentStatesForChat :many
|
||||
SELECT id, status
|
||||
FROM attachments
|
||||
WHERE chat_id = $1
|
||||
AND id = ANY($2::uuid[])
|
||||
`
|
||||
|
||||
type ListAttachmentStatesForChatParams struct {
|
||||
ChatID uuid.UUID
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
type ListAttachmentStatesForChatRow struct {
|
||||
ID uuid.UUID
|
||||
Status string
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentStatesForChat(ctx context.Context, arg ListAttachmentStatesForChatParams) ([]ListAttachmentStatesForChatRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentStatesForChat, arg.ChatID, arg.Ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAttachmentStatesForChatRow{}
|
||||
for rows.Next() {
|
||||
var i ListAttachmentStatesForChatRow
|
||||
if err := rows.Scan(&i.ID, &i.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachments = `-- name: ListMessageAttachments :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY($1::uuid[])
|
||||
ORDER BY message_id, position
|
||||
`
|
||||
|
||||
type ListMessageAttachmentsRow struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListMessageAttachments(ctx context.Context, messageIds []uuid.UUID) ([]ListMessageAttachmentsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listMessageAttachments, messageIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListMessageAttachmentsRow{}
|
||||
for rows.Next() {
|
||||
var i ListMessageAttachmentsRow
|
||||
if err := rows.Scan(&i.MessageID, &i.AttachmentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listStalePendingAttachments = `-- name: ListStalePendingAttachments :many
|
||||
SELECT id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'pending'
|
||||
AND created_at < $1
|
||||
ORDER BY created_at
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListStalePendingAttachmentsParams struct {
|
||||
CreatedBefore time.Time
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListStalePendingAttachments(ctx context.Context, arg ListStalePendingAttachmentsParams) ([]Attachment, error) {
|
||||
rows, err := q.db.Query(ctx, listStalePendingAttachments, arg.CreatedBefore, arg.RowLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Attachment{}
|
||||
for rows.Next() {
|
||||
var i Attachment
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markAttachmentReady = `-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
object_key = $1,
|
||||
updated_at = now()
|
||||
WHERE id = $2
|
||||
AND status = 'pending'
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
type MarkAttachmentReadyParams struct {
|
||||
ObjectKey string
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) MarkAttachmentReady(ctx context.Context, arg MarkAttachmentReadyParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, markAttachmentReady, arg.ObjectKey, arg.ID)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.ObjectKey,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -11,6 +11,21 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
ObjectKey string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Sha256 string
|
||||
Status string
|
||||
ErrorCode pgtype.Text
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
@@ -31,6 +46,12 @@ type Message struct {
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type MessageAttachment struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
Position int16
|
||||
}
|
||||
|
||||
type MessageIdempotency struct {
|
||||
ActorID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
|
||||
@@ -19,6 +19,21 @@ type MessageRepository struct {
|
||||
store *Store
|
||||
}
|
||||
|
||||
func (r *MessageRepository) IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) {
|
||||
ctx, cancel := r.store.timeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
member, err := r.store.queries.IsChatMember(ctx, sqlcgen.IsChatMemberParams{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check chat membership: %w", mapError(err))
|
||||
}
|
||||
|
||||
return member, nil
|
||||
}
|
||||
|
||||
func (r *MessageRepository) Create(ctx context.Context, draft domain.MessageDraft) (domain.Message, error) {
|
||||
var created domain.Message
|
||||
|
||||
@@ -80,6 +95,16 @@ func createClaimed(ctx context.Context, queries *sqlcgen.Queries, draft domain.M
|
||||
return domain.Message{}, mapError(err)
|
||||
}
|
||||
|
||||
for position, attachmentID := range draft.AttachmentIDs {
|
||||
if err := queries.AttachToMessage(ctx, sqlcgen.AttachToMessageParams{
|
||||
MessageID: row.ID,
|
||||
AttachmentID: attachmentID,
|
||||
Position: int16(position),
|
||||
}); err != nil {
|
||||
return domain.Message{}, mapError(err)
|
||||
}
|
||||
}
|
||||
|
||||
err = queries.CompleteMessageIdempotency(ctx, sqlcgen.CompleteMessageIdempotencyParams{
|
||||
MessageID: pgtype.UUID{Bytes: row.ID, Valid: true},
|
||||
ActorID: draft.SenderID,
|
||||
@@ -90,7 +115,11 @@ func createClaimed(ctx context.Context, queries *sqlcgen.Queries, draft domain.M
|
||||
return domain.Message{}, mapError(err)
|
||||
}
|
||||
|
||||
return mapMessage(row.ID, row.ChatID, row.SenderID, row.Text, row.CreatedAt), nil
|
||||
created := mapMessage(row.ID, row.ChatID, row.SenderID, row.Text, row.CreatedAt)
|
||||
created.AttachmentIDs = make([]uuid.UUID, 0, len(draft.AttachmentIDs))
|
||||
created.AttachmentIDs = append(created.AttachmentIDs, draft.AttachmentIDs...)
|
||||
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func replayIdempotent(ctx context.Context, queries *sqlcgen.Queries, draft domain.MessageDraft) (domain.Message, error) {
|
||||
@@ -116,7 +145,34 @@ func replayIdempotent(ctx context.Context, queries *sqlcgen.Queries, draft domai
|
||||
return domain.Message{}, mapError(err)
|
||||
}
|
||||
|
||||
return mapMessage(row.ID, row.ChatID, row.SenderID, row.Text, row.CreatedAt), nil
|
||||
replayed := mapMessage(row.ID, row.ChatID, row.SenderID, row.Text, row.CreatedAt)
|
||||
replayed.AttachmentIDs, err = listAttachmentIDs(ctx, queries, row.ID)
|
||||
if err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
|
||||
return replayed, nil
|
||||
}
|
||||
|
||||
// listAttachmentIDs returns attachment IDs in the order the sender supplied
|
||||
// them. An empty result is normalised to an empty slice, never nil: the
|
||||
// contract marks attachment_ids as required.
|
||||
func listAttachmentIDs(
|
||||
ctx context.Context,
|
||||
queries *sqlcgen.Queries,
|
||||
messageID uuid.UUID,
|
||||
) ([]uuid.UUID, error) {
|
||||
rows, err := queries.ListMessageAttachments(ctx, []uuid.UUID{messageID})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list message attachments: %w", mapError(err))
|
||||
}
|
||||
|
||||
ids := make([]uuid.UUID, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
ids = append(ids, row.AttachmentID)
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (r *MessageRepository) ListVisible(
|
||||
@@ -164,6 +220,8 @@ func (r *MessageRepository) fetch(
|
||||
after *domain.Cursor,
|
||||
fetch int32,
|
||||
) ([]domain.Message, error) {
|
||||
var messages []domain.Message
|
||||
|
||||
if after == nil {
|
||||
rows, err := r.store.queries.ListMessagesForChat(ctx, sqlcgen.ListMessagesForChatParams{
|
||||
ChatID: chatID,
|
||||
@@ -174,21 +232,63 @@ func (r *MessageRepository) fetch(
|
||||
return nil, fmt.Errorf("list messages: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAll(rows, mapMessageRow), nil
|
||||
messages = mapAll(rows, mapMessageRow)
|
||||
} else {
|
||||
rows, err := r.store.queries.ListMessagesForChatAfter(ctx, sqlcgen.ListMessagesForChatAfterParams{
|
||||
ChatID: chatID,
|
||||
CallerID: callerID,
|
||||
AfterCreatedAt: after.CreatedAt,
|
||||
AfterID: after.ID,
|
||||
RowLimit: fetch,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list messages: %w", mapError(err))
|
||||
}
|
||||
|
||||
messages = mapAll(rows, mapMessageRow)
|
||||
}
|
||||
|
||||
rows, err := r.store.queries.ListMessagesForChatAfter(ctx, sqlcgen.ListMessagesForChatAfterParams{
|
||||
ChatID: chatID,
|
||||
CallerID: callerID,
|
||||
AfterCreatedAt: after.CreatedAt,
|
||||
AfterID: after.ID,
|
||||
RowLimit: fetch,
|
||||
})
|
||||
if err := attachMessageLinks(ctx, r.store.queries, messages); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// attachMessageLinks fetches all links for a page in one query and normalises
|
||||
// absent lists to empty slices.
|
||||
func attachMessageLinks(
|
||||
ctx context.Context,
|
||||
queries *sqlcgen.Queries,
|
||||
messages []domain.Message,
|
||||
) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ids := make([]uuid.UUID, 0, len(messages))
|
||||
for index := range messages {
|
||||
ids = append(ids, messages[index].ID)
|
||||
messages[index].AttachmentIDs = []uuid.UUID{}
|
||||
}
|
||||
|
||||
rows, err := queries.ListMessageAttachments(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list messages: %w", mapError(err))
|
||||
return fmt.Errorf("list message attachments: %w", mapError(err))
|
||||
}
|
||||
|
||||
return mapAll(rows, mapMessageRow), nil
|
||||
byMessage := make(map[uuid.UUID][]uuid.UUID, len(messages))
|
||||
for _, row := range rows {
|
||||
byMessage[row.MessageID] = append(byMessage[row.MessageID], row.AttachmentID)
|
||||
}
|
||||
|
||||
for index := range messages {
|
||||
if linked, ok := byMessage[messages[index].ID]; ok {
|
||||
messages[index].AttachmentIDs = linked
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapMessageRow(row sqlcgen.Message) domain.Message {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
-- +goose Up
|
||||
-- Attachment metadata lives in PostgreSQL; the original bytes live only in S3.
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
|
||||
object_key text NOT NULL,
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
status text NOT NULL,
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT attachments_object_key_key UNIQUE (object_key),
|
||||
CONSTRAINT attachments_status_check CHECK (status IN ('pending', 'ready', 'failed')),
|
||||
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_filename_length CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_size_check CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_check CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
||||
CONSTRAINT attachments_error_consistency CHECK (
|
||||
(status = 'ready' AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL) OR
|
||||
status = 'pending'
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX attachments_chat_id_created_at_id_idx ON attachments (chat_id, created_at, id);
|
||||
CREATE INDEX attachments_status_created_at_idx ON attachments (status, created_at);
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE RESTRICT,
|
||||
position smallint NOT NULL,
|
||||
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
|
||||
CONSTRAINT message_attachments_position_key UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX message_attachments_attachment_id_idx ON message_attachments (attachment_id);
|
||||
|
||||
-- Lab1-3 messages must stay valid. Attachment-only messages use text = '';
|
||||
-- the "text OR attachment" rule is enforced by the service and ogen.
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_not_blank;
|
||||
|
||||
-- +goose Down
|
||||
-- WARNING: fails if attachment-only messages already exist. Roll back such
|
||||
-- rows or the constraint cannot be restored.
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank CHECK (btrim(text) <> '');
|
||||
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
@@ -0,0 +1,82 @@
|
||||
-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256, status
|
||||
)
|
||||
VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(chat_id),
|
||||
sqlc.arg(owner_id),
|
||||
sqlc.arg(object_key),
|
||||
sqlc.arg(filename),
|
||||
sqlc.arg(content_type),
|
||||
sqlc.arg(size_bytes),
|
||||
sqlc.arg(sha256),
|
||||
'pending'
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at;
|
||||
|
||||
-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
object_key = sqlc.arg(object_key),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)
|
||||
AND status = 'pending'
|
||||
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at;
|
||||
|
||||
-- name: GetAttachmentForMember :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = sqlc.arg(user_id)
|
||||
WHERE a.id = sqlc.arg(id);
|
||||
|
||||
-- name: GetAttachmentForOwner :one
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
JOIN chat_members cm
|
||||
ON cm.chat_id = a.chat_id
|
||||
AND cm.user_id = a.owner_id
|
||||
WHERE a.id = sqlc.arg(id)
|
||||
AND a.owner_id = sqlc.arg(owner_id);
|
||||
|
||||
-- name: ListAttachmentStatesForChat :many
|
||||
SELECT id, status
|
||||
FROM attachments
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND id = ANY(sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: AttachToMessage :exec
|
||||
INSERT INTO message_attachments (message_id, attachment_id, position)
|
||||
VALUES (sqlc.arg(message_id), sqlc.arg(attachment_id), sqlc.arg(position));
|
||||
|
||||
-- name: ListMessageAttachments :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
|
||||
ORDER BY message_id, position;
|
||||
|
||||
-- name: DeletePendingAttachment :exec
|
||||
DELETE FROM attachments
|
||||
WHERE id = sqlc.arg(id)
|
||||
AND status = 'pending';
|
||||
|
||||
-- name: ListStalePendingAttachments :many
|
||||
SELECT id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'pending'
|
||||
AND created_at < sqlc.arg(created_before)
|
||||
ORDER BY created_at
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
|
||||
-- name: ListAttachmentKeys :many
|
||||
SELECT id, object_key, status, created_at
|
||||
FROM attachments
|
||||
ORDER BY id
|
||||
LIMIT sqlc.arg(row_limit) OFFSET sqlc.arg(row_offset);
|
||||
@@ -17,10 +17,11 @@ type Store struct {
|
||||
queryTimeout time.Duration
|
||||
idempotencyTTL time.Duration
|
||||
|
||||
users *UserRepository
|
||||
chats *ChatRepository
|
||||
messages *MessageRepository
|
||||
sessions *SessionRepository
|
||||
users *UserRepository
|
||||
chats *ChatRepository
|
||||
messages *MessageRepository
|
||||
sessions *SessionRepository
|
||||
attachments *AttachmentRepository
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration) (*Store, error) {
|
||||
@@ -65,6 +66,7 @@ func New(ctx context.Context, cfg config.Postgres, idempotencyTTL time.Duration)
|
||||
store.chats = &ChatRepository{store: store}
|
||||
store.messages = &MessageRepository{store: store}
|
||||
store.sessions = &SessionRepository{store: store}
|
||||
store.attachments = &AttachmentRepository{store: store}
|
||||
|
||||
return store, nil
|
||||
}
|
||||
@@ -77,6 +79,8 @@ func (s *Store) Messages() *MessageRepository { return s.messages }
|
||||
|
||||
func (s *Store) Sessions() *SessionRepository { return s.sessions }
|
||||
|
||||
func (s *Store) Attachments() *AttachmentRepository { return s.attachments }
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
if err := s.pool.Ping(ctx); err != nil {
|
||||
return fmt.Errorf("ping database: %w", err)
|
||||
@@ -87,6 +91,43 @@ func (s *Store) Ping(ctx context.Context) error {
|
||||
|
||||
func (s *Store) Close() { s.pool.Close() }
|
||||
|
||||
// sweepLockKey is a process-independent constant: every replica contends for the
|
||||
// same PostgreSQL session lock, so only one sweeper runs at a time.
|
||||
const sweepLockKey int64 = 0x4c414234 // "LAB4"
|
||||
|
||||
// AcquireSweepLock holds a session-level advisory lock on a dedicated
|
||||
// connection. The returned release function must be called on that same
|
||||
// connection before it is returned to the pool.
|
||||
func (s *Store) AcquireSweepLock(ctx context.Context) (func(), bool, error) {
|
||||
conn, err := s.pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("acquire sweep connection: %w", err)
|
||||
}
|
||||
|
||||
var acquired bool
|
||||
if err := conn.QueryRow(ctx, "SELECT pg_try_advisory_lock($1)", sweepLockKey).Scan(&acquired); err != nil {
|
||||
conn.Release()
|
||||
|
||||
return nil, false, fmt.Errorf("acquire sweep lock: %w", err)
|
||||
}
|
||||
|
||||
if !acquired {
|
||||
conn.Release()
|
||||
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
release := func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, _ = conn.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", sweepLockKey)
|
||||
conn.Release()
|
||||
}
|
||||
|
||||
return release, true, nil
|
||||
}
|
||||
|
||||
func (s *Store) timeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if s.queryTimeout <= 0 {
|
||||
return context.WithCancel(ctx)
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
attachmentService "mtusi-messenger/internal/service/attachment"
|
||||
"mtusi-messenger/internal/transport/rest/internal/oas"
|
||||
)
|
||||
|
||||
func (h *Handler) UploadAttachment(
|
||||
ctx context.Context,
|
||||
req oas.UploadAttachmentReq,
|
||||
params oas.UploadAttachmentParams,
|
||||
) (oas.UploadAttachmentRes, error) {
|
||||
body, declared, err := uploadBody(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
attachment, err := h.attachments.Upload(ctx, attachmentService.UploadInput{
|
||||
ChatID: params.ChatID,
|
||||
OwnerID: caller(ctx),
|
||||
Filename: params.Filename,
|
||||
DeclaredContentType: declared,
|
||||
Body: body,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &oas.AttachmentHeaders{
|
||||
XRequestID: oas.NewOptString(requestIDFromContext(ctx)),
|
||||
Response: attachmentResponse(attachment),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Handler) GetAttachment(
|
||||
ctx context.Context,
|
||||
params oas.GetAttachmentParams,
|
||||
) (oas.GetAttachmentRes, error) {
|
||||
attachment, err := h.attachments.Get(ctx, caller(ctx), params.AttachmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &oas.AttachmentHeaders{
|
||||
XRequestID: oas.NewOptString(requestIDFromContext(ctx)),
|
||||
Response: attachmentResponse(attachment),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Handler) DownloadAttachment(
|
||||
ctx context.Context,
|
||||
params oas.DownloadAttachmentParams,
|
||||
) (oas.DownloadAttachmentRes, error) {
|
||||
if variant, ok := params.Variant.Get(); ok && variant != oas.DownloadAttachmentVariantOriginal {
|
||||
return nil, fmt.Errorf("%w: variant %q is not available", domain.ErrNotFound, variant)
|
||||
}
|
||||
|
||||
attachment, body, err := h.attachments.Open(ctx, caller(ctx), params.AttachmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The generated transport copies the body after the handler returns. Close
|
||||
// the S3 stream when the client disconnects; closeOnEOF covers the normal
|
||||
// path.
|
||||
_ = context.AfterFunc(ctx, func() { _ = body.Close() })
|
||||
|
||||
requestID := oas.NewOptString(requestIDFromContext(ctx))
|
||||
reader := closeOnEOF{ReadCloser: body}
|
||||
|
||||
switch attachment.ContentType {
|
||||
case "image/png":
|
||||
return &oas.DownloadAttachmentOKImagePNGHeaders{
|
||||
XRequestID: requestID,
|
||||
Response: oas.DownloadAttachmentOKImagePNG{Data: reader},
|
||||
}, nil
|
||||
case "image/jpeg":
|
||||
return &oas.DownloadAttachmentOKImageJpegHeaders{
|
||||
XRequestID: requestID,
|
||||
Response: oas.DownloadAttachmentOKImageJpeg{Data: reader},
|
||||
}, nil
|
||||
default:
|
||||
_ = body.Close()
|
||||
|
||||
return nil, fmt.Errorf("%w: unexpected content type %q", domain.ErrUnavailable, attachment.ContentType)
|
||||
}
|
||||
}
|
||||
|
||||
func uploadBody(req oas.UploadAttachmentReq) (io.Reader, string, error) {
|
||||
switch typed := req.(type) {
|
||||
case *oas.UploadAttachmentReqImagePNG:
|
||||
return typed.Data, "image/png", nil
|
||||
case *oas.UploadAttachmentReqImageJpeg:
|
||||
return typed.Data, "image/jpeg", nil
|
||||
default:
|
||||
return nil, "", fmt.Errorf("%w: request body must be image/png or image/jpeg", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
}
|
||||
|
||||
func attachmentResponse(attachment domain.Attachment) oas.Attachment {
|
||||
response := oas.Attachment{
|
||||
ID: attachment.ID,
|
||||
ChatID: attachment.ChatID,
|
||||
OwnerID: attachment.OwnerID,
|
||||
Filename: attachment.Filename,
|
||||
SizeBytes: int(attachment.SizeBytes),
|
||||
SHA256: attachment.SHA256,
|
||||
Status: oas.AttachmentStatusReady,
|
||||
Variants: []oas.Variant{},
|
||||
ErrorCode: oas.NilString{Null: true},
|
||||
CreatedAt: attachment.CreatedAt,
|
||||
UpdatedAt: attachment.UpdatedAt,
|
||||
}
|
||||
|
||||
switch attachment.ContentType {
|
||||
case "image/png":
|
||||
response.ContentType = oas.AttachmentContentTypeImagePNG
|
||||
case "image/jpeg":
|
||||
response.ContentType = oas.AttachmentContentTypeImageJpeg
|
||||
default:
|
||||
response.ContentType = oas.AttachmentContentType(attachment.ContentType)
|
||||
}
|
||||
|
||||
if attachment.ErrorCode != nil {
|
||||
response.ErrorCode = oas.NilString{Value: *attachment.ErrorCode}
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
type closeOnEOF struct {
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
func (r closeOnEOF) Read(p []byte) (int, error) {
|
||||
n, err := r.ReadCloser.Read(p)
|
||||
if err != nil {
|
||||
_ = r.Close()
|
||||
}
|
||||
|
||||
return n, err
|
||||
}
|
||||
@@ -3,22 +3,36 @@ package rest
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// attachmentUploadPath matches POST /api/v1/chats/{chat_id}/attachments. The
|
||||
// UUID syntax is not validated here; this middleware only selects a body limit,
|
||||
// and the generated transport rejects malformed identifiers later.
|
||||
var attachmentUploadPath = regexp.MustCompile(`^/api/v1/chats/[^/]+/attachments$`)
|
||||
|
||||
// bodyLimitMiddleware bounds the request body before it reaches the generated
|
||||
// transport. Without it the JSON decoders read the whole body into memory before
|
||||
// any schema check or authentication happens, so an oversized request to a public
|
||||
// endpoint (for example POST /api/v1/users) could exhaust the replica's memory.
|
||||
//
|
||||
// The declared length is rejected early with 413; a client that lies about
|
||||
// Content-Length is cut off by http.MaxBytesReader, which also makes the server
|
||||
// close the connection instead of reading the rest.
|
||||
func bodyLimitMiddleware(limit int64, log *slog.Logger, next http.Handler) http.Handler {
|
||||
if limit <= 0 {
|
||||
// transport. JSON endpoints keep the small default limit; the attachment upload
|
||||
// gets its own larger limit. Without this split the 10 MiB upload would be
|
||||
// rejected before the handler, and raising the global limit would let any
|
||||
// public JSON endpoint read a 10 MiB body.
|
||||
func bodyLimitMiddleware(
|
||||
defaultLimit, uploadLimit int64,
|
||||
log *slog.Logger,
|
||||
next http.Handler,
|
||||
) http.Handler {
|
||||
if defaultLimit <= 0 && uploadLimit <= 0 {
|
||||
return next
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
limit := limitForRequest(r, defaultLimit, uploadLimit)
|
||||
if limit <= 0 {
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.ContentLength > limit {
|
||||
logRejected(r.Context(), log, r, http.StatusRequestEntityTooLarge, nil)
|
||||
writeError(r.Context(), log, w, r, http.StatusRequestEntityTooLarge)
|
||||
@@ -33,3 +47,11 @@ func bodyLimitMiddleware(limit int64, log *slog.Logger, next http.Handler) http.
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func limitForRequest(r *http.Request, defaultLimit, uploadLimit int64) int64 {
|
||||
if r.Method == http.MethodPost && attachmentUploadPath.MatchString(r.URL.Path) {
|
||||
return uploadLimit
|
||||
}
|
||||
|
||||
return defaultLimit
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLimitForRequest(t *testing.T) {
|
||||
const (
|
||||
defaultLimit = 1024
|
||||
uploadLimit = 10 << 20
|
||||
)
|
||||
|
||||
upload, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments", nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(uploadLimit), limitForRequest(upload, defaultLimit, uploadLimit))
|
||||
|
||||
messages, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/api/v1/chats/11111111-1111-4111-8111-111111111111/messages", nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(defaultLimit), limitForRequest(messages, defaultLimit, uploadLimit))
|
||||
|
||||
presign, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments/presign", nil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(defaultLimit), limitForRequest(presign, defaultLimit, uploadLimit))
|
||||
}
|
||||
|
||||
func TestBodyLimitMiddlewareSelectsUploadLimit(t *testing.T) {
|
||||
const (
|
||||
defaultLimit = 16
|
||||
uploadLimit = 64
|
||||
)
|
||||
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write(body)
|
||||
})
|
||||
handler := bodyLimitMiddleware(defaultLimit, uploadLimit, nil, next)
|
||||
|
||||
upload := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost,
|
||||
"/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments",
|
||||
strings.NewReader(strings.Repeat("x", 32)),
|
||||
)
|
||||
uploadRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(uploadRecorder, upload)
|
||||
require.Equal(t, http.StatusOK, uploadRecorder.Code)
|
||||
|
||||
jsonRequest := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost,
|
||||
"/api/v1/chats/11111111-1111-4111-8111-111111111111/messages",
|
||||
strings.NewReader(strings.Repeat("x", 32)),
|
||||
)
|
||||
jsonRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(jsonRecorder, jsonRequest)
|
||||
require.Equal(t, http.StatusRequestEntityTooLarge, jsonRecorder.Code)
|
||||
}
|
||||
@@ -53,12 +53,17 @@ func statusForDomainError(err error) int {
|
||||
return http.StatusNotFound
|
||||
case errors.Is(err, domain.ErrUsernameTaken),
|
||||
errors.Is(err, domain.ErrConflict),
|
||||
errors.Is(err, domain.ErrIdempotency):
|
||||
errors.Is(err, domain.ErrIdempotency),
|
||||
errors.Is(err, domain.ErrIncomplete):
|
||||
return http.StatusConflict
|
||||
case errors.Is(err, domain.ErrValidation),
|
||||
errors.Is(err, domain.ErrUnknownMember),
|
||||
errors.Is(err, domain.ErrInvalidCursor):
|
||||
return http.StatusBadRequest
|
||||
case errors.Is(err, domain.ErrTooLarge):
|
||||
return http.StatusRequestEntityTooLarge
|
||||
case errors.Is(err, domain.ErrUnsupportedMedia):
|
||||
return http.StatusUnsupportedMediaType
|
||||
case errors.Is(err, domain.ErrUnavailable):
|
||||
return http.StatusServiceUnavailable
|
||||
default:
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
// SessionResolver is implemented by session.Service. Only token resolution is
|
||||
// needed here; the generated security handler keeps its own interface.
|
||||
type SessionResolver interface {
|
||||
Resolve(ctx context.Context, token string) (domain.User, error)
|
||||
}
|
||||
|
||||
// Extensions implements the optional presigned-upload endpoints. They are
|
||||
// mounted outside the ogen server so the mandatory OpenAPI contract remains
|
||||
// untouched.
|
||||
type Extensions struct {
|
||||
attachments AttachmentService
|
||||
sessions SessionResolver
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewExtensions(
|
||||
attachments AttachmentService,
|
||||
sessions SessionResolver,
|
||||
log *slog.Logger,
|
||||
) *Extensions {
|
||||
return &Extensions{attachments: attachments, sessions: sessions, log: log}
|
||||
}
|
||||
|
||||
func (e *Extensions) Presign() http.Handler {
|
||||
return e.route(http.MethodPost, "presign", e.presign)
|
||||
}
|
||||
|
||||
func (e *Extensions) Complete() http.Handler {
|
||||
return e.route(http.MethodPost, "complete", e.complete)
|
||||
}
|
||||
|
||||
type extensionHandler func(http.ResponseWriter, *http.Request, uuid.UUID) error
|
||||
|
||||
func (e *Extensions) route(method, operation string, handler extensionHandler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
started := time.Now()
|
||||
writer := &statusWriter{ResponseWriter: w}
|
||||
|
||||
if r.Method != method {
|
||||
writer.Header().Set("Allow", method)
|
||||
writeError(r.Context(), e.log, writer, r, http.StatusMethodNotAllowed)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := e.authenticate(r)
|
||||
if err != nil {
|
||||
e.fail(writer, r, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = handler(writer, r, userID)
|
||||
if err != nil {
|
||||
e.fail(writer, r, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if e.log != nil {
|
||||
e.log.InfoContext(r.Context(), "extension request handled",
|
||||
slog.String("request_id", requestIDFromContext(r.Context())),
|
||||
slog.String("operation", operation),
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.URL.Path),
|
||||
slog.String("user_id", userID.String()),
|
||||
slog.Int("status", writer.statusCode()),
|
||||
slog.Duration("duration", time.Since(started)),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Extensions) authenticate(r *http.Request) (uuid.UUID, error) {
|
||||
header := r.Header.Get("Authorization")
|
||||
const prefix = "Bearer "
|
||||
|
||||
if !strings.HasPrefix(header, prefix) {
|
||||
return uuid.Nil, fmt.Errorf("%w: bearer token is required", domain.ErrUnauthenticated)
|
||||
}
|
||||
|
||||
token := strings.TrimSpace(strings.TrimPrefix(header, prefix))
|
||||
if token == "" {
|
||||
return uuid.Nil, fmt.Errorf("%w: bearer token is empty", domain.ErrUnauthenticated)
|
||||
}
|
||||
|
||||
user, err := e.sessions.Resolve(r.Context(), token)
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("%w: %w", domain.ErrUnauthenticated, err)
|
||||
}
|
||||
|
||||
return user.ID, nil
|
||||
}
|
||||
|
||||
func (e *Extensions) presign(w http.ResponseWriter, r *http.Request, userID uuid.UUID) error {
|
||||
chatID, err := uuid.Parse(r.PathValue("chat_id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: chat_id is not a UUID", domain.ErrValidation)
|
||||
}
|
||||
|
||||
var request struct {
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
if err := decodeJSON(r, &request); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result, err := e.attachments.Presign(r.Context(), domain.PresignRequest{
|
||||
ChatID: chatID,
|
||||
OwnerID: userID,
|
||||
Filename: request.Filename,
|
||||
ContentType: request.ContentType,
|
||||
SizeBytes: request.SizeBytes,
|
||||
SHA256: request.SHA256,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, http.StatusCreated, struct {
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
Method string `json:"method"`
|
||||
UploadURL string `json:"upload_url"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
SHA256 string `json:"sha256"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
}{
|
||||
AttachmentID: result.AttachmentID.String(),
|
||||
Method: http.MethodPut,
|
||||
UploadURL: result.UploadURL,
|
||||
ExpiresAt: result.ExpiresAt.UTC(),
|
||||
SHA256: result.SHA256,
|
||||
SizeBytes: result.SizeBytes,
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Extensions) complete(w http.ResponseWriter, r *http.Request, userID uuid.UUID) error {
|
||||
id, err := uuid.Parse(r.PathValue("attachment_id"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: attachment_id is not a UUID", domain.ErrValidation)
|
||||
}
|
||||
|
||||
var request struct {
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
if err := decodeJSON(r, &request); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
attachment, err := e.attachments.Complete(r.Context(), userID, id, request.SHA256)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, http.StatusOK, attachmentResponse(attachment))
|
||||
}
|
||||
|
||||
func decodeJSON(r *http.Request, target any) error {
|
||||
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil || mediaType != "application/json" {
|
||||
return fmt.Errorf("%w: Content-Type must be application/json", domain.ErrUnsupportedMedia)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(target); err != nil {
|
||||
return fmt.Errorf("%w: decode JSON: %w", domain.ErrValidation, err)
|
||||
}
|
||||
|
||||
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
return fmt.Errorf("%w: trailing JSON data", domain.ErrValidation)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Extensions) fail(w http.ResponseWriter, r *http.Request, err error) {
|
||||
status := statusForDomainError(err)
|
||||
if status == http.StatusInternalServerError && e.log != nil {
|
||||
e.log.ErrorContext(r.Context(), "extension request failed",
|
||||
slog.String("request_id", requestIDFromContext(r.Context())),
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.URL.Path),
|
||||
slog.Any("error", err),
|
||||
)
|
||||
}
|
||||
|
||||
writeError(r.Context(), e.log, w, r, status)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, payload any) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
||||
return fmt.Errorf("write JSON response: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type statusWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (w *statusWriter) WriteHeader(status int) {
|
||||
if w.status == 0 {
|
||||
w.status = status
|
||||
}
|
||||
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (w *statusWriter) Write(body []byte) (int, error) {
|
||||
if w.status == 0 {
|
||||
w.status = http.StatusOK
|
||||
}
|
||||
|
||||
return w.ResponseWriter.Write(body)
|
||||
}
|
||||
|
||||
func (w *statusWriter) statusCode() int {
|
||||
if w.status == 0 {
|
||||
return http.StatusOK
|
||||
}
|
||||
|
||||
return w.status
|
||||
}
|
||||
@@ -7,12 +7,15 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
attachmentService "mtusi-messenger/internal/service/attachment"
|
||||
"mtusi-messenger/internal/transport/rest/internal/oas"
|
||||
)
|
||||
|
||||
@@ -51,14 +54,23 @@ type SessionService interface {
|
||||
Revoke(ctx context.Context, userID uuid.UUID, token string) error
|
||||
}
|
||||
|
||||
type AttachmentService interface {
|
||||
Upload(ctx context.Context, input attachmentService.UploadInput) (domain.Attachment, error)
|
||||
Get(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, error)
|
||||
Open(ctx context.Context, callerID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error)
|
||||
Presign(ctx context.Context, request domain.PresignRequest) (domain.PresignResult, error)
|
||||
Complete(ctx context.Context, ownerID, id uuid.UUID, sha256 string) (domain.Attachment, error)
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
oas.UnimplementedHandler
|
||||
|
||||
users UserService
|
||||
chats ChatService
|
||||
messages MessageService
|
||||
health HealthChecker
|
||||
sessions SessionService
|
||||
users UserService
|
||||
chats ChatService
|
||||
messages MessageService
|
||||
attachments AttachmentService
|
||||
health HealthChecker
|
||||
sessions SessionService
|
||||
}
|
||||
|
||||
var noCursor *domain.Cursor
|
||||
@@ -67,15 +79,17 @@ func NewHandler(
|
||||
users UserService,
|
||||
chats ChatService,
|
||||
messages MessageService,
|
||||
attachments AttachmentService,
|
||||
health HealthChecker,
|
||||
sessions SessionService,
|
||||
) *Handler {
|
||||
return &Handler{
|
||||
users: users,
|
||||
chats: chats,
|
||||
messages: messages,
|
||||
health: health,
|
||||
sessions: sessions,
|
||||
users: users,
|
||||
chats: chats,
|
||||
messages: messages,
|
||||
attachments: attachments,
|
||||
health: health,
|
||||
sessions: sessions,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,9 +252,10 @@ func (h *Handler) CreateMessage(
|
||||
}
|
||||
|
||||
message, err := h.messages.Send(ctx, domain.MessageDraft{
|
||||
ChatID: params.ChatID,
|
||||
SenderID: callerID,
|
||||
Text: req.Text,
|
||||
ChatID: params.ChatID,
|
||||
SenderID: callerID,
|
||||
Text: req.Text,
|
||||
AttachmentIDs: req.AttachmentIds,
|
||||
Idempotency: domain.MessageIdempotency{
|
||||
Key: params.IdempotencyKey,
|
||||
RequestHash: fingerprint,
|
||||
@@ -260,9 +275,16 @@ func (h *Handler) CreateMessage(
|
||||
// order of the client JSON cannot change the fingerprint, while a different
|
||||
// payload with the same Idempotency-Key can (contract 1.3.0).
|
||||
func messageFingerprint(req *oas.CreateMessage) ([sha256.Size]byte, error) {
|
||||
ids := make([]uuid.UUID, 0, len(req.AttachmentIds))
|
||||
ids = append(ids, req.AttachmentIds...)
|
||||
slices.SortFunc(ids, func(left, right uuid.UUID) int {
|
||||
return bytes.Compare(left[:], right[:])
|
||||
})
|
||||
|
||||
payload, err := json.Marshal(struct {
|
||||
Text string `json:"text"`
|
||||
}{Text: req.Text})
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []uuid.UUID `json:"attachment_ids"`
|
||||
}{Text: req.Text, AttachmentIDs: ids})
|
||||
if err != nil {
|
||||
return [sha256.Size]byte{}, fmt.Errorf("encode message payload: %w", err)
|
||||
}
|
||||
@@ -396,11 +418,15 @@ func chatResponse(chat domain.Chat) oas.Chat {
|
||||
}
|
||||
|
||||
func messageResponse(message domain.Message) oas.Message {
|
||||
ids := make([]uuid.UUID, 0, len(message.AttachmentIDs))
|
||||
ids = append(ids, message.AttachmentIDs...)
|
||||
|
||||
return oas.Message{
|
||||
ID: message.ID,
|
||||
ChatID: message.ChatID,
|
||||
SenderID: message.SenderID,
|
||||
Text: message.Text,
|
||||
CreatedAt: message.CreatedAt,
|
||||
ID: message.ID,
|
||||
ChatID: message.ChatID,
|
||||
SenderID: message.SenderID,
|
||||
Text: message.Text,
|
||||
AttachmentIds: ids,
|
||||
CreatedAt: message.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func TestInstanceIDMiddleware(t *testing.T) {
|
||||
|
||||
instanceIDMiddleware("api-1", test.handler).ServeHTTP(
|
||||
recorder,
|
||||
httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil),
|
||||
httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/v1/users/me", nil),
|
||||
)
|
||||
|
||||
assert.Equal(t, test.wantID, recorder.Header().Get(instanceIDHeader))
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
var regexMap = map[string]ogenregex.Regexp{
|
||||
"^[A-Za-z0-9._:-]+$": ogenregex.MustCompile("^[A-Za-z0-9._:-]+$"),
|
||||
"^[a-f0-9]{64}$": ogenregex.MustCompile("^[a-f0-9]{64}$"),
|
||||
"^[a-z0-9_]{3,32}$": ogenregex.MustCompile("^[a-z0-9_]{3,32}$"),
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Code generated by ogen, DO NOT EDIT.
|
||||
|
||||
package oas
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// setDefaults set default value of fields.
|
||||
func (s *CreateMessage) setDefaults() {
|
||||
{
|
||||
var defaultVal0 []uuid.UUID
|
||||
s.AttachmentIds = defaultVal0
|
||||
}
|
||||
}
|
||||
@@ -529,6 +529,266 @@ func (s *Server) handleCurrentUserRequest(args [0]string, argsEscaped bool, w ht
|
||||
}
|
||||
}
|
||||
|
||||
// handleDownloadAttachmentRequest handles downloadAttachment operation.
|
||||
//
|
||||
// Скачать байты через авторизованный API.
|
||||
//
|
||||
// GET /api/v1/attachments/{attachment_id}/content
|
||||
func (s *Server) handleDownloadAttachmentRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
statusWriter := &codeRecorder{ResponseWriter: w}
|
||||
w = statusWriter
|
||||
ctx := r.Context()
|
||||
|
||||
var (
|
||||
err error
|
||||
opErrContext = ogenerrors.OperationContext{
|
||||
Name: DownloadAttachmentOperation,
|
||||
ID: "downloadAttachment",
|
||||
}
|
||||
)
|
||||
{
|
||||
type bitset = [1]uint8
|
||||
var satisfied bitset
|
||||
{
|
||||
sctx, ok, err := s.securityBearerAuth(ctx, DownloadAttachmentOperation, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.SecurityError{
|
||||
OperationContext: opErrContext,
|
||||
Security: "BearerAuth",
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("Security:BearerAuth", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
if ok {
|
||||
satisfied[0] |= 1 << 0
|
||||
ctx = sctx
|
||||
}
|
||||
}
|
||||
|
||||
if ok := func() bool {
|
||||
nextRequirement:
|
||||
for _, requirement := range []bitset{
|
||||
{0b00000001},
|
||||
} {
|
||||
for i, mask := range requirement {
|
||||
if satisfied[i]&mask != mask {
|
||||
continue nextRequirement
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}(); !ok {
|
||||
err = &ogenerrors.SecurityError{
|
||||
OperationContext: opErrContext,
|
||||
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
|
||||
}
|
||||
defer recordError("Security", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
params, err := decodeDownloadAttachmentParams(args, argsEscaped, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeParamsError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeParams", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var rawBody []byte
|
||||
|
||||
var response DownloadAttachmentRes
|
||||
if m := s.cfg.Middleware; m != nil {
|
||||
mreq := middleware.Request{
|
||||
Context: ctx,
|
||||
OperationName: DownloadAttachmentOperation,
|
||||
OperationSummary: "Скачать байты через авторизованный API",
|
||||
OperationID: "downloadAttachment",
|
||||
Body: nil,
|
||||
RawBody: rawBody,
|
||||
Params: middleware.Parameters{
|
||||
{
|
||||
Name: "attachment_id",
|
||||
In: "path",
|
||||
}: params.AttachmentID,
|
||||
{
|
||||
Name: "variant",
|
||||
In: "query",
|
||||
}: params.Variant,
|
||||
},
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
type (
|
||||
Request = struct{}
|
||||
Params = DownloadAttachmentParams
|
||||
Response = DownloadAttachmentRes
|
||||
)
|
||||
response, err = middleware.HookMiddleware[
|
||||
Request,
|
||||
Params,
|
||||
Response,
|
||||
](
|
||||
m,
|
||||
mreq,
|
||||
unpackDownloadAttachmentParams,
|
||||
func(ctx context.Context, request Request, params Params) (response Response, err error) {
|
||||
response, err = s.h.DownloadAttachment(ctx, params)
|
||||
return response, err
|
||||
},
|
||||
)
|
||||
} else {
|
||||
response, err = s.h.DownloadAttachment(ctx, params)
|
||||
}
|
||||
if err != nil {
|
||||
defer recordError("Internal", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeDownloadAttachmentResponse(response, w); err != nil {
|
||||
defer recordError("EncodeResponse", err)
|
||||
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleGetAttachmentRequest handles getAttachment operation.
|
||||
//
|
||||
// Метаданные вложения, доступ участникам чата.
|
||||
//
|
||||
// GET /api/v1/attachments/{attachment_id}
|
||||
func (s *Server) handleGetAttachmentRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
statusWriter := &codeRecorder{ResponseWriter: w}
|
||||
w = statusWriter
|
||||
ctx := r.Context()
|
||||
|
||||
var (
|
||||
err error
|
||||
opErrContext = ogenerrors.OperationContext{
|
||||
Name: GetAttachmentOperation,
|
||||
ID: "getAttachment",
|
||||
}
|
||||
)
|
||||
{
|
||||
type bitset = [1]uint8
|
||||
var satisfied bitset
|
||||
{
|
||||
sctx, ok, err := s.securityBearerAuth(ctx, GetAttachmentOperation, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.SecurityError{
|
||||
OperationContext: opErrContext,
|
||||
Security: "BearerAuth",
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("Security:BearerAuth", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
if ok {
|
||||
satisfied[0] |= 1 << 0
|
||||
ctx = sctx
|
||||
}
|
||||
}
|
||||
|
||||
if ok := func() bool {
|
||||
nextRequirement:
|
||||
for _, requirement := range []bitset{
|
||||
{0b00000001},
|
||||
} {
|
||||
for i, mask := range requirement {
|
||||
if satisfied[i]&mask != mask {
|
||||
continue nextRequirement
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}(); !ok {
|
||||
err = &ogenerrors.SecurityError{
|
||||
OperationContext: opErrContext,
|
||||
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
|
||||
}
|
||||
defer recordError("Security", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
params, err := decodeGetAttachmentParams(args, argsEscaped, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeParamsError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeParams", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var rawBody []byte
|
||||
|
||||
var response GetAttachmentRes
|
||||
if m := s.cfg.Middleware; m != nil {
|
||||
mreq := middleware.Request{
|
||||
Context: ctx,
|
||||
OperationName: GetAttachmentOperation,
|
||||
OperationSummary: "Метаданные вложения, доступ участникам чата",
|
||||
OperationID: "getAttachment",
|
||||
Body: nil,
|
||||
RawBody: rawBody,
|
||||
Params: middleware.Parameters{
|
||||
{
|
||||
Name: "attachment_id",
|
||||
In: "path",
|
||||
}: params.AttachmentID,
|
||||
},
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
type (
|
||||
Request = struct{}
|
||||
Params = GetAttachmentParams
|
||||
Response = GetAttachmentRes
|
||||
)
|
||||
response, err = middleware.HookMiddleware[
|
||||
Request,
|
||||
Params,
|
||||
Response,
|
||||
](
|
||||
m,
|
||||
mreq,
|
||||
unpackGetAttachmentParams,
|
||||
func(ctx context.Context, request Request, params Params) (response Response, err error) {
|
||||
response, err = s.h.GetAttachment(ctx, params)
|
||||
return response, err
|
||||
},
|
||||
)
|
||||
} else {
|
||||
response, err = s.h.GetAttachment(ctx, params)
|
||||
}
|
||||
if err != nil {
|
||||
defer recordError("Internal", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeGetAttachmentResponse(response, w); err != nil {
|
||||
defer recordError("EncodeResponse", err)
|
||||
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleGetChatRequest handles getChat operation.
|
||||
//
|
||||
// Получить доступный чат.
|
||||
@@ -1251,3 +1511,150 @@ func (s *Server) handleRevokeSessionRequest(args [0]string, argsEscaped bool, w
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleUploadAttachmentRequest handles uploadAttachment operation.
|
||||
//
|
||||
// Принять один PNG/JPEG, до 10 MiB.
|
||||
//
|
||||
// POST /api/v1/chats/{chat_id}/attachments
|
||||
func (s *Server) handleUploadAttachmentRequest(args [1]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
statusWriter := &codeRecorder{ResponseWriter: w}
|
||||
w = statusWriter
|
||||
ctx := r.Context()
|
||||
|
||||
var (
|
||||
err error
|
||||
opErrContext = ogenerrors.OperationContext{
|
||||
Name: UploadAttachmentOperation,
|
||||
ID: "uploadAttachment",
|
||||
}
|
||||
)
|
||||
{
|
||||
type bitset = [1]uint8
|
||||
var satisfied bitset
|
||||
{
|
||||
sctx, ok, err := s.securityBearerAuth(ctx, UploadAttachmentOperation, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.SecurityError{
|
||||
OperationContext: opErrContext,
|
||||
Security: "BearerAuth",
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("Security:BearerAuth", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
if ok {
|
||||
satisfied[0] |= 1 << 0
|
||||
ctx = sctx
|
||||
}
|
||||
}
|
||||
|
||||
if ok := func() bool {
|
||||
nextRequirement:
|
||||
for _, requirement := range []bitset{
|
||||
{0b00000001},
|
||||
} {
|
||||
for i, mask := range requirement {
|
||||
if satisfied[i]&mask != mask {
|
||||
continue nextRequirement
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}(); !ok {
|
||||
err = &ogenerrors.SecurityError{
|
||||
OperationContext: opErrContext,
|
||||
Err: ogenerrors.ErrSecurityRequirementIsNotSatisfied,
|
||||
}
|
||||
defer recordError("Security", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
params, err := decodeUploadAttachmentParams(args, argsEscaped, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeParamsError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeParams", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var rawBody []byte
|
||||
request, rawBody, close, err := s.decodeUploadAttachmentRequest(r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeRequestError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeRequest", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err := close(); err != nil {
|
||||
recordError("CloseRequest", err)
|
||||
}
|
||||
}()
|
||||
|
||||
var response UploadAttachmentRes
|
||||
if m := s.cfg.Middleware; m != nil {
|
||||
mreq := middleware.Request{
|
||||
Context: ctx,
|
||||
OperationName: UploadAttachmentOperation,
|
||||
OperationSummary: "Принять один PNG/JPEG, до 10 MiB",
|
||||
OperationID: "uploadAttachment",
|
||||
Body: request,
|
||||
RawBody: rawBody,
|
||||
Params: middleware.Parameters{
|
||||
{
|
||||
Name: "chat_id",
|
||||
In: "path",
|
||||
}: params.ChatID,
|
||||
{
|
||||
Name: "filename",
|
||||
In: "query",
|
||||
}: params.Filename,
|
||||
},
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
type (
|
||||
Request = UploadAttachmentReq
|
||||
Params = UploadAttachmentParams
|
||||
Response = UploadAttachmentRes
|
||||
)
|
||||
response, err = middleware.HookMiddleware[
|
||||
Request,
|
||||
Params,
|
||||
Response,
|
||||
](
|
||||
m,
|
||||
mreq,
|
||||
unpackUploadAttachmentParams,
|
||||
func(ctx context.Context, request Request, params Params) (response Response, err error) {
|
||||
response, err = s.h.UploadAttachment(ctx, request, params)
|
||||
return response, err
|
||||
},
|
||||
)
|
||||
} else {
|
||||
response, err = s.h.UploadAttachment(ctx, request, params)
|
||||
}
|
||||
if err != nil {
|
||||
defer recordError("Internal", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeUploadAttachmentResponse(response, w); err != nil {
|
||||
defer recordError("EncodeResponse", err)
|
||||
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,14 @@ type CurrentUserRes interface {
|
||||
currentUserRes()
|
||||
}
|
||||
|
||||
type DownloadAttachmentRes interface {
|
||||
downloadAttachmentRes()
|
||||
}
|
||||
|
||||
type GetAttachmentRes interface {
|
||||
getAttachmentRes()
|
||||
}
|
||||
|
||||
type GetChatRes interface {
|
||||
getChatRes()
|
||||
}
|
||||
@@ -44,3 +52,11 @@ type RegisterUserRes interface {
|
||||
type RevokeSessionRes interface {
|
||||
revokeSessionRes()
|
||||
}
|
||||
|
||||
type UploadAttachmentReq interface {
|
||||
uploadAttachmentReq()
|
||||
}
|
||||
|
||||
type UploadAttachmentRes interface {
|
||||
uploadAttachmentRes()
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,15 +6,18 @@ package oas
|
||||
type OperationName = string
|
||||
|
||||
const (
|
||||
CreateChatOperation OperationName = "CreateChat"
|
||||
CreateMessageOperation OperationName = "CreateMessage"
|
||||
CreateSessionOperation OperationName = "CreateSession"
|
||||
CurrentUserOperation OperationName = "CurrentUser"
|
||||
GetChatOperation OperationName = "GetChat"
|
||||
ListChatsOperation OperationName = "ListChats"
|
||||
ListMessagesOperation OperationName = "ListMessages"
|
||||
LivenessOperation OperationName = "Liveness"
|
||||
ReadinessOperation OperationName = "Readiness"
|
||||
RegisterUserOperation OperationName = "RegisterUser"
|
||||
RevokeSessionOperation OperationName = "RevokeSession"
|
||||
CreateChatOperation OperationName = "CreateChat"
|
||||
CreateMessageOperation OperationName = "CreateMessage"
|
||||
CreateSessionOperation OperationName = "CreateSession"
|
||||
CurrentUserOperation OperationName = "CurrentUser"
|
||||
DownloadAttachmentOperation OperationName = "DownloadAttachment"
|
||||
GetAttachmentOperation OperationName = "GetAttachment"
|
||||
GetChatOperation OperationName = "GetChat"
|
||||
ListChatsOperation OperationName = "ListChats"
|
||||
ListMessagesOperation OperationName = "ListMessages"
|
||||
LivenessOperation OperationName = "Liveness"
|
||||
ReadinessOperation OperationName = "Readiness"
|
||||
RegisterUserOperation OperationName = "RegisterUser"
|
||||
RevokeSessionOperation OperationName = "RevokeSession"
|
||||
UploadAttachmentOperation OperationName = "UploadAttachment"
|
||||
)
|
||||
|
||||
@@ -147,6 +147,208 @@ func decodeCreateMessageParams(args [1]string, argsEscaped bool, r *http.Request
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// DownloadAttachmentParams is parameters of downloadAttachment operation.
|
||||
type DownloadAttachmentParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
Variant OptDownloadAttachmentVariant `json:",omitempty,omitzero"`
|
||||
}
|
||||
|
||||
func unpackDownloadAttachmentParams(packed middleware.Parameters) (params DownloadAttachmentParams) {
|
||||
{
|
||||
key := middleware.ParameterKey{
|
||||
Name: "attachment_id",
|
||||
In: "path",
|
||||
}
|
||||
params.AttachmentID = packed[key].(uuid.UUID)
|
||||
}
|
||||
{
|
||||
key := middleware.ParameterKey{
|
||||
Name: "variant",
|
||||
In: "query",
|
||||
}
|
||||
if v, ok := packed[key]; ok {
|
||||
params.Variant = v.(OptDownloadAttachmentVariant)
|
||||
}
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
func decodeDownloadAttachmentParams(args [1]string, argsEscaped bool, r *http.Request) (params DownloadAttachmentParams, _ error) {
|
||||
q := uri.NewQueryDecoder(r.URL.Query())
|
||||
// Decode path: attachment_id.
|
||||
if err := func() error {
|
||||
param := args[0]
|
||||
if argsEscaped {
|
||||
unescaped, err := url.PathUnescape(args[0])
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unescape path")
|
||||
}
|
||||
param = unescaped
|
||||
}
|
||||
if len(param) > 0 {
|
||||
d := uri.NewPathDecoder(uri.PathDecoderConfig{
|
||||
Param: "attachment_id",
|
||||
Value: param,
|
||||
Style: uri.PathStyleSimple,
|
||||
Explode: false,
|
||||
})
|
||||
|
||||
if err := func() error {
|
||||
val, err := d.DecodeValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := conv.ToUUID(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params.AttachmentID = c
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return validate.ErrFieldRequired
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return params, &ogenerrors.DecodeParamError{
|
||||
Name: "attachment_id",
|
||||
In: "path",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
// Set default value for query: variant.
|
||||
{
|
||||
val := DownloadAttachmentVariant("original")
|
||||
params.Variant.SetTo(val)
|
||||
}
|
||||
// Decode query: variant.
|
||||
if err := func() error {
|
||||
cfg := uri.QueryParameterDecodingConfig{
|
||||
Name: "variant",
|
||||
Style: uri.QueryStyleForm,
|
||||
Explode: true,
|
||||
}
|
||||
|
||||
if err := q.HasParam(cfg); err == nil {
|
||||
if err := q.DecodeParam(cfg, func(d uri.Decoder) error {
|
||||
var paramsDotVariantVal DownloadAttachmentVariant
|
||||
if err := func() error {
|
||||
val, err := d.DecodeValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := conv.ToString(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
paramsDotVariantVal = DownloadAttachmentVariant(c)
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
params.Variant.SetTo(paramsDotVariantVal)
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := func() error {
|
||||
if value, ok := params.Variant.Get(); ok {
|
||||
if err := func() error {
|
||||
if err := value.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return params, &ogenerrors.DecodeParamError{
|
||||
Name: "variant",
|
||||
In: "query",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetAttachmentParams is parameters of getAttachment operation.
|
||||
type GetAttachmentParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
}
|
||||
|
||||
func unpackGetAttachmentParams(packed middleware.Parameters) (params GetAttachmentParams) {
|
||||
{
|
||||
key := middleware.ParameterKey{
|
||||
Name: "attachment_id",
|
||||
In: "path",
|
||||
}
|
||||
params.AttachmentID = packed[key].(uuid.UUID)
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
func decodeGetAttachmentParams(args [1]string, argsEscaped bool, r *http.Request) (params GetAttachmentParams, _ error) {
|
||||
// Decode path: attachment_id.
|
||||
if err := func() error {
|
||||
param := args[0]
|
||||
if argsEscaped {
|
||||
unescaped, err := url.PathUnescape(args[0])
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unescape path")
|
||||
}
|
||||
param = unescaped
|
||||
}
|
||||
if len(param) > 0 {
|
||||
d := uri.NewPathDecoder(uri.PathDecoderConfig{
|
||||
Param: "attachment_id",
|
||||
Value: param,
|
||||
Style: uri.PathStyleSimple,
|
||||
Explode: false,
|
||||
})
|
||||
|
||||
if err := func() error {
|
||||
val, err := d.DecodeValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := conv.ToUUID(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params.AttachmentID = c
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return validate.ErrFieldRequired
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return params, &ogenerrors.DecodeParamError{
|
||||
Name: "attachment_id",
|
||||
In: "path",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetChatParams is parameters of getChat operation.
|
||||
type GetChatParams struct {
|
||||
ChatID uuid.UUID
|
||||
@@ -608,3 +810,133 @@ func decodeListMessagesParams(args [1]string, argsEscaped bool, r *http.Request)
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// UploadAttachmentParams is parameters of uploadAttachment operation.
|
||||
type UploadAttachmentParams struct {
|
||||
ChatID uuid.UUID
|
||||
Filename string
|
||||
}
|
||||
|
||||
func unpackUploadAttachmentParams(packed middleware.Parameters) (params UploadAttachmentParams) {
|
||||
{
|
||||
key := middleware.ParameterKey{
|
||||
Name: "chat_id",
|
||||
In: "path",
|
||||
}
|
||||
params.ChatID = packed[key].(uuid.UUID)
|
||||
}
|
||||
{
|
||||
key := middleware.ParameterKey{
|
||||
Name: "filename",
|
||||
In: "query",
|
||||
}
|
||||
params.Filename = packed[key].(string)
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
func decodeUploadAttachmentParams(args [1]string, argsEscaped bool, r *http.Request) (params UploadAttachmentParams, _ error) {
|
||||
q := uri.NewQueryDecoder(r.URL.Query())
|
||||
// Decode path: chat_id.
|
||||
if err := func() error {
|
||||
param := args[0]
|
||||
if argsEscaped {
|
||||
unescaped, err := url.PathUnescape(args[0])
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unescape path")
|
||||
}
|
||||
param = unescaped
|
||||
}
|
||||
if len(param) > 0 {
|
||||
d := uri.NewPathDecoder(uri.PathDecoderConfig{
|
||||
Param: "chat_id",
|
||||
Value: param,
|
||||
Style: uri.PathStyleSimple,
|
||||
Explode: false,
|
||||
})
|
||||
|
||||
if err := func() error {
|
||||
val, err := d.DecodeValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := conv.ToUUID(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params.ChatID = c
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return validate.ErrFieldRequired
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return params, &ogenerrors.DecodeParamError{
|
||||
Name: "chat_id",
|
||||
In: "path",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
// Decode query: filename.
|
||||
if err := func() error {
|
||||
cfg := uri.QueryParameterDecodingConfig{
|
||||
Name: "filename",
|
||||
Style: uri.QueryStyleForm,
|
||||
Explode: true,
|
||||
}
|
||||
|
||||
if err := q.HasParam(cfg); err == nil {
|
||||
if err := q.DecodeParam(cfg, func(d uri.Decoder) error {
|
||||
val, err := d.DecodeValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := conv.ToString(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params.Filename = c
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := func() error {
|
||||
if err := (validate.String{
|
||||
MinLength: 1,
|
||||
MinLengthSet: true,
|
||||
MaxLength: 255,
|
||||
MaxLengthSet: true,
|
||||
Email: false,
|
||||
Hostname: false,
|
||||
Regex: nil,
|
||||
MinNumeric: 0,
|
||||
MinNumericSet: false,
|
||||
MaxNumeric: 0,
|
||||
MaxNumericSet: false,
|
||||
}).Validate(string(params.Filename)); err != nil {
|
||||
return errors.Wrap(err, "string")
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return params, &ogenerrors.DecodeParamError{
|
||||
Name: "filename",
|
||||
In: "query",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
@@ -250,3 +250,42 @@ func (s *Server) decodeRegisterUserRequest(r *http.Request) (
|
||||
return req, rawBody, close, validate.InvalidContentType(ct)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) decodeUploadAttachmentRequest(r *http.Request) (
|
||||
req UploadAttachmentReq,
|
||||
rawBody []byte,
|
||||
close func() error,
|
||||
rerr error,
|
||||
) {
|
||||
var closers []func() error
|
||||
close = func() error {
|
||||
var merr error
|
||||
// Close in reverse order, to match defer behavior.
|
||||
for i := len(closers) - 1; i >= 0; i-- {
|
||||
c := closers[i]
|
||||
merr = errors.Join(merr, c())
|
||||
}
|
||||
return merr
|
||||
}
|
||||
defer func() {
|
||||
if rerr != nil {
|
||||
rerr = errors.Join(rerr, close())
|
||||
}
|
||||
}()
|
||||
ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
return req, rawBody, close, errors.Wrap(err, "parse media type")
|
||||
}
|
||||
switch {
|
||||
case ct == "image/jpeg":
|
||||
reader := r.Body
|
||||
request := UploadAttachmentReqImageJpeg{Data: reader}
|
||||
return &request, rawBody, close, nil
|
||||
case ct == "image/png":
|
||||
reader := r.Body
|
||||
request := UploadAttachmentReqImagePNG{Data: reader}
|
||||
return &request, rawBody, close, nil
|
||||
default:
|
||||
return req, rawBody, close, validate.InvalidContentType(ct)
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -11,10 +11,16 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
rn10AllowedHeaders = map[string]string{
|
||||
"GET": "Authorization",
|
||||
}
|
||||
rn11AllowedHeaders = map[string]string{
|
||||
"GET": "Authorization",
|
||||
}
|
||||
rn6AllowedHeaders = map[string]string{
|
||||
"POST": "Authorization",
|
||||
}
|
||||
rn13AllowedHeaders = map[string]string{
|
||||
rn17AllowedHeaders = map[string]string{
|
||||
"DELETE": "Authorization",
|
||||
}
|
||||
rn1AllowedHeaders = map[string]string{
|
||||
@@ -24,11 +30,14 @@ var (
|
||||
rn3AllowedHeaders = map[string]string{
|
||||
"GET": "Authorization",
|
||||
}
|
||||
rn19AllowedHeaders = map[string]string{
|
||||
"POST": "Authorization,Content-Type",
|
||||
}
|
||||
rn4AllowedHeaders = map[string]string{
|
||||
"GET": "Authorization",
|
||||
"POST": "Authorization,Content-Type,Idempotency-Key",
|
||||
}
|
||||
rn12AllowedHeaders = map[string]string{
|
||||
rn16AllowedHeaders = map[string]string{
|
||||
"POST": "Content-Type",
|
||||
}
|
||||
rn7AllowedHeaders = map[string]string{
|
||||
@@ -99,47 +108,45 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
break
|
||||
}
|
||||
switch elem[0] {
|
||||
case 'a': // Prefix: "auth/sessions"
|
||||
case 'a': // Prefix: "a"
|
||||
|
||||
if l := len("auth/sessions"); len(elem) >= l && elem[0:l] == "auth/sessions" {
|
||||
if l := len("a"); len(elem) >= l && elem[0:l] == "a" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
s.handleCreateSessionRequest([0]string{}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "POST",
|
||||
allowedHeaders: rn6AllowedHeaders,
|
||||
acceptPost: "",
|
||||
acceptPatch: "",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
break
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/current"
|
||||
case 't': // Prefix: "ttachments/"
|
||||
|
||||
if l := len("/current"); len(elem) >= l && elem[0:l] == "/current" {
|
||||
if l := len("ttachments/"); len(elem) >= l && elem[0:l] == "ttachments/" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
// Param: "attachment_id"
|
||||
// Match until "/"
|
||||
idx := strings.IndexByte(elem, '/')
|
||||
if idx < 0 {
|
||||
idx = len(elem)
|
||||
}
|
||||
args[0] = elem[:idx]
|
||||
elem = elem[idx:]
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch r.Method {
|
||||
case "DELETE":
|
||||
s.handleRevokeSessionRequest([0]string{}, elemIsEscaped, w, r)
|
||||
case "GET":
|
||||
s.handleGetAttachmentRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "DELETE",
|
||||
allowedHeaders: rn13AllowedHeaders,
|
||||
allowedMethods: "GET",
|
||||
allowedHeaders: rn10AllowedHeaders,
|
||||
acceptPost: "",
|
||||
acceptPatch: "",
|
||||
})
|
||||
@@ -147,6 +154,86 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
return
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/content"
|
||||
|
||||
if l := len("/content"); len(elem) >= l && elem[0:l] == "/content" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
s.handleDownloadAttachmentRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "GET",
|
||||
allowedHeaders: rn11AllowedHeaders,
|
||||
acceptPost: "",
|
||||
acceptPatch: "",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case 'u': // Prefix: "uth/sessions"
|
||||
|
||||
if l := len("uth/sessions"); len(elem) >= l && elem[0:l] == "uth/sessions" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
s.handleCreateSessionRequest([0]string{}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "POST",
|
||||
allowedHeaders: rn6AllowedHeaders,
|
||||
acceptPost: "",
|
||||
acceptPatch: "",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/current"
|
||||
|
||||
if l := len("/current"); len(elem) >= l && elem[0:l] == "/current" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch r.Method {
|
||||
case "DELETE":
|
||||
s.handleRevokeSessionRequest([0]string{}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "DELETE",
|
||||
allowedHeaders: rn17AllowedHeaders,
|
||||
acceptPost: "",
|
||||
acceptPatch: "",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -211,35 +298,76 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/messages"
|
||||
case '/': // Prefix: "/"
|
||||
|
||||
if l := len("/messages"); len(elem) >= l && elem[0:l] == "/messages" {
|
||||
if l := len("/"); len(elem) >= l && elem[0:l] == "/" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
s.handleListMessagesRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
case "POST":
|
||||
s.handleCreateMessageRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "GET,POST",
|
||||
allowedHeaders: rn4AllowedHeaders,
|
||||
acceptPost: "application/json",
|
||||
acceptPatch: "",
|
||||
})
|
||||
break
|
||||
}
|
||||
switch elem[0] {
|
||||
case 'a': // Prefix: "attachments"
|
||||
|
||||
if l := len("attachments"); len(elem) >= l && elem[0:l] == "attachments" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
s.handleUploadAttachmentRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "POST",
|
||||
allowedHeaders: rn19AllowedHeaders,
|
||||
acceptPost: "image/jpeg,image/png",
|
||||
acceptPatch: "",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
case 'm': // Prefix: "messages"
|
||||
|
||||
if l := len("messages"); len(elem) >= l && elem[0:l] == "messages" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
s.handleListMessagesRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
case "POST":
|
||||
s.handleCreateMessageRequest([1]string{
|
||||
args[0],
|
||||
}, elemIsEscaped, w, r)
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "GET,POST",
|
||||
allowedHeaders: rn4AllowedHeaders,
|
||||
acceptPost: "application/json",
|
||||
acceptPatch: "",
|
||||
})
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
@@ -261,7 +389,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
default:
|
||||
s.notAllowed(w, r, notAllowedParams{
|
||||
allowedMethods: "POST",
|
||||
allowedHeaders: rn12AllowedHeaders,
|
||||
allowedHeaders: rn16AllowedHeaders,
|
||||
acceptPost: "application/json",
|
||||
acceptPatch: "",
|
||||
})
|
||||
@@ -475,47 +603,94 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) {
|
||||
break
|
||||
}
|
||||
switch elem[0] {
|
||||
case 'a': // Prefix: "auth/sessions"
|
||||
case 'a': // Prefix: "a"
|
||||
|
||||
if l := len("auth/sessions"); len(elem) >= l && elem[0:l] == "auth/sessions" {
|
||||
if l := len("a"); len(elem) >= l && elem[0:l] == "a" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
switch method {
|
||||
case "POST":
|
||||
r.name = CreateSessionOperation
|
||||
r.summary = "Вход: Basic username:password; выдаёт сессию"
|
||||
r.operationID = "createSession"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/auth/sessions"
|
||||
r.args = args
|
||||
r.count = 0
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/current"
|
||||
case 't': // Prefix: "ttachments/"
|
||||
|
||||
if l := len("/current"); len(elem) >= l && elem[0:l] == "/current" {
|
||||
if l := len("ttachments/"); len(elem) >= l && elem[0:l] == "ttachments/" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
// Param: "attachment_id"
|
||||
// Match until "/"
|
||||
idx := strings.IndexByte(elem, '/')
|
||||
if idx < 0 {
|
||||
idx = len(elem)
|
||||
}
|
||||
args[0] = elem[:idx]
|
||||
elem = elem[idx:]
|
||||
|
||||
if len(elem) == 0 {
|
||||
switch method {
|
||||
case "GET":
|
||||
r.name = GetAttachmentOperation
|
||||
r.summary = "Метаданные вложения, доступ участникам чата"
|
||||
r.operationID = "getAttachment"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/attachments/{attachment_id}"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/content"
|
||||
|
||||
if l := len("/content"); len(elem) >= l && elem[0:l] == "/content" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch method {
|
||||
case "GET":
|
||||
r.name = DownloadAttachmentOperation
|
||||
r.summary = "Скачать байты через авторизованный API"
|
||||
r.operationID = "downloadAttachment"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/attachments/{attachment_id}/content"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case 'u': // Prefix: "uth/sessions"
|
||||
|
||||
if l := len("uth/sessions"); len(elem) >= l && elem[0:l] == "uth/sessions" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch method {
|
||||
case "DELETE":
|
||||
r.name = RevokeSessionOperation
|
||||
r.summary = "Отозвать текущую сессию"
|
||||
r.operationID = "revokeSession"
|
||||
case "POST":
|
||||
r.name = CreateSessionOperation
|
||||
r.summary = "Вход: Basic username:password; выдаёт сессию"
|
||||
r.operationID = "createSession"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/auth/sessions/current"
|
||||
r.pathPattern = "/api/v1/auth/sessions"
|
||||
r.args = args
|
||||
r.count = 0
|
||||
return r, true
|
||||
@@ -523,6 +698,33 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) {
|
||||
return
|
||||
}
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/current"
|
||||
|
||||
if l := len("/current"); len(elem) >= l && elem[0:l] == "/current" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch method {
|
||||
case "DELETE":
|
||||
r.name = RevokeSessionOperation
|
||||
r.summary = "Отозвать текущую сессию"
|
||||
r.operationID = "revokeSession"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/auth/sessions/current"
|
||||
r.args = args
|
||||
r.count = 0
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -592,38 +794,77 @@ func (s *Server) FindPath(method string, u *url.URL) (r Route, _ bool) {
|
||||
}
|
||||
}
|
||||
switch elem[0] {
|
||||
case '/': // Prefix: "/messages"
|
||||
case '/': // Prefix: "/"
|
||||
|
||||
if l := len("/messages"); len(elem) >= l && elem[0:l] == "/messages" {
|
||||
if l := len("/"); len(elem) >= l && elem[0:l] == "/" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch method {
|
||||
case "GET":
|
||||
r.name = ListMessagesOperation
|
||||
r.summary = "Сообщения по (created_at, id) по возрастанию"
|
||||
r.operationID = "listMessages"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/chats/{chat_id}/messages"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
case "POST":
|
||||
r.name = CreateMessageOperation
|
||||
r.summary = "Отправить сообщение"
|
||||
r.operationID = "createMessage"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/chats/{chat_id}/messages"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
break
|
||||
}
|
||||
switch elem[0] {
|
||||
case 'a': // Prefix: "attachments"
|
||||
|
||||
if l := len("attachments"); len(elem) >= l && elem[0:l] == "attachments" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch method {
|
||||
case "POST":
|
||||
r.name = UploadAttachmentOperation
|
||||
r.summary = "Принять один PNG/JPEG, до 10 MiB"
|
||||
r.operationID = "uploadAttachment"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/chats/{chat_id}/attachments"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
case 'm': // Prefix: "messages"
|
||||
|
||||
if l := len("messages"); len(elem) >= l && elem[0:l] == "messages" {
|
||||
elem = elem[l:]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
if len(elem) == 0 {
|
||||
// Leaf node.
|
||||
switch method {
|
||||
case "GET":
|
||||
r.name = ListMessagesOperation
|
||||
r.summary = "Сообщения по (created_at, id) по возрастанию"
|
||||
r.operationID = "listMessages"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/chats/{chat_id}/messages"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
case "POST":
|
||||
r.name = CreateMessageOperation
|
||||
r.summary = "Отправить сообщение"
|
||||
r.operationID = "createMessage"
|
||||
r.operationGroup = ""
|
||||
r.pathPattern = "/api/v1/chats/{chat_id}/messages"
|
||||
r.args = args
|
||||
r.count = 1
|
||||
return r, true
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -62,13 +62,16 @@ func GetRolesForBasicAuth(operation string) []string {
|
||||
|
||||
// operationRolesBearerAuth is a private map storing roles per operation.
|
||||
var operationRolesBearerAuth = map[string][]string{
|
||||
CreateChatOperation: []string{},
|
||||
CreateMessageOperation: []string{},
|
||||
CurrentUserOperation: []string{},
|
||||
GetChatOperation: []string{},
|
||||
ListChatsOperation: []string{},
|
||||
ListMessagesOperation: []string{},
|
||||
RevokeSessionOperation: []string{},
|
||||
CreateChatOperation: []string{},
|
||||
CreateMessageOperation: []string{},
|
||||
CurrentUserOperation: []string{},
|
||||
DownloadAttachmentOperation: []string{},
|
||||
GetAttachmentOperation: []string{},
|
||||
GetChatOperation: []string{},
|
||||
ListChatsOperation: []string{},
|
||||
ListMessagesOperation: []string{},
|
||||
RevokeSessionOperation: []string{},
|
||||
UploadAttachmentOperation: []string{},
|
||||
}
|
||||
|
||||
// GetRolesForBearerAuth returns the required roles for the given operation.
|
||||
|
||||
@@ -32,6 +32,18 @@ type Handler interface {
|
||||
//
|
||||
// GET /api/v1/users/me
|
||||
CurrentUser(ctx context.Context) (CurrentUserRes, error)
|
||||
// DownloadAttachment implements downloadAttachment operation.
|
||||
//
|
||||
// Скачать байты через авторизованный API.
|
||||
//
|
||||
// GET /api/v1/attachments/{attachment_id}/content
|
||||
DownloadAttachment(ctx context.Context, params DownloadAttachmentParams) (DownloadAttachmentRes, error)
|
||||
// GetAttachment implements getAttachment operation.
|
||||
//
|
||||
// Метаданные вложения, доступ участникам чата.
|
||||
//
|
||||
// GET /api/v1/attachments/{attachment_id}
|
||||
GetAttachment(ctx context.Context, params GetAttachmentParams) (GetAttachmentRes, error)
|
||||
// GetChat implements getChat operation.
|
||||
//
|
||||
// Получить доступный чат.
|
||||
@@ -74,6 +86,12 @@ type Handler interface {
|
||||
//
|
||||
// DELETE /api/v1/auth/sessions/current
|
||||
RevokeSession(ctx context.Context) (RevokeSessionRes, error)
|
||||
// UploadAttachment implements uploadAttachment operation.
|
||||
//
|
||||
// Принять один PNG/JPEG, до 10 MiB.
|
||||
//
|
||||
// POST /api/v1/chats/{chat_id}/attachments
|
||||
UploadAttachment(ctx context.Context, req UploadAttachmentReq, params UploadAttachmentParams) (UploadAttachmentRes, error)
|
||||
}
|
||||
|
||||
// Server implements http server based on OpenAPI v3 specification and
|
||||
|
||||
@@ -49,6 +49,24 @@ func (UnimplementedHandler) CurrentUser(ctx context.Context) (r CurrentUserRes,
|
||||
return r, ht.ErrNotImplemented
|
||||
}
|
||||
|
||||
// DownloadAttachment implements downloadAttachment operation.
|
||||
//
|
||||
// Скачать байты через авторизованный API.
|
||||
//
|
||||
// GET /api/v1/attachments/{attachment_id}/content
|
||||
func (UnimplementedHandler) DownloadAttachment(ctx context.Context, params DownloadAttachmentParams) (r DownloadAttachmentRes, _ error) {
|
||||
return r, ht.ErrNotImplemented
|
||||
}
|
||||
|
||||
// GetAttachment implements getAttachment operation.
|
||||
//
|
||||
// Метаданные вложения, доступ участникам чата.
|
||||
//
|
||||
// GET /api/v1/attachments/{attachment_id}
|
||||
func (UnimplementedHandler) GetAttachment(ctx context.Context, params GetAttachmentParams) (r GetAttachmentRes, _ error) {
|
||||
return r, ht.ErrNotImplemented
|
||||
}
|
||||
|
||||
// GetChat implements getChat operation.
|
||||
//
|
||||
// Получить доступный чат.
|
||||
@@ -111,3 +129,12 @@ func (UnimplementedHandler) RegisterUser(ctx context.Context, req *CreateUser) (
|
||||
func (UnimplementedHandler) RevokeSession(ctx context.Context) (r RevokeSessionRes, _ error) {
|
||||
return r, ht.ErrNotImplemented
|
||||
}
|
||||
|
||||
// UploadAttachment implements uploadAttachment operation.
|
||||
//
|
||||
// Принять один PNG/JPEG, до 10 MiB.
|
||||
//
|
||||
// POST /api/v1/chats/{chat_id}/attachments
|
||||
func (UnimplementedHandler) UploadAttachment(ctx context.Context, req UploadAttachmentReq, params UploadAttachmentParams) (r UploadAttachmentRes, _ error) {
|
||||
return r, ht.ErrNotImplemented
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,413 @@
|
||||
// Code generated by mockery; DO NOT EDIT.
|
||||
// github.com/vektra/mockery
|
||||
// template: testify
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/service/attachment"
|
||||
|
||||
"github.com/google/uuid"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NewMockAttachmentService creates a new instance of MockAttachmentService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockAttachmentService(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockAttachmentService {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
|
||||
mock := &MockAttachmentService{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
mock.AssertExpectations(t)
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockAttachmentService is an autogenerated mock type for the AttachmentService type
|
||||
type MockAttachmentService struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockAttachmentService_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockAttachmentService) EXPECT() *MockAttachmentService_Expecter {
|
||||
return &MockAttachmentService_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// Complete provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) Complete(ctx context.Context, ownerID uuid.UUID, id uuid.UUID, sha256 string) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, ownerID, id, sha256)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Complete")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID, string) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, ownerID, id, sha256)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID, string) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, ownerID, id, sha256)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID, string) error); ok {
|
||||
r1 = returnFunc(ctx, ownerID, id, sha256)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockAttachmentService_Complete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Complete'
|
||||
type MockAttachmentService_Complete_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Complete is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - ownerID uuid.UUID
|
||||
// - id uuid.UUID
|
||||
// - sha256 string
|
||||
func (_e *MockAttachmentService_Expecter) Complete(ctx any, ownerID any, id any, sha256 any) *MockAttachmentService_Complete_Call {
|
||||
return &MockAttachmentService_Complete_Call{Call: _e.mock.On("Complete", ctx, ownerID, id, sha256)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Complete_Call) Run(run func(ctx context.Context, ownerID uuid.UUID, id uuid.UUID, sha256 string)) *MockAttachmentService_Complete_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
var arg3 string
|
||||
if args[3] != nil {
|
||||
arg3 = args[3].(string)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
arg3,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Complete_Call) Return(attachment domain.Attachment, err error) *MockAttachmentService_Complete_Call {
|
||||
_c.Call.Return(attachment, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Complete_Call) RunAndReturn(run func(ctx context.Context, ownerID uuid.UUID, id uuid.UUID, sha256 string) (domain.Attachment, error)) *MockAttachmentService_Complete_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// Get provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) Get(ctx context.Context, callerID uuid.UUID, id uuid.UUID) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, callerID, id)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Get")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, callerID, id)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, callerID, id)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r1 = returnFunc(ctx, callerID, id)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockAttachmentService_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get'
|
||||
type MockAttachmentService_Get_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Get is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - callerID uuid.UUID
|
||||
// - id uuid.UUID
|
||||
func (_e *MockAttachmentService_Expecter) Get(ctx any, callerID any, id any) *MockAttachmentService_Get_Call {
|
||||
return &MockAttachmentService_Get_Call{Call: _e.mock.On("Get", ctx, callerID, id)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Get_Call) Run(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID)) *MockAttachmentService_Get_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Get_Call) Return(attachment domain.Attachment, err error) *MockAttachmentService_Get_Call {
|
||||
_c.Call.Return(attachment, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Get_Call) RunAndReturn(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID) (domain.Attachment, error)) *MockAttachmentService_Get_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// Open provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) Open(ctx context.Context, callerID uuid.UUID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error) {
|
||||
ret := _mock.Called(ctx, callerID, id)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Open")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 io.ReadCloser
|
||||
var r2 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) (domain.Attachment, io.ReadCloser, error)); ok {
|
||||
return returnFunc(ctx, callerID, id)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, uuid.UUID) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, callerID, id)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, uuid.UUID) io.ReadCloser); ok {
|
||||
r1 = returnFunc(ctx, callerID, id)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(io.ReadCloser)
|
||||
}
|
||||
}
|
||||
if returnFunc, ok := ret.Get(2).(func(context.Context, uuid.UUID, uuid.UUID) error); ok {
|
||||
r2 = returnFunc(ctx, callerID, id)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// MockAttachmentService_Open_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Open'
|
||||
type MockAttachmentService_Open_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Open is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - callerID uuid.UUID
|
||||
// - id uuid.UUID
|
||||
func (_e *MockAttachmentService_Expecter) Open(ctx any, callerID any, id any) *MockAttachmentService_Open_Call {
|
||||
return &MockAttachmentService_Open_Call{Call: _e.mock.On("Open", ctx, callerID, id)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Open_Call) Run(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID)) *MockAttachmentService_Open_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 uuid.UUID
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(uuid.UUID)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Open_Call) Return(attachment domain.Attachment, readCloser io.ReadCloser, err error) *MockAttachmentService_Open_Call {
|
||||
_c.Call.Return(attachment, readCloser, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Open_Call) RunAndReturn(run func(ctx context.Context, callerID uuid.UUID, id uuid.UUID) (domain.Attachment, io.ReadCloser, error)) *MockAttachmentService_Open_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// Presign provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) Presign(ctx context.Context, request domain.PresignRequest) (domain.PresignResult, error) {
|
||||
ret := _mock.Called(ctx, request)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Presign")
|
||||
}
|
||||
|
||||
var r0 domain.PresignResult
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.PresignRequest) (domain.PresignResult, error)); ok {
|
||||
return returnFunc(ctx, request)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, domain.PresignRequest) domain.PresignResult); ok {
|
||||
r0 = returnFunc(ctx, request)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.PresignResult)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, domain.PresignRequest) error); ok {
|
||||
r1 = returnFunc(ctx, request)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockAttachmentService_Presign_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Presign'
|
||||
type MockAttachmentService_Presign_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Presign is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - request domain.PresignRequest
|
||||
func (_e *MockAttachmentService_Expecter) Presign(ctx any, request any) *MockAttachmentService_Presign_Call {
|
||||
return &MockAttachmentService_Presign_Call{Call: _e.mock.On("Presign", ctx, request)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Presign_Call) Run(run func(ctx context.Context, request domain.PresignRequest)) *MockAttachmentService_Presign_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 domain.PresignRequest
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(domain.PresignRequest)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Presign_Call) Return(presignResult domain.PresignResult, err error) *MockAttachmentService_Presign_Call {
|
||||
_c.Call.Return(presignResult, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Presign_Call) RunAndReturn(run func(ctx context.Context, request domain.PresignRequest) (domain.PresignResult, error)) *MockAttachmentService_Presign_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// Upload provides a mock function for the type MockAttachmentService
|
||||
func (_mock *MockAttachmentService) Upload(ctx context.Context, input attachment.UploadInput) (domain.Attachment, error) {
|
||||
ret := _mock.Called(ctx, input)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Upload")
|
||||
}
|
||||
|
||||
var r0 domain.Attachment
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, attachment.UploadInput) (domain.Attachment, error)); ok {
|
||||
return returnFunc(ctx, input)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, attachment.UploadInput) domain.Attachment); ok {
|
||||
r0 = returnFunc(ctx, input)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.Attachment)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, attachment.UploadInput) error); ok {
|
||||
r1 = returnFunc(ctx, input)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockAttachmentService_Upload_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Upload'
|
||||
type MockAttachmentService_Upload_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Upload is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - input attachment.UploadInput
|
||||
func (_e *MockAttachmentService_Expecter) Upload(ctx any, input any) *MockAttachmentService_Upload_Call {
|
||||
return &MockAttachmentService_Upload_Call{Call: _e.mock.On("Upload", ctx, input)}
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Upload_Call) Run(run func(ctx context.Context, input attachment.UploadInput)) *MockAttachmentService_Upload_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 attachment.UploadInput
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(attachment.UploadInput)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Upload_Call) Return(attachment1 domain.Attachment, err error) *MockAttachmentService_Upload_Call {
|
||||
_c.Call.Return(attachment1, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockAttachmentService_Upload_Call) RunAndReturn(run func(ctx context.Context, input attachment.UploadInput) (domain.Attachment, error)) *MockAttachmentService_Upload_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// Code generated by mockery; DO NOT EDIT.
|
||||
// github.com/vektra/mockery
|
||||
// template: testify
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"mtusi-messenger/internal/domain"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NewMockSessionResolver creates a new instance of MockSessionResolver. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockSessionResolver(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockSessionResolver {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
|
||||
mock := &MockSessionResolver{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
if helper, ok := t.(interface{ Helper() }); ok {
|
||||
helper.Helper()
|
||||
}
|
||||
mock.AssertExpectations(t)
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// MockSessionResolver is an autogenerated mock type for the SessionResolver type
|
||||
type MockSessionResolver struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockSessionResolver_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockSessionResolver) EXPECT() *MockSessionResolver_Expecter {
|
||||
return &MockSessionResolver_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// Resolve provides a mock function for the type MockSessionResolver
|
||||
func (_mock *MockSessionResolver) Resolve(ctx context.Context, token string) (domain.User, error) {
|
||||
ret := _mock.Called(ctx, token)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Resolve")
|
||||
}
|
||||
|
||||
var r0 domain.User
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, string) (domain.User, error)); ok {
|
||||
return returnFunc(ctx, token)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, string) domain.User); ok {
|
||||
r0 = returnFunc(ctx, token)
|
||||
} else {
|
||||
r0 = ret.Get(0).(domain.User)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, string) error); ok {
|
||||
r1 = returnFunc(ctx, token)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockSessionResolver_Resolve_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Resolve'
|
||||
type MockSessionResolver_Resolve_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Resolve is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - token string
|
||||
func (_e *MockSessionResolver_Expecter) Resolve(ctx any, token any) *MockSessionResolver_Resolve_Call {
|
||||
return &MockSessionResolver_Resolve_Call{Call: _e.mock.On("Resolve", ctx, token)}
|
||||
}
|
||||
|
||||
func (_c *MockSessionResolver_Resolve_Call) Run(run func(ctx context.Context, token string)) *MockSessionResolver_Resolve_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 string
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(string)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockSessionResolver_Resolve_Call) Return(user domain.User, err error) *MockSessionResolver_Resolve_Call {
|
||||
_c.Call.Return(user, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockSessionResolver_Resolve_Call) RunAndReturn(run func(ctx context.Context, token string) (domain.User, error)) *MockSessionResolver_Resolve_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
@@ -10,8 +10,9 @@ import (
|
||||
func NewRouter(
|
||||
handler oas.Handler,
|
||||
security oas.SecurityHandler,
|
||||
extensions *Extensions,
|
||||
log *slog.Logger,
|
||||
maxBodyBytes int64,
|
||||
maxBodyBytes, maxUploadBytes int64,
|
||||
) (http.Handler, error) {
|
||||
server, err := oas.NewServer(
|
||||
handler,
|
||||
@@ -25,7 +26,19 @@ func NewRouter(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
if extensions != nil {
|
||||
// These routes are an optional contract extension (presigned uploads).
|
||||
// They live outside the generated server so contracts/openapi.json stays
|
||||
// byte-identical to the mandatory contract.
|
||||
mux.Handle("/api/v1/chats/{chat_id}/attachments/presign", extensions.Presign())
|
||||
mux.Handle("/api/v1/attachments/{attachment_id}/complete", extensions.Complete())
|
||||
}
|
||||
|
||||
mux.Handle("/", server)
|
||||
|
||||
return requestIDMiddleware(
|
||||
instanceIDMiddleware(InstanceID(), bodyLimitMiddleware(maxBodyBytes, log, server)),
|
||||
instanceIDMiddleware(InstanceID(), bodyLimitMiddleware(maxBodyBytes, maxUploadBytes, log, mux)),
|
||||
), nil
|
||||
}
|
||||
|
||||
@@ -44,11 +44,12 @@ type httpResult struct {
|
||||
}
|
||||
|
||||
type routerFixture struct {
|
||||
users *mocks.MockUserService
|
||||
chats *mocks.MockChatService
|
||||
messages *mocks.MockMessageService
|
||||
health *mocks.MockHealthChecker
|
||||
sessions *mocks.MockSessionActions
|
||||
users *mocks.MockUserService
|
||||
chats *mocks.MockChatService
|
||||
messages *mocks.MockMessageService
|
||||
attachments *mocks.MockAttachmentService
|
||||
health *mocks.MockHealthChecker
|
||||
sessions *mocks.MockSessionActions
|
||||
|
||||
server *httptest.Server
|
||||
}
|
||||
@@ -60,18 +61,28 @@ func newRouterFixture(t *testing.T) *routerFixture {
|
||||
t.Helper()
|
||||
|
||||
fixture := &routerFixture{
|
||||
users: mocks.NewMockUserService(t),
|
||||
chats: mocks.NewMockChatService(t),
|
||||
messages: mocks.NewMockMessageService(t),
|
||||
health: mocks.NewMockHealthChecker(t),
|
||||
sessions: mocks.NewMockSessionActions(t),
|
||||
users: mocks.NewMockUserService(t),
|
||||
chats: mocks.NewMockChatService(t),
|
||||
messages: mocks.NewMockMessageService(t),
|
||||
attachments: mocks.NewMockAttachmentService(t),
|
||||
health: mocks.NewMockHealthChecker(t),
|
||||
sessions: mocks.NewMockSessionActions(t),
|
||||
}
|
||||
|
||||
router, err := rest.NewRouter(
|
||||
rest.NewHandler(fixture.users, fixture.chats, fixture.messages, fixture.health, fixture.sessions),
|
||||
rest.NewHandler(
|
||||
fixture.users,
|
||||
fixture.chats,
|
||||
fixture.messages,
|
||||
fixture.attachments,
|
||||
fixture.health,
|
||||
fixture.sessions,
|
||||
),
|
||||
rest.NewSecurityHandler(fixture.sessions),
|
||||
nil,
|
||||
logger.New(logger.WithLevel(slog.LevelError)),
|
||||
testMaxBodyBytes,
|
||||
testMaxBodyBytes,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail when the SeaweedFS identities and the API environment disagree.
|
||||
|
||||
The stand keeps synthetic credentials in two places on purpose: the identities
|
||||
file is mounted into SeaweedFS, while the API receives the same values through
|
||||
the environment. This check keeps the copies from drifting.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
IDENTITIES = ROOT / "infra/s3/s3.json"
|
||||
|
||||
|
||||
def parse_env(path: Path) -> dict[str, str]:
|
||||
values: dict[str, str] = {}
|
||||
for raw in path.read_text().splitlines():
|
||||
line = raw.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value.strip().strip('"').strip("'")
|
||||
return values
|
||||
|
||||
|
||||
def main() -> int:
|
||||
identities = json.loads(IDENTITIES.read_text())
|
||||
by_name = {item["name"]: item for item in identities.get("identities", [])}
|
||||
|
||||
source = ROOT / ".env"
|
||||
if not source.is_file():
|
||||
source = ROOT / ".env.example"
|
||||
env = parse_env(source)
|
||||
|
||||
expected = {
|
||||
"admin": ("S3_ADMIN_ACCESS_KEY", "S3_ADMIN_SECRET_ACCESS_KEY"),
|
||||
"app": ("S3_ACCESS_KEY_ID", "S3_SECRET_ACCESS_KEY"),
|
||||
}
|
||||
|
||||
failures: list[str] = []
|
||||
for name, (key_var, secret_var) in expected.items():
|
||||
identity = by_name.get(name)
|
||||
if identity is None:
|
||||
failures.append(f"{IDENTITIES}: identity {name!r} is missing")
|
||||
continue
|
||||
credentials = identity.get("credentials") or []
|
||||
if not credentials:
|
||||
failures.append(f"{IDENTITIES}: identity {name!r} has no credentials")
|
||||
continue
|
||||
access_key = credentials[0].get("accessKey")
|
||||
secret_key = credentials[0].get("secretKey")
|
||||
if env.get(key_var) != access_key:
|
||||
failures.append(f"{source.name}: {key_var} does not match identity {name!r}")
|
||||
if env.get(secret_var) != secret_key:
|
||||
failures.append(f"{source.name}: {secret_var} does not match identity {name!r}")
|
||||
|
||||
if failures:
|
||||
print("FAIL: S3 credentials are inconsistent")
|
||||
for failure in failures:
|
||||
print(" -", failure)
|
||||
return 1
|
||||
|
||||
print(f"PASS: S3 identities match {source.name} (admin + app)")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+147
@@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Measure API memory under concurrent 9 MiB uploads (lab4 level 4).
|
||||
|
||||
Run against a live stand. Writes raw CSV samples and a summary under
|
||||
evidence/lab4/memory/. The primary metric is cgroup anon memory: memory.current
|
||||
also includes page cache from the temporary spool files.
|
||||
|
||||
python3 scripts/lab4_memory_experiment.py --base-url http://localhost:8080
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import csv
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import zlib
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
|
||||
OUTPUT = ROOT / 'evidence/lab4/memory'
|
||||
|
||||
|
||||
def png_of_size(width: int, height: int, seed: int) -> bytes:
|
||||
"""Build a valid, poorly compressible PNG (random RGB scanlines)."""
|
||||
rng = __import__('random').Random(seed)
|
||||
raw = bytearray()
|
||||
for _ in range(height):
|
||||
raw.append(0) # filter: None
|
||||
raw.extend(rng.randbytes(width * 3))
|
||||
|
||||
def chunk(kind: bytes, data: bytes) -> bytes:
|
||||
return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data) & 0xFFFFFFFF)
|
||||
|
||||
ihdr = struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)
|
||||
return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', ihdr) + chunk(b'IDAT', zlib.compress(bytes(raw), 6)) + chunk(b'IEND', b'')
|
||||
|
||||
|
||||
def containers(service: str) -> list[str]:
|
||||
output = os.popen(f'docker compose ps -q {service}').read().split()
|
||||
if not output:
|
||||
raise SystemExit(f'no running containers for service {service}')
|
||||
return output
|
||||
|
||||
|
||||
def read_number(container: str, path: str) -> int:
|
||||
output = os.popen(f'docker exec {container} cat {path} 2>/dev/null').read().strip()
|
||||
return int(output) if output.isdigit() else 0
|
||||
|
||||
|
||||
def memory_snapshot() -> dict[str, int]:
|
||||
api = containers('api')
|
||||
anon = current = 0
|
||||
for container in api:
|
||||
stats = os.popen(f'docker exec {container} cat /sys/fs/cgroup/memory.stat 2>/dev/null').read()
|
||||
for line in stats.splitlines():
|
||||
if line.startswith('anon '):
|
||||
anon += int(line.split()[1])
|
||||
current += read_number(container, '/sys/fs/cgroup/memory.current')
|
||||
return {'anon': anon, 'current': current}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--concurrency', type=int, default=10)
|
||||
parser.add_argument('--width', type=int, default=2100)
|
||||
parser.add_argument('--height', type=int, default=1500)
|
||||
parser.add_argument('--rounds', type=int, default=3)
|
||||
args = parser.parse_args()
|
||||
|
||||
content = png_of_size(args.width, args.height, 42)
|
||||
if len(content) > 10 * 1024 * 1024:
|
||||
raise SystemExit(f'generated PNG is {len(content)} bytes, above the 10 MiB contract limit')
|
||||
|
||||
client = Client(args.base_url, timeout=120)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
|
||||
OUTPUT.mkdir(parents=True, exist_ok=True)
|
||||
samples: list[dict[str, int | float | str]] = []
|
||||
stop = threading.Event()
|
||||
|
||||
def sample(phase: str = 'during') -> None:
|
||||
started = time.monotonic()
|
||||
while not stop.is_set():
|
||||
snapshot = memory_snapshot()
|
||||
samples.append({'elapsed': round(time.monotonic() - started, 3), 'phase': phase, **snapshot})
|
||||
time.sleep(0.2)
|
||||
|
||||
baseline = memory_snapshot()
|
||||
sampler = threading.Thread(target=sample, daemon=True)
|
||||
sampler.start()
|
||||
|
||||
def upload(_: int) -> None:
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=memory.png'
|
||||
client.call(
|
||||
'POST', path, 201, actor=alice, raw=content,
|
||||
headers={'Content-Type': 'image/png'}, schema='Attachment',
|
||||
)
|
||||
|
||||
started = time.monotonic()
|
||||
for _ in range(args.rounds):
|
||||
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
||||
list(pool.map(upload, range(args.concurrency)))
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
stop.set()
|
||||
sampler.join(timeout=2)
|
||||
after = memory_snapshot()
|
||||
|
||||
peak_anon = max((int(item['anon']) for item in samples), default=after['anon'])
|
||||
peak_current = max((int(item['current']) for item in samples), default=after['current'])
|
||||
|
||||
csv_path = OUTPUT / 'samples.csv'
|
||||
with csv_path.open('w', newline='') as handle:
|
||||
writer = csv.DictWriter(handle, fieldnames=['elapsed', 'phase', 'anon', 'current'])
|
||||
writer.writeheader()
|
||||
writer.writerows(samples)
|
||||
|
||||
summary = OUTPUT / 'summary.txt'
|
||||
summary.write_text(
|
||||
'Lab4 memory experiment\n'
|
||||
f'concurrency: {args.concurrency}\n'
|
||||
f'file_size_bytes: {len(content)}\n'
|
||||
f'uploads: {args.concurrency * args.rounds}\n'
|
||||
f'rounds: {args.rounds}\n'
|
||||
f'elapsed_seconds: {elapsed:.3f}\n'
|
||||
f'baseline_anon_bytes: {baseline["anon"]}\n'
|
||||
f'peak_anon_bytes: {peak_anon}\n'
|
||||
f'anon_delta_bytes: {peak_anon - baseline["anon"]}\n'
|
||||
f'peak_memory_current_bytes: {peak_current}\n'
|
||||
'note: memory.current includes page cache from temporary spool files; anon is the primary metric\n'
|
||||
)
|
||||
|
||||
print(summary.read_text())
|
||||
print('CSV:', csv_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+179
@@ -0,0 +1,179 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare proxy upload with presigned direct upload (lab4 track 1).
|
||||
|
||||
Measures client-facing LB traffic, API traffic, API anon memory and latency for
|
||||
two file sizes. Run against a live stand from the repository root.
|
||||
|
||||
python3 scripts/lab4_track1.py --base-url http://localhost:8080
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import csv
|
||||
import os
|
||||
import statistics
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
sys.path.insert(0, str(ROOT / 'student-tests'))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
from presign_flow import complete, presign, put # noqa: E402
|
||||
from lab4_memory_experiment import png_of_size # noqa: E402
|
||||
|
||||
OUTPUT = ROOT / 'evidence/lab4/track1'
|
||||
|
||||
|
||||
def containers(service: str) -> list[str]:
|
||||
ids = os.popen(f'docker compose ps -q {service}').read().split()
|
||||
if not ids:
|
||||
raise SystemExit(f'no running containers for service {service}')
|
||||
return ids
|
||||
|
||||
|
||||
def read_net(service: str) -> tuple[int, int]:
|
||||
rx = tx = 0
|
||||
for container in containers(service):
|
||||
for direction, path in (('rx', 'rx_bytes'), ('tx', 'tx_bytes')):
|
||||
value = os.popen(
|
||||
f'docker exec {container} cat /sys/class/net/eth0/statistics/{path} 2>/dev/null'
|
||||
).read().strip()
|
||||
if value.isdigit():
|
||||
if direction == 'rx':
|
||||
rx += int(value)
|
||||
else:
|
||||
tx += int(value)
|
||||
return rx, tx
|
||||
|
||||
|
||||
def read_anon() -> int:
|
||||
total = 0
|
||||
for container in containers('api'):
|
||||
stats = os.popen(f'docker exec {container} cat /sys/fs/cgroup/memory.stat 2>/dev/null').read()
|
||||
for line in stats.splitlines():
|
||||
if line.startswith('anon '):
|
||||
total += int(line.split()[1])
|
||||
return total
|
||||
|
||||
|
||||
class Sampler:
|
||||
def __init__(self) -> None:
|
||||
self.stop = threading.Event()
|
||||
self.peak = 0
|
||||
self.thread = threading.Thread(target=self.run, daemon=True)
|
||||
|
||||
def run(self) -> None:
|
||||
while not self.stop.is_set():
|
||||
self.peak = max(self.peak, read_anon())
|
||||
time.sleep(0.1)
|
||||
|
||||
def __enter__(self) -> 'Sampler':
|
||||
self.thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args: object) -> None:
|
||||
self.stop.set()
|
||||
self.thread.join(timeout=2)
|
||||
|
||||
|
||||
def proxy_upload(client: Client, actor: dict, chat: dict, content: bytes) -> None:
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=track1.png'
|
||||
client.call(
|
||||
'POST', path, 201, actor=actor, raw=content,
|
||||
headers={'Content-Type': 'image/png'}, schema='Attachment',
|
||||
)
|
||||
|
||||
|
||||
def presign_upload(client: Client, actor: dict, chat: dict, content: bytes) -> None:
|
||||
prepared = presign(client, actor, chat, content)
|
||||
status = put(prepared['upload_url'], content)
|
||||
if status != 200:
|
||||
raise SystemExit(f'presigned PUT failed with {status}')
|
||||
complete(client, actor, prepared['attachment_id'], prepared['sha256'])
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--iterations', type=int, default=5)
|
||||
args = parser.parse_args()
|
||||
|
||||
client = Client(args.base_url, timeout=180)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
|
||||
files = {
|
||||
'~1MiB': png_of_size(700, 500, 11),
|
||||
'~9MiB': png_of_size(2100, 1500, 12),
|
||||
}
|
||||
|
||||
OUTPUT.mkdir(parents=True, exist_ok=True)
|
||||
rows: list[dict[str, object]] = []
|
||||
|
||||
for label, content in files.items():
|
||||
for flow, operation in (('proxy', proxy_upload), ('presign', presign_upload)):
|
||||
for iteration in range(args.iterations):
|
||||
lb_before, lb_tx_before = read_net('lb')
|
||||
api_before, api_tx_before = read_net('api')
|
||||
started = time.monotonic()
|
||||
|
||||
with Sampler() as sampler:
|
||||
operation(client, alice, chat, content)
|
||||
|
||||
latency = time.monotonic() - started
|
||||
lb_after, lb_tx_after = read_net('lb')
|
||||
api_after, api_tx_after = read_net('api')
|
||||
|
||||
rows.append({
|
||||
'size': label,
|
||||
'bytes': len(content),
|
||||
'flow': flow,
|
||||
'iteration': iteration,
|
||||
'latency_s': round(latency, 4),
|
||||
'lb_rx_bytes': lb_after - lb_before,
|
||||
'lb_tx_bytes': lb_tx_after - lb_tx_before,
|
||||
'api_rx_bytes': api_after - api_before,
|
||||
'api_tx_bytes': api_tx_after - api_tx_before,
|
||||
'api_anon_peak': sampler.peak,
|
||||
})
|
||||
|
||||
csv_path = OUTPUT / 'results.csv'
|
||||
with csv_path.open('w', newline='') as handle:
|
||||
writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
|
||||
lines = ['Lab4 track 1: proxy vs presigned upload', '']
|
||||
lines.append('| size | flow | median latency, s | p95 latency, s | LB rx, bytes | API rx, bytes | API anon peak, bytes |')
|
||||
lines.append('|---|---|---:|---:|---:|---:|---:|')
|
||||
for label in files:
|
||||
for flow in ('proxy', 'presign'):
|
||||
group = [row for row in rows if row['size'] == label and row['flow'] == flow]
|
||||
latencies = sorted(float(row['latency_s']) for row in group)
|
||||
median = statistics.median(latencies)
|
||||
p95 = latencies[min(len(latencies) - 1, int(round(0.95 * (len(latencies) - 1))))]
|
||||
lb_rx = sum(int(row['lb_rx_bytes']) for row in group) / len(group)
|
||||
api_rx = sum(int(row['api_rx_bytes']) for row in group) / len(group)
|
||||
peak = max(int(row['api_anon_peak']) for row in group)
|
||||
lines.append(
|
||||
f'| {label} | {flow} | {median:.3f} | {p95:.3f} | {lb_rx:.0f} | {api_rx:.0f} | {peak} |'
|
||||
)
|
||||
|
||||
lines += [
|
||||
'',
|
||||
'Note: presigned upload keeps the file body off the API request path and off the load balancer.',
|
||||
'Completion still streams the object from S3 once to verify its SHA-256, so api_rx stays close to',
|
||||
'the file size; the saving is in the client-facing LB leg and in not accepting/buffering the body.',
|
||||
'',
|
||||
]
|
||||
summary = OUTPUT / 'summary.txt'
|
||||
summary.write_text('\n'.join(lines))
|
||||
print(summary.read_text())
|
||||
print('CSV:', csv_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -24,9 +24,9 @@ if [ -f .env ]; then
|
||||
fi
|
||||
|
||||
OUT=${OUT:-evidence/lb-readiness.txt}
|
||||
PROJECT=${COMPOSE_PROJECT_NAME:-mtusi-messenger-lab3}
|
||||
PROJECT=${COMPOSE_PROJECT_NAME:-mtusi-messenger-lab4}
|
||||
NET=${PROJECT}_default
|
||||
IMAGE=${IMAGE:-mtusi-messenger:lab3}
|
||||
IMAGE=${IMAGE:-mtusi-messenger:lab4}
|
||||
STATS=${STATS:-http://127.0.0.1:8404/stats;csv}
|
||||
BASE_URL=${BASE_URL:-http://localhost:8080}
|
||||
CANDIDATE=lb-readiness-candidate
|
||||
|
||||
+4
-4
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Restart hook: bounce API replicas and every state store of the lab3 stand.
|
||||
# Restart hook: bounce API replicas and every state store of the lab4 stand.
|
||||
#
|
||||
# scripts/restart.sh
|
||||
#
|
||||
@@ -31,9 +31,9 @@ compose() {
|
||||
|
||||
# 1. State stores: restart the running ones, then make sure they are healthy.
|
||||
# Volumes are attached as before, nothing is dropped or re-seeded.
|
||||
compose up -d --wait postgres redis
|
||||
compose restart postgres redis
|
||||
compose up -d --wait postgres redis
|
||||
compose up -d --wait postgres redis seaweedfs
|
||||
compose restart postgres redis seaweedfs
|
||||
compose up -d --wait postgres redis seaweedfs
|
||||
|
||||
# 2. API replicas: restart in place (same containers, same addresses) and bring
|
||||
# back any replica that was stopped by the fault-injection hook.
|
||||
|
||||
@@ -25,8 +25,8 @@ DURATION=${DURATION:-60}
|
||||
CONCURRENCY=${CONCURRENCY:-8}
|
||||
BASE_URL=${BASE_URL:-http://localhost:8080}
|
||||
OUT=${OUT:-evidence/rolling-update}
|
||||
IMAGE=${IMAGE:-mtusi-messenger:lab3}
|
||||
PROJECT=${COMPOSE_PROJECT_NAME:-mtusi-messenger-lab3}
|
||||
IMAGE=${IMAGE:-mtusi-messenger:lab4}
|
||||
PROJECT=${COMPOSE_PROJECT_NAME:-mtusi-messenger-lab4}
|
||||
NET=${PROJECT}_default
|
||||
STATS=${STATS:-http://127.0.0.1:8404/stats;csv}
|
||||
PREFIX=lb-rollout
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Fault-injection hook: stop exactly ONE api replica of the lab3 stand.
|
||||
# Fault-injection hook: stop exactly ONE api replica of the lab4 stand.
|
||||
#
|
||||
# TARGET_INSTANCE_ID=<hostname|container name|short id> scripts/stop-one.sh
|
||||
#
|
||||
@@ -13,7 +13,7 @@ set -eu
|
||||
# No credentials are printed.
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
|
||||
PROJECT=${COMPOSE_PROJECT_NAME:-mtusi-messenger-lab3}
|
||||
PROJECT=${COMPOSE_PROJECT_NAME:-mtusi-messenger-lab4}
|
||||
|
||||
candidates=$(docker ps -q \
|
||||
--filter "label=com.docker.compose.project=${PROJECT}" \
|
||||
@@ -26,7 +26,7 @@ fi
|
||||
|
||||
# Matches the requested alias against every reasonable identifier of a replica:
|
||||
# container hostname (= X-Instance-Id), full/short container id, full container
|
||||
# name (mtusi-messenger-lab3-api-1) and the service-local suffix (api-1).
|
||||
# name (mtusi-messenger-lab4-api-1) and the service-local suffix (api-1).
|
||||
match_replica() {
|
||||
target=$1
|
||||
full_id=$2
|
||||
|
||||
Executable
+80
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Upload through one API replica, stop it, download through another.
|
||||
|
||||
Run against a live stand:
|
||||
python3 student-tests/attachment_failover.py --base-url http://localhost:8080
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
|
||||
from client import Client, Failure, arguments, check, run # noqa: E402
|
||||
from tests_helpers import ROOT, compose # noqa: E402
|
||||
|
||||
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Attachment failover across API replicas (lab4)')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
content = FIXTURE.read_bytes()
|
||||
|
||||
response = client.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=' + quote('failover.png'),
|
||||
201,
|
||||
actor=alice,
|
||||
raw=content,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
schema='Attachment',
|
||||
)
|
||||
attachment = response['json']
|
||||
instance = response['headers'].get('X-Instance-Id')
|
||||
check(bool(instance), 'upload response has no X-Instance-Id')
|
||||
check(attachment['sha256'] == hashlib.sha256(content).hexdigest(), 'upload checksum mismatch')
|
||||
|
||||
stop = subprocess.run(
|
||||
[str(ROOT / 'scripts/stop-one.sh')],
|
||||
cwd=ROOT,
|
||||
env={**os.environ, 'TARGET_INSTANCE_ID': instance},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
check(stop.returncode == 0, 'stop-one.sh failed: ' + stop.stderr.strip())
|
||||
|
||||
deadline = time.monotonic() + 30
|
||||
last_error = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
download = client.call(
|
||||
'GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob,
|
||||
)
|
||||
if download['bytes'] == content:
|
||||
metadata = client.call(
|
||||
'GET', '/api/v1/attachments/' + attachment['id'], actor=bob, schema='Attachment',
|
||||
)['json']
|
||||
check(metadata['sha256'] == attachment['sha256'], 'metadata changed after failover')
|
||||
print('PASS: lab4 attachment failover; served by', instance)
|
||||
return
|
||||
last_error = 'downloaded bytes differ from the original'
|
||||
except Failure as error:
|
||||
last_error = str(error)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
raise Failure('download did not recover within 30s: ' + str(last_error))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
run(main)
|
||||
finally:
|
||||
compose('up', '-d', '--no-deps', 'api', check=False)
|
||||
Executable
+107
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative attachment scenarios required by lab4 level 4.
|
||||
|
||||
Run against a live stand:
|
||||
python3 student-tests/attachments_negative.py --base-url http://localhost:8080
|
||||
"""
|
||||
import hashlib
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
|
||||
from client import Client, Failure, arguments, check, run # noqa: E402
|
||||
|
||||
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
|
||||
|
||||
|
||||
def upload(client, actor, chat, content, filename='sample.png', content_type='image/png', expected=201):
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + quote(filename, safe='')
|
||||
return client.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type})
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Attachment negative scenarios (lab4)')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
fixture = FIXTURE.read_bytes()
|
||||
|
||||
# Oversized body: rejected before any image parsing.
|
||||
upload(client, alice, chat, b'\x00' * (10 * 1024 * 1024 + 1), expected=413)
|
||||
|
||||
# Empty body is not a valid image.
|
||||
upload(client, alice, chat, b'', expected=415)
|
||||
|
||||
# Corrupt PNG: flip one IDAT payload byte, CRC no longer matches.
|
||||
corrupt = bytearray(fixture)
|
||||
index = corrupt.index(b'IDAT') + 8
|
||||
corrupt[index] ^= 0xFF
|
||||
upload(client, alice, chat, bytes(corrupt), expected=415)
|
||||
|
||||
# Declared JPEG but the bytes are PNG.
|
||||
upload(client, alice, chat, fixture, filename='photo.jpg', content_type='image/jpeg', expected=415)
|
||||
|
||||
# Dangerous display names.
|
||||
for name in ('../x.png', 'a/b.png', 'a\\b.png', 'a\x00.png', 'a\n.png'):
|
||||
upload(client, alice, chat, fixture, filename=name, expected=400)
|
||||
|
||||
# A member of one chat cannot read an attachment of another chat.
|
||||
other = client.call(
|
||||
'POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Другой чат', 'member_ids': [alice['id'], eve['id']]}, schema='Chat',
|
||||
)['json']
|
||||
foreign, foreign_content = client.attachment(eve, other)
|
||||
client.call('GET', '/api/v1/attachments/' + foreign['id'], 404, actor=bob)
|
||||
client.call('GET', '/api/v1/attachments/' + foreign['id'] + '/content', 404, actor=bob)
|
||||
|
||||
# Message rules: empty text without attachments, attachment from another chat.
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': 'чужое', 'attachment_ids': [foreign['id']]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
|
||||
# Contract limits: 11 unique IDs and duplicate IDs are rejected by the schema.
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': 'много', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
duplicate = str(uuid.uuid4())
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': 'дубликат', 'attachment_ids': [duplicate, duplicate]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex},
|
||||
)
|
||||
|
||||
# The only variant in the lab4 contract is original; other values are 400.
|
||||
own, content = client.attachment(alice, chat)
|
||||
client.call('GET', '/api/v1/attachments/' + own['id'] + '/content?variant=thumbnail', 400, actor=alice)
|
||||
|
||||
# Attachment-only message is valid.
|
||||
client.message(alice, chat, '', attachments=[own['id']])
|
||||
|
||||
# Object keys are server-generated; anonymous direct access must be denied.
|
||||
key = 'attachments/' + chat['id'] + '/' + own['id']
|
||||
request = urllib.request.Request('http://127.0.0.1:8333/messenger-attachments/' + key, method='GET')
|
||||
try:
|
||||
urllib.request.urlopen(request, timeout=5)
|
||||
except urllib.error.HTTPError as error:
|
||||
check(error.code == 403, 'anonymous S3 GET must be 403, got ' + str(error.code))
|
||||
else:
|
||||
raise Failure('anonymous S3 GET unexpectedly succeeded')
|
||||
|
||||
check(hashlib.sha256(content).hexdigest() == own['sha256'], 'fixture checksum mismatch')
|
||||
print('PASS: lab4 attachment negative scenarios')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Executable
+135
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Presigned direct-upload track (lab4 level 5).
|
||||
|
||||
The mandatory proxy endpoint stays available; this script exercises the
|
||||
optional extension: presign -> direct PUT to S3 -> complete -> message.
|
||||
|
||||
Run against a live stand:
|
||||
python3 student-tests/presign_flow.py --base-url http://localhost:8080
|
||||
|
||||
Set PRESIGN_EXPIRY_WAIT=seconds to also verify that an expired URL is rejected
|
||||
(the stand must run with a matching S3_PRESIGN_TTL).
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
|
||||
from client import Client, Failure, arguments, check, run # noqa: E402
|
||||
|
||||
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
|
||||
|
||||
|
||||
def presign(client, actor, chat, content, filename='presigned.png', content_type='image/png'):
|
||||
body = {
|
||||
'filename': filename,
|
||||
'content_type': content_type,
|
||||
'size_bytes': len(content),
|
||||
'sha256': hashlib.sha256(content).hexdigest(),
|
||||
}
|
||||
response = client.call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/attachments/presign', 201,
|
||||
actor=actor, body=body,
|
||||
)
|
||||
return json.loads(response['bytes'])
|
||||
|
||||
|
||||
def put(url, content, content_type='image/png'):
|
||||
request = urllib.request.Request(
|
||||
url, data=content, method='PUT', headers={'Content-Type': content_type},
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=15) as response:
|
||||
return response.status
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code
|
||||
|
||||
|
||||
def complete(client, actor, attachment_id, digest, expected=200):
|
||||
return client.call(
|
||||
'POST', '/api/v1/attachments/' + attachment_id + '/complete', expected,
|
||||
actor=actor, body={'sha256': digest}, schema='Attachment' if expected == 200 else None,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Presigned upload flow (lab4 track 1)')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
content = FIXTURE.read_bytes()
|
||||
digest = hashlib.sha256(content).hexdigest()
|
||||
|
||||
# Happy path: direct PUT, completion, message, download.
|
||||
presigned = presign(client, alice, chat, content)
|
||||
check(presigned['method'] == 'PUT', 'presign method must be PUT')
|
||||
check(put(presigned['upload_url'], content) == 200, 'direct PUT failed')
|
||||
attachment = complete(client, alice, presigned['attachment_id'], digest)['json']
|
||||
check(attachment['sha256'] == digest and attachment['size_bytes'] == len(content), 'completed metadata mismatch')
|
||||
|
||||
client.message(alice, chat, '', attachments=[attachment['id']])
|
||||
download = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob)
|
||||
check(download['bytes'] == content, 'download differs from the presigned original')
|
||||
|
||||
# Completion is idempotent.
|
||||
again = complete(client, alice, presigned['attachment_id'], digest)['json']
|
||||
check(again['id'] == attachment['id'], 'repeated complete returned another attachment')
|
||||
|
||||
# A foreign user cannot complete someone else's pending upload.
|
||||
pending = presign(client, alice, chat, content)
|
||||
complete(client, bob, pending['attachment_id'], digest, expected=404)
|
||||
|
||||
# Incomplete upload: complete before PUT.
|
||||
incomplete = presign(client, alice, chat, content)
|
||||
complete(client, alice, incomplete['attachment_id'], digest, expected=409)
|
||||
|
||||
# Wrong checksum: the object is rejected and removed.
|
||||
mismatch = presign(client, alice, chat, content)
|
||||
check(put(mismatch['upload_url'], content) == 200, 'direct PUT failed')
|
||||
complete(client, alice, mismatch['attachment_id'], '0' * 64, expected=409)
|
||||
|
||||
# Declared size is enforced at completion, after the direct PUT.
|
||||
oversized = presign(client, alice, chat, content)
|
||||
check(put(oversized['upload_url'], content + b'x') == 200, 'direct PUT failed')
|
||||
complete(client, alice, oversized['attachment_id'], digest, expected=400)
|
||||
|
||||
# Last PUT wins before completion; the declared checksum must describe it.
|
||||
overwrite = presign(client, alice, chat, content)
|
||||
check(put(overwrite['upload_url'], b'not an image') == 200, 'first PUT failed')
|
||||
check(put(overwrite['upload_url'], content) == 200, 'second PUT failed')
|
||||
overwritten = complete(client, alice, overwrite['attachment_id'], digest)['json']
|
||||
check(overwritten['sha256'] == digest, 'last PUT was not the completed object')
|
||||
|
||||
# A user outside the chat cannot obtain a presigned URL.
|
||||
other = client.call(
|
||||
'POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Приватный чат', 'member_ids': [alice['id'], eve['id']]}, schema='Chat',
|
||||
)['json']
|
||||
client.call(
|
||||
'POST', '/api/v1/chats/' + other['id'] + '/attachments/presign', 404,
|
||||
actor=bob,
|
||||
body={
|
||||
'filename': 'x.png', 'content_type': 'image/png',
|
||||
'size_bytes': len(content), 'sha256': digest,
|
||||
},
|
||||
)
|
||||
|
||||
wait = float(os.environ.get('PRESIGN_EXPIRY_WAIT', '0'))
|
||||
if wait > 0:
|
||||
expired = presign(client, alice, chat, content)
|
||||
time.sleep(wait)
|
||||
status = put(expired['upload_url'], content)
|
||||
check(status == 403, 'expired presigned URL must be 403, got ' + str(status))
|
||||
print('EXPIRED: presigned URL rejected after', wait, 'seconds')
|
||||
|
||||
print('PASS: lab4 presigned upload flow')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -18,7 +18,7 @@ def compose(*args, check=True):
|
||||
|
||||
def containers_by_hostname(service='api'):
|
||||
"""{hostname: {id, name, status}} для контейнеров сервиса."""
|
||||
project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab3')
|
||||
project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab4')
|
||||
listed = subprocess.run(
|
||||
[
|
||||
'docker', 'ps', '-q',
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
// Command specgen produces the OpenAPI document used for code generation.
|
||||
//
|
||||
// The mandatory contract (contracts/openapi.json, version 1.4.0) describes the
|
||||
// cross-field rule "text is non-empty OR attachment_ids is non-empty" with an
|
||||
// anyOf. ogen v1.24 cannot generate Go types for two unnamed anyOf branches
|
||||
// with the same name; ignoring the limitation silently drops entire operations.
|
||||
// This tool removes exactly those two anyOf constraints from a derived document
|
||||
// and fails on any other union, so a contract change cannot silently weaken
|
||||
// code generation. The canonical contract is never modified; the application
|
||||
// enforces the cross-field rule itself.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
contractPath = "contracts/openapi.json"
|
||||
labPath = "lab.json"
|
||||
outputPath = ".cache/openapi.codegen.json"
|
||||
)
|
||||
|
||||
var rewriteTargets = [][]string{
|
||||
{"components", "schemas", "Message"},
|
||||
{"components", "schemas", "CreateMessage"},
|
||||
}
|
||||
|
||||
func main() {
|
||||
check := flag.Bool("check", false, "verify that the derived document matches the generated file")
|
||||
flag.Parse()
|
||||
|
||||
derived, err := derive()
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
|
||||
if *check {
|
||||
current, err := os.ReadFile(outputPath)
|
||||
if err != nil {
|
||||
fail(fmt.Errorf("read %s (run make generate-api first): %w", outputPath, err))
|
||||
}
|
||||
|
||||
if !bytes.Equal(current, derived) {
|
||||
fail(fmt.Errorf("%s is stale: run make generate-api", outputPath))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o750); err != nil {
|
||||
fail(fmt.Errorf("create cache directory: %w", err))
|
||||
}
|
||||
|
||||
if err := os.WriteFile(outputPath, derived, 0o600); err != nil {
|
||||
fail(fmt.Errorf("write %s: %w", outputPath, err))
|
||||
}
|
||||
}
|
||||
|
||||
func derive() ([]byte, error) {
|
||||
lab, err := readObject(labPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
spec, err := readObject(contractPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contractVersion, _ := lab["contract_version"].(string)
|
||||
if contractVersion == "" {
|
||||
return nil, fmt.Errorf("%s: contract_version is missing", labPath)
|
||||
}
|
||||
|
||||
info, _ := spec["info"].(map[string]any)
|
||||
version, _ := info["version"].(string)
|
||||
if version != contractVersion {
|
||||
return nil, fmt.Errorf(
|
||||
"contract version mismatch: %s has %q, %s has %q",
|
||||
labPath, contractVersion, contractPath, version,
|
||||
)
|
||||
}
|
||||
|
||||
number, _ := lab["number"].(float64)
|
||||
if number != 4 {
|
||||
return nil, fmt.Errorf("%s: expected lab number 4, got %v", labPath, lab["number"])
|
||||
}
|
||||
|
||||
for _, path := range rewriteTargets {
|
||||
schema, err := lookup(spec, path...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, ok := schema["anyOf"]; !ok {
|
||||
return nil, fmt.Errorf("%s: anyOf is missing; re-check the codegen workaround", filepath.Join(path...))
|
||||
}
|
||||
|
||||
delete(schema, "anyOf")
|
||||
}
|
||||
|
||||
if err := rejectRemainingUnions(spec, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
encoded, err := json.MarshalIndent(spec, "", " ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode derived document: %w", err)
|
||||
}
|
||||
|
||||
return append(encoded, '\n'), nil
|
||||
}
|
||||
|
||||
func readObject(path string) (map[string]any, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
|
||||
var object map[string]any
|
||||
if err := json.Unmarshal(raw, &object); err != nil {
|
||||
return nil, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
|
||||
return object, nil
|
||||
}
|
||||
|
||||
func lookup(root map[string]any, path ...string) (map[string]any, error) {
|
||||
current := root
|
||||
|
||||
for _, key := range path {
|
||||
next, ok := current[key].(map[string]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s: object is missing", filepath.Join(path...))
|
||||
}
|
||||
|
||||
current = next
|
||||
}
|
||||
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func rejectRemainingUnions(value any, path []string) error {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
for key, child := range typed {
|
||||
if key == "anyOf" || key == "oneOf" {
|
||||
return errors.New("unsupported union at " + filepath.Join(append(path, key)...) +
|
||||
": extend the codegen workaround explicitly")
|
||||
}
|
||||
|
||||
if err := rejectRemainingUnions(child, append(path, key)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for index, child := range typed {
|
||||
if err := rejectRemainingUnions(child, append(path, strconv.Itoa(index))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func fail(err error) {
|
||||
fmt.Fprintln(os.Stderr, "specgen:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user