Commit a28c6863 authored by Your Name's avatar Your Name

Fix image generation URL port and 404 issues

- Fixed file path not being set in app.py for /v1/files endpoint
- Fixed Host header parsing to correctly extract hostname without port
- Added debug logging to trace URL construction and file serving
parent ccc0f6ac
...@@ -92,9 +92,11 @@ async def list_models(): ...@@ -92,9 +92,11 @@ async def list_models():
@app.get("/v1/files/{filename}") @app.get("/v1/files/{filename}")
async def get_file(filename: str): async def get_file(filename: str):
"""Serve uploaded/generated files.""" """Serve uploaded/generated files."""
print(f"DEBUG get_file: filename={filename}, global_file_path={global_file_path}")
if global_file_path: if global_file_path:
import os import os
file_path = os.path.join(global_file_path, filename) file_path = os.path.join(global_file_path, filename)
print(f"DEBUG get_file: full path={file_path}, exists={os.path.exists(file_path)}")
if os.path.exists(file_path): if os.path.exists(file_path):
return FileResponse(file_path) return FileResponse(file_path)
raise HTTPException(status_code=404, detail="File not found") raise HTTPException(status_code=404, detail="File not found")
...@@ -102,18 +102,31 @@ def save_image_response(img, request_format="base64", http_request=None): ...@@ -102,18 +102,31 @@ def save_image_response(img, request_format="base64", http_request=None):
# Use server host from request headers (what client used to connect) # Use server host from request headers (what client used to connect)
if http_request: if http_request:
# Get the Host header - this is what the client used to reach the server # Get the Host header - this is what the client used to reach the server
# The Host header typically includes the port, e.g., "192.168.1.1:6745"
client_host = http_request.headers.get('host', '') client_host = http_request.headers.get('host', '')
if not client_host: if not client_host:
# Fallback to client IP if no Host header # Fallback to client IP if no Host header
client_host = http_request.client.host if http_request.client else '127.0.0.1' client_host = http_request.client.host if http_request.client else '127.0.0.1'
# Strip port if present in Host header
if ':' in client_host and not client_host.replace(':', '').isdigit(): # Strip port from host if present (Host header includes port like "192.168.1.1:6745")
client_host = client_host.split(':')[0] if ':' in client_host:
# Check if the part after : is a port number
parts = client_host.split(':')
if len(parts) == 2 and parts[1].isdigit():
# It's a port number, strip it
client_host = parts[0]
elif len(parts) > 2:
# IPv6 or other format, take last part as port check
last_part = parts[-1]
if last_part.isdigit():
client_host = ':'.join(parts[:-1])
# Check if HTTPS is enabled # Check if HTTPS is enabled
use_https = getattr(global_args, 'https', False) or getattr(global_args, 'pubkey', None) use_https = getattr(global_args, 'https', False) or getattr(global_args, 'pubkey', None)
protocol = "https" if use_https else "http" protocol = "https" if use_https else "http"
port = getattr(global_args, 'port', 8000) port = getattr(global_args, 'port', 8000)
base_url = f"{protocol}://{client_host}:{port}" base_url = f"{protocol}://{client_host}:{port}"
print(f"DEBUG: client_host={client_host}, port={port}, base_url={base_url}")
else: else:
base_url = "http://127.0.0.1:8000" base_url = "http://127.0.0.1:8000"
else: else:
......
...@@ -94,6 +94,10 @@ def main(): ...@@ -94,6 +94,10 @@ def main():
from codai.api.images import set_global_file_path as set_images_file_path from codai.api.images import set_global_file_path as set_images_file_path
set_images_file_path(global_file_path) set_images_file_path(global_file_path)
# Also set file path for app.py (needed for /v1/files endpoint)
from codai.api.app import set_global_file_path_wrapper
set_global_file_path_wrapper(global_file_path)
if global_debug: if global_debug:
# Print the full command line that was used to invoke codai # Print the full command line that was used to invoke codai
import shlex import shlex
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment