#!/bin/bash # Configuration API_URL="http://localhost:8080" CLIENT_ID="AAA" # The client ID is AAA PDF_FILE="./test.pdf" # Colors for output GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' # No Color echo "Document Upload Script" echo "=====================" # Check if test.pdf exists if [ ! -f "$PDF_FILE" ]; then echo -e "${RED}Error: $PDF_FILE not found${NC}" echo "Please create a test.pdf file first" exit 1 fi # Check if API is running echo "Checking if API is running..." if ! curl -s -f "$API_URL/health" > /dev/null 2>&1; then echo -e "${RED}Error: API is not running at $API_URL${NC}" echo "Please start the API with: DISABLE_AUTH=true go run ./cmd/queryAPI" exit 1 fi echo -e "${GREEN}API is running${NC}" # Function to create client create_client() { echo "Creating client with ID '$CLIENT_ID'..." CREATE_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API_URL/client" \ -H "Content-Type: application/json" \ -d "{\"id\":\"$CLIENT_ID\",\"name\":\"Test Client $CLIENT_ID\"}") CREATE_HTTP_CODE=$(echo "$CREATE_RESPONSE" | tail -n1) CREATE_RESPONSE_BODY=$(echo "$CREATE_RESPONSE" | sed '$d') if [ "$CREATE_HTTP_CODE" = "201" ] || [ "$CREATE_HTTP_CODE" = "200" ]; then echo -e "${GREEN}Client '$CLIENT_ID' created successfully${NC}" return 0 elif [ "$CREATE_HTTP_CODE" = "409" ] || [ "$CREATE_HTTP_CODE" = "400" ]; then echo -e "${GREEN}Client '$CLIENT_ID' already exists${NC}" return 0 else echo -e "${RED}Error: Failed to create client (HTTP $CREATE_HTTP_CODE)${NC}" echo "Response: $CREATE_RESPONSE_BODY" return 1 fi } # Verify client exists or create it echo "Verifying client with ID '$CLIENT_ID' exists..." CLIENT_CHECK=$(curl -s -o /dev/null -w "%{http_code}" "$API_URL/client/$CLIENT_ID") if [ "$CLIENT_CHECK" != "200" ]; then echo "Client '$CLIENT_ID' not found, attempting to create it..." if ! create_client; then exit 1 fi else echo -e "${GREEN}Client '$CLIENT_ID' verified${NC}" fi # Upload document echo "Uploading document..." # Try different approaches based on what the server expects # Using explicit content type for the file field UPLOAD_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API_URL/client/$CLIENT_ID/document" \ -F "file=@$PDF_FILE;type=application/octet-stream" \ -F "filename=test.pdf") HTTP_CODE=$(echo "$UPLOAD_RESPONSE" | tail -n1) RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | sed '$d') if [ "$HTTP_CODE" = "200" ]; then echo -e "${GREEN}Document uploaded successfully!${NC}" echo "Response: $RESPONSE_BODY" else echo -e "${RED}Error: Upload failed with HTTP $HTTP_CODE${NC}" echo "Response: $RESPONSE_BODY" exit 1 fi echo "" echo "You can check the uploaded documents with:" echo "curl $API_URL/client/$CLIENT_ID/document"