Spaces:
Running
Running
image preview fix
Browse files- py_backend/app/config.py +1 -0
- py_backend/app/routers/caption.py +12 -12
- py_backend/app/routers/upload.py +13 -7
py_backend/app/config.py
CHANGED
|
@@ -6,6 +6,7 @@ class Settings(BaseSettings):
|
|
| 6 |
S3_ACCESS_KEY: str = ""
|
| 7 |
S3_SECRET_KEY: str = ""
|
| 8 |
S3_BUCKET: str = ""
|
|
|
|
| 9 |
OPENAI_API_KEY: str = ""
|
| 10 |
ANTHROPIC_API_KEY: str = ""
|
| 11 |
GOOGLE_API_KEY: str = ""
|
|
|
|
| 6 |
S3_ACCESS_KEY: str = ""
|
| 7 |
S3_SECRET_KEY: str = ""
|
| 8 |
S3_BUCKET: str = ""
|
| 9 |
+
S3_PUBLIC_URL_BASE: str = "" # For Cloudflare R2 public URLs (e.g., https://your-bucket.your-subdomain.r2.cloudflarestorage.com)
|
| 10 |
OPENAI_API_KEY: str = ""
|
| 11 |
ANTHROPIC_API_KEY: str = ""
|
| 12 |
GOOGLE_API_KEY: str = ""
|
py_backend/app/routers/caption.py
CHANGED
|
@@ -94,7 +94,7 @@ async def create_caption(
|
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error reading image file: {e}")
|
| 96 |
try:
|
| 97 |
-
url = storage.
|
| 98 |
if url.startswith('/'):
|
| 99 |
url = f"http://localhost:8000{url}"
|
| 100 |
import requests
|
|
@@ -138,7 +138,7 @@ async def create_caption(
|
|
| 138 |
|
| 139 |
from .upload import convert_image_to_dict
|
| 140 |
try:
|
| 141 |
-
|
| 142 |
if url.startswith('/') and settings.STORAGE_PROVIDER == "local":
|
| 143 |
url = f"http://localhost:8000{url}"
|
| 144 |
except Exception:
|
|
@@ -166,7 +166,7 @@ def get_caption(
|
|
| 166 |
|
| 167 |
from .upload import convert_image_to_dict
|
| 168 |
try:
|
| 169 |
-
url = storage.
|
| 170 |
if url.startswith('/') and settings.STORAGE_PROVIDER == "local":
|
| 171 |
url = f"http://localhost:8000{url}"
|
| 172 |
except Exception:
|
|
@@ -196,7 +196,7 @@ def get_captions_by_image(
|
|
| 196 |
db.refresh(caption)
|
| 197 |
|
| 198 |
try:
|
| 199 |
-
url = storage.
|
| 200 |
except Exception:
|
| 201 |
url = f"/api/images/{caption.image_id}/file"
|
| 202 |
|
|
@@ -228,7 +228,7 @@ def get_all_captions_with_images(
|
|
| 228 |
db.refresh(caption)
|
| 229 |
|
| 230 |
try:
|
| 231 |
-
url = storage.
|
| 232 |
except Exception:
|
| 233 |
url = f"/api/images/{caption.image_id}/file"
|
| 234 |
|
|
@@ -254,13 +254,13 @@ def update_caption(
|
|
| 254 |
db.refresh(caption)
|
| 255 |
|
| 256 |
from .upload import convert_image_to_dict
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
|
| 265 |
img_dict = convert_image_to_dict(caption, url)
|
| 266 |
return schemas.ImageOut(**img_dict)
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error reading image file: {e}")
|
| 96 |
try:
|
| 97 |
+
url = storage.get_object_url(img.file_key)
|
| 98 |
if url.startswith('/'):
|
| 99 |
url = f"http://localhost:8000{url}"
|
| 100 |
import requests
|
|
|
|
| 138 |
|
| 139 |
from .upload import convert_image_to_dict
|
| 140 |
try:
|
| 141 |
+
url = storage.get_object_url(c.file_key)
|
| 142 |
if url.startswith('/') and settings.STORAGE_PROVIDER == "local":
|
| 143 |
url = f"http://localhost:8000{url}"
|
| 144 |
except Exception:
|
|
|
|
| 166 |
|
| 167 |
from .upload import convert_image_to_dict
|
| 168 |
try:
|
| 169 |
+
url = storage.get_object_url(caption.file_key)
|
| 170 |
if url.startswith('/') and settings.STORAGE_PROVIDER == "local":
|
| 171 |
url = f"http://localhost:8000{url}"
|
| 172 |
except Exception:
|
|
|
|
| 196 |
db.refresh(caption)
|
| 197 |
|
| 198 |
try:
|
| 199 |
+
url = storage.get_object_url(caption.file_key)
|
| 200 |
except Exception:
|
| 201 |
url = f"/api/images/{caption.image_id}/file"
|
| 202 |
|
|
|
|
| 228 |
db.refresh(caption)
|
| 229 |
|
| 230 |
try:
|
| 231 |
+
url = storage.get_object_url(caption.file_key)
|
| 232 |
except Exception:
|
| 233 |
url = f"/api/images/{caption.image_id}/file"
|
| 234 |
|
|
|
|
| 254 |
db.refresh(caption)
|
| 255 |
|
| 256 |
from .upload import convert_image_to_dict
|
| 257 |
+
try:
|
| 258 |
+
url = storage.get_object_url(caption.file_key)
|
| 259 |
+
except Exception:
|
| 260 |
+
url = f"/api/images/{caption.image_id}/file"
|
| 261 |
+
|
| 262 |
+
if url and url.startswith('/') and settings.BASE_URL:
|
| 263 |
+
url = f"{settings.BASE_URL}{url}"
|
| 264 |
|
| 265 |
img_dict = convert_image_to_dict(caption, url)
|
| 266 |
return schemas.ImageOut(**img_dict)
|
py_backend/app/routers/upload.py
CHANGED
|
@@ -36,7 +36,6 @@ def convert_image_to_dict(img, image_url):
|
|
| 36 |
print(f"Warning: Error processing countries for image {img.image_id}: {e}")
|
| 37 |
countries_list = []
|
| 38 |
|
| 39 |
-
# Handle image URL - if BASE_URL is set, use it; otherwise keep relative URLs
|
| 40 |
if image_url and image_url.startswith('/') and settings.BASE_URL:
|
| 41 |
image_url = f"{settings.BASE_URL}{image_url}"
|
| 42 |
|
|
@@ -123,7 +122,7 @@ async def upload_image(
|
|
| 123 |
raise HTTPException(500, f"Failed to save image to database: {str(e)}")
|
| 124 |
|
| 125 |
try:
|
| 126 |
-
url = storage.
|
| 127 |
except Exception as e:
|
| 128 |
url = f"/api/images/{img.image_id}/file"
|
| 129 |
|
|
@@ -171,7 +170,7 @@ async def copy_image_for_contribution(
|
|
| 171 |
)
|
| 172 |
|
| 173 |
try:
|
| 174 |
-
url = storage.
|
| 175 |
except Exception as e:
|
| 176 |
url = f"/api/images/{new_img.image_id}/file"
|
| 177 |
|
|
@@ -196,9 +195,16 @@ async def get_image_file(image_id: str, db: Session = Depends(get_db)):
|
|
| 196 |
|
| 197 |
try:
|
| 198 |
if hasattr(storage, 's3') and settings.STORAGE_PROVIDER != "local":
|
| 199 |
-
print(f"π Using S3 storage")
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
else:
|
| 203 |
print(f"π Using local storage")
|
| 204 |
import os
|
|
@@ -269,7 +275,7 @@ def update_image_metadata(
|
|
| 269 |
print(f"DEBUG: Metadata update successful for image {image_id}")
|
| 270 |
|
| 271 |
try:
|
| 272 |
-
url = storage.
|
| 273 |
except Exception:
|
| 274 |
url = f"/api/images/{img.image_id}/file"
|
| 275 |
|
|
|
|
| 36 |
print(f"Warning: Error processing countries for image {img.image_id}: {e}")
|
| 37 |
countries_list = []
|
| 38 |
|
|
|
|
| 39 |
if image_url and image_url.startswith('/') and settings.BASE_URL:
|
| 40 |
image_url = f"{settings.BASE_URL}{image_url}"
|
| 41 |
|
|
|
|
| 122 |
raise HTTPException(500, f"Failed to save image to database: {str(e)}")
|
| 123 |
|
| 124 |
try:
|
| 125 |
+
url = storage.get_object_url(key)
|
| 126 |
except Exception as e:
|
| 127 |
url = f"/api/images/{img.image_id}/file"
|
| 128 |
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
try:
|
| 173 |
+
url = storage.get_object_url(new_key)
|
| 174 |
except Exception as e:
|
| 175 |
url = f"/api/images/{new_img.image_id}/file"
|
| 176 |
|
|
|
|
| 195 |
|
| 196 |
try:
|
| 197 |
if hasattr(storage, 's3') and settings.STORAGE_PROVIDER != "local":
|
| 198 |
+
print(f"π Using S3 storage - redirecting to S3 URL")
|
| 199 |
+
try:
|
| 200 |
+
url = storage.get_object_url(img.file_key)
|
| 201 |
+
from fastapi.responses import RedirectResponse
|
| 202 |
+
return RedirectResponse(url=url, status_code=302)
|
| 203 |
+
except Exception as e:
|
| 204 |
+
print(f"β Failed to generate S3 URL: {e}")
|
| 205 |
+
# Fallback to direct S3 object serving
|
| 206 |
+
response = storage.s3.get_object(Bucket=settings.S3_BUCKET, Key=img.file_key)
|
| 207 |
+
content = response['Body'].read()
|
| 208 |
else:
|
| 209 |
print(f"π Using local storage")
|
| 210 |
import os
|
|
|
|
| 275 |
print(f"DEBUG: Metadata update successful for image {image_id}")
|
| 276 |
|
| 277 |
try:
|
| 278 |
+
url = storage.get_object_url(img.file_key)
|
| 279 |
except Exception:
|
| 280 |
url = f"/api/images/{img.image_id}/file"
|
| 281 |
|