Skip to content

Commit 3d48993

Browse files
committed
feat(config): add SAVE_IMAGES_LOCALLY option
Allow disabling local image storage when using Open WebUI exclusively. When false, images are only uploaded to Open WebUI with no local backup.
1 parent 1d1ce00 commit 3d48993

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ GEMINI_API_KEY=
2626
# Path to store generated images
2727
STORAGE_PATH=./generated_images
2828

29+
# Save images locally (default: true)
30+
# Set to false if using Open WebUI integration exclusively
31+
SAVE_IMAGES_LOCALLY=true
32+
2933
# IMPORTANT: URL where THIS API serves images (not Open WebUI or other services!)
3034
# This should be the URL where this image-generation API is accessible.
3135
# Examples:

CONFIGURATION.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ Complete configuration reference for the Image Generation API.
3737
- Default: `./generated_images`
3838
- Docker: Use volume mount (e.g., `/app/generated_images`)
3939

40+
**`SAVE_IMAGES_LOCALLY`**
41+
- Whether to save images to local storage
42+
- Default: `true`
43+
- Set to `false` if using Open WebUI integration exclusively
44+
- When `false`, images are only uploaded to Open WebUI (no local backup)
45+
4046
**`IMAGE_BASE_URL`**
4147
- Public base URL for serving images
4248
- Default: `http://localhost:8000`

app/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Settings(BaseSettings):
4343
# Storage Configuration
4444
STORAGE_PATH: str = "./generated_images"
4545
IMAGE_BASE_URL: str = "http://localhost:8000" # URL where this API serves images
46+
SAVE_IMAGES_LOCALLY: bool = True # Set to false if using Open WebUI exclusively
4647

4748
# Security (Optional)
4849
API_BEARER_TOKEN: str | None = None

app/services/storage_service.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self):
2222

2323
async def save_image(self, image_data: bytes, extension: str = "png") -> str:
2424
"""
25-
Save image bytes to local storage and optionally upload to Open WebUI.
25+
Save image bytes to local storage and/or upload to Open WebUI.
2626
2727
Args:
2828
image_data: Raw image bytes
@@ -32,24 +32,26 @@ async def save_image(self, image_data: bytes, extension: str = "png") -> str:
3232
Public URL to access the image (Open WebUI URL if configured, else local)
3333
"""
3434
filename = f"{uuid.uuid4()}.{extension}"
35-
filepath = self.storage_path / filename
36-
37-
# Always save locally first
38-
async with aiofiles.open(filepath, "wb") as f:
39-
await f.write(image_data)
40-
4135
local_url = f"{settings.IMAGE_BASE_URL.rstrip('/')}/images/{filename}"
4236

37+
# Save locally if configured
38+
if settings.SAVE_IMAGES_LOCALLY:
39+
filepath = self.storage_path / filename
40+
async with aiofiles.open(filepath, "wb") as f:
41+
await f.write(image_data)
42+
4343
# Upload to Open WebUI if configured
4444
if settings.openwebui_available:
4545
try:
4646
from app.services.openwebui_service import get_openwebui_service
4747

4848
webui_service = get_openwebui_service()
49-
webui_url = await webui_service.upload_image(image_data, filename)
50-
return webui_url
49+
return await webui_service.upload_image(image_data, filename)
5150
except Exception as e:
52-
logger.warning(f"Open WebUI upload failed, using local URL: {e}")
51+
logger.warning(f"Open WebUI upload failed: {e}")
52+
if settings.SAVE_IMAGES_LOCALLY:
53+
return local_url
54+
raise
5355

5456
return local_url
5557

0 commit comments

Comments
 (0)