From 7144a750ca99d63099f62a00a5ac93b046f3b59f Mon Sep 17 00:00:00 2001 From: Alexandre Junod Date: Mon, 6 Nov 2023 16:30:59 +0100 Subject: [PATCH] wip, thumbor on one route, with params interpreted as thumbor --- docker-compose.yml | 4 ++++ geocity/apps/api/views.py | 36 ++++++++++++++++++++++++++---------- geocity/settings.py | 6 ++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4ca85b2c0..b84ff774a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -77,6 +77,7 @@ services: networks: - isolated - default + - geocity_network # Builder for the geocity_qgis image. qgis: @@ -113,3 +114,6 @@ networks: # Use this for containers that should only be able to communicate with Django isolated: name: ${ISOLATED_NETWORK_NAME} + geocity_network: + name: geocity_network + driver: bridge diff --git a/geocity/apps/api/views.py b/geocity/apps/api/views.py index 6cc6b4013..810459f4a 100644 --- a/geocity/apps/api/views.py +++ b/geocity/apps/api/views.py @@ -1,6 +1,7 @@ import datetime import os +import requests from django.conf import settings from django.contrib.auth.models import AnonymousUser, User from django.db.models import F, Prefetch, Q @@ -474,19 +475,34 @@ def get_queryset(self): def image_display(request, form_id, image_name): - image_dir = settings.PRIVATE_MEDIA_ROOT + 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 - image_path = os.path.join( - image_dir, f"permit_requests_uploads/{form_id}/{image_name}" - ) + # print(query_params) - # 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 + if no_thumbor: + image_dir = settings.PRIVATE_MEDIA_ROOT + + image_path = os.path.join( + image_dir, f"permit_requests_uploads/{form_id}/{image_name}" + ) + + # 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) else: - return JsonResponse({"message": "Image non trouvée."}, status=404) + 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 class AgendaViewSet(viewsets.ReadOnlyModelViewSet): diff --git a/geocity/settings.py b/geocity/settings.py index d7fadd7e5..6d033c535 100644 --- a/geocity/settings.py +++ b/geocity/settings.py @@ -266,6 +266,7 @@ "IP_WHITELIST", "NETWORK_WHITELIST", "LOGOUT_REDIRECT_HOSTNAME_WHITELIST", + "THUMBOR_SERVICE_URL", ), } @@ -411,6 +412,11 @@ "localhost,geocity.ch", "Domaines autorisés à la redirection après logout", ), + "THUMBOR_SERVICE_URL": ( + "http://host.docker.internal:8083", + "URL du service thumbor (https://github.com/thumbor/thumbor), recommandé pour l'optimisation des images du module Agenda", + str, + ), } TEMPLATES = [