2024-01-07 20:06:06 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
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
|
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
from core.settings import MEDIA_ROOT
|
|
|
|
from files.models import File
|
2024-01-14 15:01:32 +00:00
|
|
|
from mail.models import EmailAttachment
|
2024-01-07 20:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema(method='GET', auto_schema=None)
|
|
|
|
@api_view(['GET'])
|
|
|
|
@permission_classes([IsAuthenticated])
|
|
|
|
def media_urls(request, hash):
|
|
|
|
try:
|
|
|
|
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)
|
|
|
|
|
2024-01-14 15:01:32 +00:00
|
|
|
file = File.objects.filter(hash=hash).first()
|
|
|
|
attachment = EmailAttachment.objects.filter(hash=hash).first()
|
|
|
|
file = file if file else attachment
|
|
|
|
if not file:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2024-01-07 20:06:06 +00:00
|
|
|
hash_path = file.file
|
|
|
|
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': '*',
|
|
|
|
'Cache-Control': 'max-age=31536000, private',
|
|
|
|
'Expires': datetime.utcnow() + timedelta(days=365),
|
|
|
|
'Age': 0,
|
|
|
|
'ETag': file.hash,
|
|
|
|
})
|
|
|
|
except File.DoesNotExist:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2024-01-14 15:01:32 +00:00
|
|
|
except EmailAttachment.DoesNotExist:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2024-01-07 20:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema(method='GET', auto_schema=None)
|
|
|
|
@api_view(['GET'])
|
|
|
|
@permission_classes([IsAuthenticated])
|
|
|
|
def thumbnail_urls(request, size, hash):
|
|
|
|
if size not in [32, 64, 256]:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
|
|
|
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)
|
|
|
|
try:
|
2024-01-14 15:01:32 +00:00
|
|
|
file = File.objects.filter(hash=hash).first()
|
|
|
|
attachment = EmailAttachment.objects.filter(hash=hash).first()
|
|
|
|
file = file if file else attachment
|
|
|
|
if not file:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2024-01-07 20:06:06 +00:00
|
|
|
hash_path = file.file
|
|
|
|
if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'):
|
|
|
|
from PIL import Image
|
|
|
|
image = Image.open(file.file)
|
|
|
|
image.thumbnail((size, size))
|
|
|
|
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)
|
|
|
|
|
|
|
|
return HttpResponse(status=status.HTTP_200_OK,
|
|
|
|
content_type="image/jpeg",
|
|
|
|
headers={
|
|
|
|
'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}',
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'Cache-Control': 'max-age=31536000, private',
|
|
|
|
'Expires': datetime.utcnow() + timedelta(days=365),
|
|
|
|
'Age': 0,
|
|
|
|
'ETag': file.hash + "_" + str(size),
|
|
|
|
})
|
|
|
|
|
|
|
|
except File.DoesNotExist:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2024-01-14 15:01:32 +00:00
|
|
|
except EmailAttachment.DoesNotExist:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
2024-01-07 20:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
urlpatterns = [
|
|
|
|
path('<int:size>/<path:hash>/', thumbnail_urls),
|
|
|
|
path('<path:hash>/', media_urls),
|
|
|
|
]
|