From 153d79f126d7b7e8d6480c3ae414aadbe7a94eef Mon Sep 17 00:00:00 2001 From: jedi Date: Mon, 20 Nov 2023 21:01:03 +0100 Subject: [PATCH] serve images via X-Sendfile --- core/core/urls.py | 1 + core/files/{media_urls.py => media_v1.py} | 26 +++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) rename core/files/{media_urls.py => media_v1.py} (76%) diff --git a/core/core/urls.py b/core/core/urls.py index 50e4d91..cbc52c0 100644 --- a/core/core/urls.py +++ b/core/core/urls.py @@ -23,5 +23,6 @@ urlpatterns = [ path('admin/', admin.site.urls), path('api/1/', include('inventory.api_v1')), path('api/1/', include('files.api_v1')), + path('api/1/', include('files.media_v1')), path('api/', get_info), ] diff --git a/core/files/media_urls.py b/core/files/media_v1.py similarity index 76% rename from core/files/media_urls.py rename to core/files/media_v1.py index 78961d8..6dabc54 100644 --- a/core/files/media_urls.py +++ b/core/files/media_v1.py @@ -3,7 +3,7 @@ from django.http import HttpResponse from django.urls import path from drf_yasg.utils import swagger_auto_schema from rest_framework import status -from rest_framework.decorators import api_view +from rest_framework.decorators import api_view, permission_classes, authentication_classes from rest_framework.response import Response from core.settings import MEDIA_ROOT @@ -12,10 +12,12 @@ from files.models import File @swagger_auto_schema(method='GET', auto_schema=None) @api_view(['GET']) -def media_urls(request, hash_path): +@permission_classes([]) +@authentication_classes([]) +def media_urls(request, hash): try: - file = File.objects.get(file=hash_path) - + file = File.objects.get(hash=hash) + hash_path = file.file return HttpResponse(status=status.HTTP_200_OK, content_type=file.mime_type, headers={ @@ -29,11 +31,13 @@ def media_urls(request, hash_path): @swagger_auto_schema(method='GET', auto_schema=None) @api_view(['GET']) -def thumbnail_urls(request, size, hash_path): - if size not in [32, 64, 256]: - return Response(status=status.HTTP_404_NOT_FOUND) +@permission_classes([]) +@authentication_classes([]) +def thumbnail_urls(request, hash): + size = 200 try: - file = File.objects.get(file=hash_path) + file = File.objects.get(hash=hash) + hash_path = file.file if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'): from PIL import Image iamge = Image.open(file.file) @@ -52,6 +56,6 @@ def thumbnail_urls(request, size, hash_path): urlpatterns = [ - path('/', thumbnail_urls), - path('', media_urls), -] + path('thumbs/', thumbnail_urls), + path('images/', media_urls), +] \ No newline at end of file