c3lf-system-3/core/files/media_v2.py

81 lines
3.3 KiB
Python
Raw Normal View History

2023-12-13 08:30:46 +00:00
from datetime import datetime, timedelta
2023-11-23 22:17:20 +00:00
from coverage.annotate import os
from django.http import HttpResponse
from django.urls import path
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
2023-12-13 08:30:46 +00:00
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
2023-11-23 22:17:20 +00:00
from rest_framework.response import Response
from core.settings import MEDIA_ROOT
from files.models import File
@swagger_auto_schema(method='GET', auto_schema=None)
@api_view(['GET'])
2023-12-13 08:30:46 +00:00
@permission_classes([IsAuthenticated])
2023-12-12 21:20:38 +00:00
def media_urls(request, hash):
2023-11-23 22:17:20 +00:00
try:
2023-12-13 08:30:46 +00:00
if request.META.get('HTTP_IF_NONE_MATCH') and request.META.get('HTTP_IF_NONE_MATCH') == hash:
return HttpResponse(status=status.HTTP_304_NOT_MODIFIED)
2023-12-12 21:20:38 +00:00
file = File.objects.get(hash=hash)
hash_path = file.file
2023-11-23 22:17:20 +00:00
return HttpResponse(status=status.HTTP_200_OK,
content_type=file.mime_type,
headers={
'X-Accel-Redirect': f'/redirect_media/{hash_path}',
'Access-Control-Allow-Origin': '*',
2023-12-13 08:30:46 +00:00
'Cache-Control': 'max-age=31536000, private',
'Expires': datetime.utcnow() + timedelta(days=365),
'Age': 0,
'ETag': file.hash,
})
2023-11-23 22:17:20 +00:00
except File.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
@swagger_auto_schema(method='GET', auto_schema=None)
@api_view(['GET'])
2023-12-13 08:30:46 +00:00
@permission_classes([IsAuthenticated])
2023-12-12 21:20:38 +00:00
def thumbnail_urls(request, size, hash):
2023-11-23 22:17:20 +00:00
if size not in [32, 64, 256]:
return Response(status=status.HTTP_404_NOT_FOUND)
2023-12-13 08:30:46 +00:00
if request.META.get('HTTP_IF_NONE_MATCH') and request.META.get('HTTP_IF_NONE_MATCH') == hash + "_" + str(size):
return HttpResponse(status=status.HTTP_304_NOT_MODIFIED)
2023-11-23 22:17:20 +00:00
try:
2023-12-12 21:20:38 +00:00
file = File.objects.get(hash=hash)
hash_path = file.file
2023-11-23 22:17:20 +00:00
if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'):
from PIL import Image
2023-12-12 21:20:38 +00:00
image = Image.open(file.file)
image.thumbnail((size, size))
2023-12-12 23:54:59 +00:00
rgb_image = image.convert('RGB')
thumb_dir = os.path.dirname(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}')
if not os.path.exists(thumb_dir):
os.makedirs(thumb_dir)
rgb_image.save(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}', 'jpeg', quality=90)
2023-11-23 22:17:20 +00:00
return HttpResponse(status=status.HTTP_200_OK,
2023-12-12 23:54:59 +00:00
content_type="image/jpeg",
2023-11-23 22:17:20 +00:00
headers={
'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}',
'Access-Control-Allow-Origin': '*',
2023-12-13 08:30:46 +00:00
'Cache-Control': 'max-age=31536000, private',
'Expires': datetime.utcnow() + timedelta(days=365),
'Age': 0,
'ETag': file.hash + "_" + str(size),
})
2023-11-23 22:17:20 +00:00
except File.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
urlpatterns = [
2023-12-12 21:20:38 +00:00
path('<int:size>/<path:hash>/', thumbnail_urls),
path('<path:hash>/', media_urls),
2023-11-23 22:17:20 +00:00
]