Refactor image processing and add image file serving functionality
- Updated image directory handling to prioritize mounted volume for persistence. - Enhanced logging for directory write tests and fallback mechanisms. - Introduced a new endpoint to serve image files directly from local paths. - Added error handling for file serving, including checks for file existence and readability. - Updated the frontend to include a new ContentView component and corresponding route.
This commit is contained in:
@@ -367,6 +367,76 @@ class ImagesViewSet(SiteSectorModelViewSet):
|
||||
else:
|
||||
serializer.save()
|
||||
|
||||
@action(detail=True, methods=['get'], url_path='file', url_name='image_file')
|
||||
def serve_image_file(self, request, pk=None):
|
||||
"""
|
||||
Serve image file from local path via URL
|
||||
GET /api/v1/writer/images/{id}/file/
|
||||
"""
|
||||
import os
|
||||
from django.http import FileResponse, Http404
|
||||
from django.conf import settings
|
||||
|
||||
try:
|
||||
# Get image directly without account filtering for file serving
|
||||
# This allows public access to image files
|
||||
try:
|
||||
image = Images.objects.get(pk=pk)
|
||||
except Images.DoesNotExist:
|
||||
return Response({
|
||||
'error': 'Image not found'
|
||||
}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
# Check if image has a local path
|
||||
if not image.image_path:
|
||||
return Response({
|
||||
'error': 'No local file path available for this image'
|
||||
}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
file_path = image.image_path
|
||||
|
||||
# Verify file exists
|
||||
if not os.path.exists(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):
|
||||
return Response({
|
||||
'error': 'Image file is not readable'
|
||||
}, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
# Determine content type from file extension
|
||||
import mimetypes
|
||||
content_type, _ = mimetypes.guess_type(file_path)
|
||||
if not content_type:
|
||||
content_type = 'image/png' # Default to PNG
|
||||
|
||||
# Serve the file
|
||||
try:
|
||||
return FileResponse(
|
||||
open(file_path, 'rb'),
|
||||
content_type=content_type,
|
||||
filename=os.path.basename(file_path)
|
||||
)
|
||||
except Exception as e:
|
||||
return Response({
|
||||
'error': f'Failed to serve file: {str(e)}'
|
||||
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
except Images.DoesNotExist:
|
||||
return Response({
|
||||
'error': 'Image not found'
|
||||
}, status=status.HTTP_404_NOT_FOUND)
|
||||
except Exception as e:
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.error(f"Error serving image file: {str(e)}", exc_info=True)
|
||||
return Response({
|
||||
'error': f'Failed to serve image: {str(e)}'
|
||||
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
@action(detail=False, methods=['post'], url_path='auto_generate', url_name='auto_generate_images')
|
||||
def auto_generate_images(self, request):
|
||||
"""Auto-generate images for tasks using AI"""
|
||||
|
||||
Reference in New Issue
Block a user