# Общие функции для скриптов lab4 (подключается через `. scripts/_lib.sh`). env_get() { v=$(grep -E "^$1=" .env 2>/dev/null | tail -n1 | cut -d= -f2- || true) if [ -n "$v" ]; then printf '%s' "$v"; else printf '%s' "$2"; fi } DC="docker compose -p gavrilenko-lab4 --env-file .env" LB_PORT=$(env_get LB_HOST_PORT 8081) MINIO_PORT=$(env_get MINIO_HOST_PORT 9002) S3_BUCKET=$(env_get S3_BUCKET messenger) BASE_URL=${BASE_URL:-http://127.0.0.1:${LB_PORT}} json_get() { python3 -c 'import json,sys; print(json.load(sys.stdin)[sys.argv[1]])' "$1"; } register_and_login() { name="u$(date +%s)$$$(awk 'BEGIN{srand(); printf "%05d", rand()*99999}')" pw="correct-horse-1" uid=$(curl -fsS -X POST "$BASE_URL/api/v1/users" -H 'Content-Type: application/json' \ -d "{\"username\":\"$name\",\"display_name\":\"Script\",\"password\":\"$pw\"}" | json_get id) tok=$(curl -fsS -X POST "$BASE_URL/api/v1/auth/sessions" -u "$name:$pw" | json_get token) printf '%s %s' "$tok" "$uid" } create_chat() { curl -fsS -X POST "$BASE_URL/api/v1/chats" -H "Authorization: Bearer $1" -H 'Content-Type: application/json' \ -d "{\"title\":\"script\",\"member_ids\":[\"$2\"]}" | json_get id } upload_file() { curl -fsS -X POST "$BASE_URL/api/v1/chats/$2/attachments?filename=$(basename "$3")" \ -H "Authorization: Bearer $1" -H "Content-Type: ${4:-image/png}" --data-binary "@$3" } psql_q() { $DC exec -T postgres sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Atc "$1"' _ "$1" } s3sh() { $DC run --rm -T --no-deps --entrypoint /bin/sh minio-init -c \ "mc alias set local http://minio:9000 \"\$MINIO_ROOT_USER\" \"\$MINIO_ROOT_PASSWORD\" >/dev/null && $1" } uuid() { python3 -c 'import uuid; print(uuid.uuid4())'; }