some improvements

This commit is contained in:
IGNY8 VPS (Salman)
2025-11-12 04:28:13 +00:00
parent 8798d06310
commit c29ecc1664
4 changed files with 196 additions and 33 deletions

View File

@@ -153,6 +153,8 @@ class ImagesSerializer(serializers.ModelSerializer):
class ContentImageSerializer(serializers.ModelSerializer):
"""Serializer for individual image in grouped content images"""
image_url = serializers.SerializerMethodField()
class Meta:
model = Images
fields = [
@@ -166,6 +168,24 @@ class ContentImageSerializer(serializers.ModelSerializer):
'created_at',
'updated_at',
]
def get_image_url(self, obj):
"""
Return proper HTTP URL for image.
Priority: If image_path exists, return file endpoint URL, otherwise return image_url (API URL).
"""
if obj.image_path:
# Return file endpoint URL for locally saved images
request = self.context.get('request')
if request:
# Build absolute URL for file endpoint
file_url = request.build_absolute_uri(f'/api/v1/writer/images/{obj.id}/file/')
return file_url
else:
# Fallback: return relative URL if no request context
return f'/api/v1/writer/images/{obj.id}/file/'
# Fallback to original image_url (API URL) if no local path
return obj.image_url
class ContentImagesGroupSerializer(serializers.Serializer):

View File

@@ -395,11 +395,47 @@ class ImagesViewSet(SiteSectorModelViewSet):
file_path = image.image_path
# Verify file exists
# Verify file exists - if not, try alternative locations
if not os.path.exists(file_path):
return Response({
'error': f'Image file not found at: {file_path}'
}, status=status.HTTP_404_NOT_FOUND)
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/images/' + filename, # Primary location
'/data/app/images/' + filename, # Fallback location
]
# Also try project-relative path
try:
from django.conf import settings
from pathlib import Path
base_dir = Path(settings.BASE_DIR) if hasattr(settings, 'BASE_DIR') else Path(__file__).resolve().parent.parent.parent
alternative_paths.append(str(base_dir / 'data' / 'app' / 'images' / filename))
except Exception:
pass
# 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)
# Check if file is readable
if not os.access(file_path, os.R_OK):
@@ -603,11 +639,15 @@ class ImagesViewSet(SiteSectorModelViewSet):
else:
overall_status = 'pending'
# Create serializer instances with request context for proper URL generation
featured_serializer = ContentImageSerializer(featured_image, context={'request': request}) if featured_image else None
in_article_serializers = [ContentImageSerializer(img, context={'request': request}) for img in in_article_images]
grouped_data.append({
'content_id': content.id,
'content_title': content.title or content.meta_title or f"Content #{content.id}",
'featured_image': ContentImageSerializer(featured_image).data if featured_image else None,
'in_article_images': [ContentImageSerializer(img).data for img in in_article_images],
'featured_image': featured_serializer.data if featured_serializer else None,
'in_article_images': [s.data for s in in_article_serializers],
'overall_status': overall_status,
})
except Content.DoesNotExist: