Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -168,44 +168,49 @@ def send_text_request(model_name, prompt, image):
|
|
| 168 |
print(f"Error with model {model_name}: {e}")
|
| 169 |
return None
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
"""
|
| 174 |
-
Fetches an image from a given URL and returns it.
|
| 175 |
This acts as a proxy to get around client-side CORS issues for PDF generation.
|
| 176 |
The image URL is passed as a query parameter.
|
| 177 |
-
"""
|
| 178 |
-
uid = verify_token(request.headers.get('Authorization'))
|
| 179 |
-
if not uid:
|
| 180 |
-
return jsonify({'error': 'Unauthorized'}), 401
|
| 181 |
-
|
| 182 |
-
# Verify the user has access to this project to prevent unauthorized image proxying
|
| 183 |
-
project_data = db_ref.child(f'projects/{project_id}').get()
|
| 184 |
-
if not project_data or project_data.get('uid') != uid:
|
| 185 |
-
return jsonify({'error': 'Project not found or access denied'}), 404
|
| 186 |
|
|
|
|
|
|
|
|
|
|
| 187 |
image_url = request.args.get('url')
|
| 188 |
if not image_url:
|
| 189 |
-
return jsonify({'error': '
|
| 190 |
|
| 191 |
try:
|
| 192 |
-
#
|
|
|
|
| 193 |
with requests.Session() as s:
|
| 194 |
-
response = s.get(image_url, stream=True)
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
-
# Get content type from the original response
|
| 198 |
content_type = response.headers.get('Content-Type', 'application/octet-stream')
|
| 199 |
|
| 200 |
-
# Stream the response back to the client
|
| 201 |
-
|
|
|
|
| 202 |
|
|
|
|
|
|
|
| 203 |
except requests.exceptions.RequestException as e:
|
| 204 |
-
|
| 205 |
-
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
-
|
| 208 |
-
|
|
|
|
| 209 |
|
| 210 |
# -----------------------------------------------------------------------------
|
| 211 |
# 3. AUTHENTICATION & USER MANAGEMENT
|
|
|
|
| 168 |
print(f"Error with model {model_name}: {e}")
|
| 169 |
return None
|
| 170 |
|
| 171 |
+
|
| 172 |
+
# =============================================================================
|
| 173 |
+
# OPEN IMAGE PROXY ENDPOINT (NO AUTHENTICATION)
|
| 174 |
+
# =============================================================================
|
| 175 |
+
@app.route('/api/image-proxy', methods=['GET'])
|
| 176 |
+
def image_proxy():
|
| 177 |
"""
|
| 178 |
+
[NO AUTH - OPEN PROXY] Fetches an image from a given URL and returns it.
|
| 179 |
This acts as a proxy to get around client-side CORS issues for PDF generation.
|
| 180 |
The image URL is passed as a query parameter.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
WARNING: This endpoint is an open proxy and is not secure. Anyone can use it
|
| 183 |
+
to route traffic through your server. Use with caution.
|
| 184 |
+
"""
|
| 185 |
image_url = request.args.get('url')
|
| 186 |
if not image_url:
|
| 187 |
+
return jsonify({'error': 'The "url" query parameter is required.'}), 400
|
| 188 |
|
| 189 |
try:
|
| 190 |
+
# Use a session object for better performance and connection management.
|
| 191 |
+
# Set a timeout to prevent requests from hanging indefinitely.
|
| 192 |
with requests.Session() as s:
|
| 193 |
+
response = s.get(image_url, stream=True, timeout=15)
|
| 194 |
+
# Raise an exception for bad status codes (e.g., 404 Not Found, 500 Server Error).
|
| 195 |
+
response.raise_for_status()
|
| 196 |
|
| 197 |
+
# Get the content type from the original image response to pass it along to the client.
|
| 198 |
content_type = response.headers.get('Content-Type', 'application/octet-stream')
|
| 199 |
|
| 200 |
+
# Stream the response back to the client. This is memory-efficient as it doesn't
|
| 201 |
+
# load the entire image into memory at once.
|
| 202 |
+
return Response(response.iter_content(chunk_size=1024), content_type=content_type)
|
| 203 |
|
| 204 |
+
except requests.exceptions.Timeout:
|
| 205 |
+
return jsonify({'error': 'The request to the external image URL timed out.'}), 504 # 504 Gateway Timeout
|
| 206 |
except requests.exceptions.RequestException as e:
|
| 207 |
+
# This catches other network-related errors (e.g., DNS failure, connection error).
|
| 208 |
+
print(f"Error fetching image from URL '{image_url}': {e}")
|
| 209 |
+
return jsonify({'error': f'Failed to fetch the image from the provided URL. The server may be down or the URL may be incorrect.'}), 502 # 502 Bad Gateway
|
| 210 |
except Exception as e:
|
| 211 |
+
# Catch any other unexpected errors.
|
| 212 |
+
print(f"An unexpected error occurred while proxying the image '{image_url}': {e}")
|
| 213 |
+
return jsonify({'error': 'An internal server error occurred while trying to fetch the image.'}), 500
|
| 214 |
|
| 215 |
# -----------------------------------------------------------------------------
|
| 216 |
# 3. AUTHENTICATION & USER MANAGEMENT
|