diff --git a/geocity/apps/api/serializers.py b/geocity/apps/api/serializers.py index 406b9878b..31ca08113 100644 --- a/geocity/apps/api/serializers.py +++ b/geocity/apps/api/serializers.py @@ -908,7 +908,7 @@ def get_agenda_form_fields(value, detailed, available_filters): "permit_requests_uploads/", "" ) result["properties"]["poster"] = { - "src": f"http://localhost:9095/rest/image/{file_name}", + "src": f"http://localhost:9095/rest/image/thumbor/{file_name}", "width": "1365", "height": "2048", } diff --git a/geocity/apps/api/urls.py b/geocity/apps/api/urls.py index 4040beb9d..216dbb2fb 100644 --- a/geocity/apps/api/urls.py +++ b/geocity/apps/api/urls.py @@ -23,4 +23,9 @@ views.image_display, name="image_display", ), + path( + "image/thumbor//", + views.image_thumbor_display, + name="image_thumbor_display", + ), ] diff --git a/geocity/apps/api/views.py b/geocity/apps/api/views.py index 810459f4a..6489a3fed 100644 --- a/geocity/apps/api/views.py +++ b/geocity/apps/api/views.py @@ -2,6 +2,7 @@ import os import requests +from constance import config from django.conf import settings from django.contrib.auth.models import AnonymousUser, User from django.db.models import F, Prefetch, Q @@ -475,34 +476,45 @@ def get_queryset(self): def image_display(request, form_id, image_name): - no_thumbor = request.GET.get("no_thumbor", False) height = request.GET.get("height", None) width = request.GET.get("width", None) - # query_params = request.query_params - # print(query_params) + image_dir = settings.PRIVATE_MEDIA_ROOT - if no_thumbor: - image_dir = settings.PRIVATE_MEDIA_ROOT + image_path = os.path.join( + image_dir, f"permit_requests_uploads/{form_id}/{image_name}" + ) - image_path = os.path.join( - image_dir, f"permit_requests_uploads/{form_id}/{image_name}" - ) + # TODO: Secure access + if os.path.exists(image_path): + image_file = open(image_path, "rb") + response = FileResponse(image_file, content_type="image/jpeg") + return response + else: + return JsonResponse({"message": "Image non trouvée."}, status=404) - # TODO: Ajouter de la sécurité afin de savoir si l'image peut-être affichée ou non - if os.path.exists(image_path): - image_file = open(image_path, "rb") - response = FileResponse(image_file, content_type="image/jpeg") - return response - else: - return JsonResponse({"message": "Image non trouvée."}, status=404) + +def image_thumbor_display(request, form_id, image_name): + width = request.GET.get("width", None) + height = request.GET.get("height", None) + + INTERNAL_WEB_ROOT_URL = "http://web:9000" + image_url = f"{INTERNAL_WEB_ROOT_URL}/rest/image/{form_id}/{image_name}" + + thumbor_params = f"unsafe" + + if width and height: + thumbor_params += f"/{width}x{height}" else: - INTERNAL_WEB_ROOT_URL = "http://web:9000" - image_url = f"{INTERNAL_WEB_ROOT_URL}/agenda/image/display/permit_requests_uploads/{form_id}/{image_name}?no_thumbor=true" - thumbor_params = "300x200" - resp = requests.get(f"http://nginx-proxy/unsafe/{thumbor_params}/{image_url}") - thumbor_response = FileResponse(resp, content_type="image/jpeg") - return thumbor_response + thumbor_params += "/397x562" + + response = requests.get( + f"{config.THUMBOR_SERVICE_URL}/{thumbor_params}/{image_url}" + ) + # TODO: support PNG or other types + thumbor_response = FileResponse(response, content_type="image/jpeg") + + return thumbor_response class AgendaViewSet(viewsets.ReadOnlyModelViewSet): diff --git a/geocity/settings.py b/geocity/settings.py index 6d033c535..6941fdc9d 100644 --- a/geocity/settings.py +++ b/geocity/settings.py @@ -413,7 +413,7 @@ "Domaines autorisés à la redirection après logout", ), "THUMBOR_SERVICE_URL": ( - "http://host.docker.internal:8083", + "http://nginx-proxy", "URL du service thumbor (https://github.com/thumbor/thumbor), recommandé pour l'optimisation des images du module Agenda", str, ),