rairo commited on
Commit
89f7839
·
verified ·
1 Parent(s): e353eec

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -7
main.py CHANGED
@@ -169,7 +169,7 @@ def send_text_request(model_name, prompt, image):
169
  return None
170
 
171
 
172
- # =============================================================================
173
  # OPEN IMAGE PROXY ENDPOINT (NO AUTHENTICATION)
174
  # =============================================================================
175
  @app.route('/api/image-proxy', methods=['GET'])
@@ -179,21 +179,47 @@ def image_proxy():
179
  return jsonify({'error': 'URL parameter is missing.'}), 400
180
 
181
  try:
182
- bucket_name = bucket.name
183
- # Extract the file path from the full URL
184
- # Example: https://.../bucket-name/users/file.png -> users/file.png
185
- blob_path = image_url.split(f"{bucket_name}/")[1].split("?")[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
 
187
  blob = bucket.blob(blob_path)
188
 
189
  if not blob.exists():
190
  return jsonify({'error': 'Image not found.'}), 404
191
 
 
192
  image_bytes = blob.download_as_bytes()
193
  content_type = blob.content_type or 'application/octet-stream'
194
 
195
- return Response(image_bytes, content_type=content_type)
196
-
 
 
 
 
 
 
197
  except Exception as e:
198
  # This will catch parsing errors or other unexpected issues.
199
  print(f"Error in image proxy: {e}")
 
169
  return None
170
 
171
 
172
+ #=============================================================================
173
  # OPEN IMAGE PROXY ENDPOINT (NO AUTHENTICATION)
174
  # =============================================================================
175
  @app.route('/api/image-proxy', methods=['GET'])
 
179
  return jsonify({'error': 'URL parameter is missing.'}), 400
180
 
181
  try:
182
+ # Parse Firebase Storage URL
183
+ # Expected format: https://storage.googleapis.com/bucket-name/path/to/file.ext
184
+ if 'storage.googleapis.com' not in image_url:
185
+ return jsonify({'error': 'Invalid Firebase Storage URL.'}), 400
186
+
187
+ # Extract bucket name and blob path from the URL
188
+ url_parts = image_url.split('storage.googleapis.com/')[1]
189
+ # Remove query parameters if present
190
+ url_parts = url_parts.split('?')[0]
191
+
192
+ # Split into bucket name and blob path
193
+ path_components = url_parts.split('/', 1)
194
+ if len(path_components) < 2:
195
+ return jsonify({'error': 'Invalid URL format.'}), 400
196
+
197
+ url_bucket_name = path_components[0]
198
+ blob_path = path_components[1]
199
+
200
+ # Verify bucket name matches (optional security check)
201
+ expected_bucket_name = bucket.name
202
+ if url_bucket_name != expected_bucket_name:
203
+ return jsonify({'error': 'Bucket name mismatch.'}), 403
204
 
205
+ # Get the blob
206
  blob = bucket.blob(blob_path)
207
 
208
  if not blob.exists():
209
  return jsonify({'error': 'Image not found.'}), 404
210
 
211
+ # Download and return the image
212
  image_bytes = blob.download_as_bytes()
213
  content_type = blob.content_type or 'application/octet-stream'
214
 
215
+ # Add cache headers for better performance
216
+ response = Response(image_bytes, content_type=content_type)
217
+ response.headers['Cache-Control'] = 'public, max-age=3600' # Cache for 1 hour
218
+ return response
219
+
220
+ except IndexError as e:
221
+ print(f"URL parsing error in image proxy: {e}")
222
+ return jsonify({'error': 'Invalid URL format.'}), 400
223
  except Exception as e:
224
  # This will catch parsing errors or other unexpected issues.
225
  print(f"Error in image proxy: {e}")