F(backend): add public media derivatives pipeline
This commit is contained in:
@@ -5,6 +5,7 @@ from typing import Optional
|
||||
|
||||
from apps.blog.api.schemas import AuthorSchema
|
||||
from apps.gallery.models import Gallery
|
||||
from core.media import BLUR_VARIANT, PREVIEW_VARIANT, derivative_url
|
||||
|
||||
|
||||
class GallerySchema(ModelSchema):
|
||||
@@ -12,12 +13,34 @@ class GallerySchema(ModelSchema):
|
||||
uploaded_by: AuthorSchema
|
||||
file_size_mb: float
|
||||
markdown_url: str
|
||||
absolute_image_url: Optional[str] = None
|
||||
absolute_image_preview_url: Optional[str] = None
|
||||
absolute_image_blur_url: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
model = Gallery
|
||||
model_fields = ['id', 'title', 'description', 'image', 'alt_text',
|
||||
'width', 'height', 'is_public', 'created_at']
|
||||
|
||||
@staticmethod
|
||||
def resolve_absolute_image_url(obj, context):
|
||||
request = context["request"]
|
||||
if obj.image and hasattr(obj.image, "url"):
|
||||
return request.build_absolute_uri(obj.image.url)
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def resolve_absolute_image_preview_url(obj, context):
|
||||
request = context["request"]
|
||||
url = derivative_url(obj.image, PREVIEW_VARIANT)
|
||||
return request.build_absolute_uri(url) if url else None
|
||||
|
||||
@staticmethod
|
||||
def resolve_absolute_image_blur_url(obj, context):
|
||||
request = context["request"]
|
||||
url = derivative_url(obj.image, BLUR_VARIANT)
|
||||
return request.build_absolute_uri(url) if url else None
|
||||
|
||||
|
||||
class GalleryCreateSchema(Schema):
|
||||
"""Payload for creating a gallery entry."""
|
||||
|
||||
@@ -63,6 +63,7 @@ def upload_image(request, file: UploadedFile = File(...), data: GalleryCreateSch
|
||||
alt_text=data.alt_text if data else "",
|
||||
is_public=data.is_public if data else True
|
||||
)
|
||||
gallery_item._defer_image_processing = True
|
||||
|
||||
# Save image
|
||||
filename = f"gallery/{uuid.uuid4().hex}.{file.name.split('.')[-1]}"
|
||||
|
||||
Reference in New Issue
Block a user