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():
@app.get("/v1/files/{filename}")
async def get_file(filename: str):
"""Serve uploaded/generated files."""
print(f"DEBUG get_file: filename={filename}, global_file_path={global_file_path}")
if global_file_path:
import os
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):
return FileResponse(file_path)
raise HTTPException(status_code=404, detail="File not found")
......@@ -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)
if http_request:
# 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', '')
if not client_host:
# Fallback to client IP if no Host header
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():
client_host = client_host.split(':')[0]
# Strip port from host if present (Host header includes port like "192.168.1.1:6745")
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
use_https = getattr(global_args, 'https', False) or getattr(global_args, 'pubkey', None)
protocol = "https" if use_https else "http"
port = getattr(global_args, 'port', 8000)
base_url = f"{protocol}://{client_host}:{port}"
print(f"DEBUG: client_host={client_host}, port={port}, base_url={base_url}")
else:
base_url = "http://127.0.0.1:8000"
else:
......
......@@ -94,6 +94,10 @@ def main():
from codai.api.images import set_global_file_path as set_images_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:
# Print the full command line that was used to invoke codai
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