Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
make it work with thumbor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreJunod committed Nov 7, 2023
1 parent 7144a75 commit b098f8a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion geocity/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
5 changes: 5 additions & 0 deletions geocity/apps/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@
views.image_display,
name="image_display",
),
path(
"image/thumbor/<int:form_id>/<str:image_name>",
views.image_thumbor_display,
name="image_thumbor_display",
),
]
54 changes: 33 additions & 21 deletions geocity/apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion geocity/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down

0 comments on commit b098f8a

Please sign in to comment.