rairo commited on
Commit
9d4f581
·
verified ·
1 Parent(s): 7408586

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +39 -0
main.py CHANGED
@@ -168,6 +168,45 @@ def send_text_request(model_name, prompt, image):
168
  print(f"Error with model {model_name}: {e}")
169
  return None
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  # -----------------------------------------------------------------------------
172
  # 3. AUTHENTICATION & USER MANAGEMENT
173
  # -----------------------------------------------------------------------------
 
168
  print(f"Error with model {model_name}: {e}")
169
  return None
170
 
171
+ @app.route('/api/projects/<string:project_id>/images', methods=['GET'])
172
+ def get_project_image(project_id):
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': 'Image URL parameter is required'}), 400
190
+
191
+ try:
192
+ # It's crucial to use a session object for potential connection pooling and header management
193
+ with requests.Session() as s:
194
+ response = s.get(image_url, stream=True)
195
+ response.raise_for_status() # Raise an exception for bad status codes
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
+ return Response(response.iter_content(chunk_size=8192), content_type=content_type)
202
+
203
+ except requests.exceptions.RequestException as e:
204
+ print(f"Error fetching image from URL {image_url}: {e}")
205
+ return jsonify({'error': f'Failed to fetch image from the provided URL: {e}'}), 502 # 502 Bad Gateway is appropriate here
206
+ except Exception as e:
207
+ print(f"An unexpected error occurred while proxying image: {e}")
208
+ return jsonify({'error': 'An internal error occurred'}), 500
209
+
210
  # -----------------------------------------------------------------------------
211
  # 3. AUTHENTICATION & USER MANAGEMENT
212
  # -----------------------------------------------------------------------------