serve images via X-Sendfile

This commit is contained in:
j3d1 2023-11-20 21:01:03 +01:00
parent 27589a09bd
commit 153d79f126
2 changed files with 16 additions and 11 deletions

View file

@ -23,5 +23,6 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('api/1/', include('inventory.api_v1')), path('api/1/', include('inventory.api_v1')),
path('api/1/', include('files.api_v1')), path('api/1/', include('files.api_v1')),
path('api/1/', include('files.media_v1')),
path('api/', get_info), path('api/', get_info),
] ]

View file

@ -3,7 +3,7 @@ from django.http import HttpResponse
from django.urls import path from django.urls import path
from drf_yasg.utils import swagger_auto_schema from drf_yasg.utils import swagger_auto_schema
from rest_framework import status 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 rest_framework.response import Response
from core.settings import MEDIA_ROOT from core.settings import MEDIA_ROOT
@ -12,10 +12,12 @@ from files.models import File
@swagger_auto_schema(method='GET', auto_schema=None) @swagger_auto_schema(method='GET', auto_schema=None)
@api_view(['GET']) @api_view(['GET'])
def media_urls(request, hash_path): @permission_classes([])
@authentication_classes([])
def media_urls(request, hash):
try: 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, return HttpResponse(status=status.HTTP_200_OK,
content_type=file.mime_type, content_type=file.mime_type,
headers={ headers={
@ -29,11 +31,13 @@ def media_urls(request, hash_path):
@swagger_auto_schema(method='GET', auto_schema=None) @swagger_auto_schema(method='GET', auto_schema=None)
@api_view(['GET']) @api_view(['GET'])
def thumbnail_urls(request, size, hash_path): @permission_classes([])
if size not in [32, 64, 256]: @authentication_classes([])
return Response(status=status.HTTP_404_NOT_FOUND) def thumbnail_urls(request, hash):
size = 200
try: 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}'): if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'):
from PIL import Image from PIL import Image
iamge = Image.open(file.file) iamge = Image.open(file.file)
@ -52,6 +56,6 @@ def thumbnail_urls(request, size, hash_path):
urlpatterns = [ urlpatterns = [
path('<int:size>/<path:hash_path>', thumbnail_urls), path('thumbs/<path:hash>', thumbnail_urls),
path('<path:hash_path>', media_urls), path('images/<path:hash>', media_urls),
] ]