image url

This commit is contained in:
Desktop
2025-11-12 10:46:01 +05:00
parent a61471c34a
commit 1a51e6bc39
3 changed files with 142 additions and 84 deletions

View File

@@ -527,67 +527,23 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
}
)
# Download and save image to /data/app/images
# Download and save image to /data/app/igny8/frontend/public/images/ai-images
saved_file_path = None
if image_url:
try:
import os
import requests
from django.conf import settings
from pathlib import Path
import time
# Create images directory if it doesn't exist
# Use frontend/public/images/ai-images/ for web accessibility (like /images/logo/)
# This allows images to be served via app.igny8.com/images/ai-images/
write_test_passed = False
images_dir = None
# Use the correct path: /data/app/igny8/frontend/public/images/ai-images
# This is web-accessible via /images/ai-images/ (Vite serves from public/)
images_dir = '/data/app/igny8/frontend/public/images/ai-images'
# Try frontend/public/images/ai-images/ first (web-accessible)
try:
# Use absolute path: /data/app/igny8/frontend/public/images/ai-images/
# This matches the structure where frontend is at project root level
images_dir = '/data/app/igny8/frontend/public/images/ai-images'
os.makedirs(images_dir, exist_ok=True)
# Test write access
test_file = os.path.join(images_dir, '.write_test')
with open(test_file, 'w') as f:
f.write('test')
os.remove(test_file)
write_test_passed = True
logger.info(f"[process_image_generation_queue] Image {image_id} - Directory writable (web-accessible): {images_dir}")
except Exception as web_dir_error:
logger.warning(f"[process_image_generation_queue] Image {image_id} - Web-accessible directory not writable: {images_dir}, error: {web_dir_error}")
# Fallback to /data/app/igny8/images (mounted volume)
try:
images_dir = '/data/app/igny8/images'
os.makedirs(images_dir, exist_ok=True)
test_file = os.path.join(images_dir, '.write_test')
with open(test_file, 'w') as f:
f.write('test')
os.remove(test_file)
write_test_passed = True
logger.info(f"[process_image_generation_queue] Image {image_id} - Using mounted volume directory: {images_dir}")
except Exception as mounted_error:
logger.warning(f"[process_image_generation_queue] Image {image_id} - Mounted directory not writable: {images_dir}, error: {mounted_error}")
# Final fallback to /data/app/images
try:
images_dir = '/data/app/images'
os.makedirs(images_dir, exist_ok=True)
test_file = os.path.join(images_dir, '.write_test')
with open(test_file, 'w') as f:
f.write('test')
os.remove(test_file)
write_test_passed = True
logger.info(f"[process_image_generation_queue] Image {image_id} - Using fallback directory: {images_dir}")
except Exception as final_error:
logger.error(f"[process_image_generation_queue] Image {image_id} - All directories not writable. Last error: {final_error}")
raise Exception(f"None of the image directories are writable. Last error: {final_error}")
if not write_test_passed:
raise Exception(f"Failed to find writable directory for saving images")
# Create directory if it doesn't exist
os.makedirs(images_dir, exist_ok=True)
logger.info(f"[process_image_generation_queue] Image {image_id} - Using directory: {images_dir}")
# Generate filename: image_{image_id}_{timestamp}.png (or .webp for Runware)
import time
timestamp = int(time.time())
# Use webp extension if provider is Runware, otherwise png
file_ext = 'webp' if provider == 'runware' else 'png'

View File

@@ -395,39 +395,12 @@ class ImagesViewSet(SiteSectorModelViewSet):
file_path = image.image_path
# Verify file exists - if not, try alternative locations
# Verify file exists at the saved path
if not os.path.exists(file_path):
logger.warning(f"[serve_image_file] Image {pk} - File not found at saved path: {file_path}, trying alternative locations...")
# Try alternative locations based on the filename
filename = os.path.basename(file_path)
alternative_paths = [
'/data/app/igny8/frontend/public/images/ai-images/' + filename, # Primary location (web-accessible)
'/data/app/igny8/images/' + filename, # Secondary location
'/data/app/images/' + filename, # Fallback location
]
# Try each alternative path
found = False
for alt_path in alternative_paths:
if os.path.exists(alt_path):
file_path = alt_path
logger.info(f"[serve_image_file] Image {pk} - Found file at alternative location: {file_path}")
# Update database with correct path
try:
image.image_path = file_path
image.save(update_fields=['image_path'])
logger.info(f"[serve_image_file] Image {pk} - Updated database with correct path: {file_path}")
except Exception as update_error:
logger.warning(f"[serve_image_file] Image {pk} - Failed to update database path: {update_error}")
found = True
break
if not found:
logger.error(f"[serve_image_file] Image {pk} - File not found in any location. Tried: {[file_path] + alternative_paths}")
return Response({
'error': f'Image file not found at: {file_path} (also checked alternative locations)'
}, status=status.HTTP_404_NOT_FOUND)
logger.error(f"[serve_image_file] Image {pk} - File not found at saved path: {file_path}")
return Response({
'error': f'Image file not found at: {file_path}'
}, status=status.HTTP_404_NOT_FOUND)
# Check if file is readable
if not os.access(file_path, os.R_OK):