2023-11-01 01:42:34 +00:00
|
|
|
from django.http import HttpResponse
|
2023-10-23 21:51:30 +00:00
|
|
|
from django.urls import path
|
2023-10-24 22:44:58 +00:00
|
|
|
from drf_yasg.utils import swagger_auto_schema
|
2023-10-23 21:51:30 +00:00
|
|
|
from rest_framework import status
|
2023-11-01 03:32:03 +00:00
|
|
|
from rest_framework.decorators import api_view, permission_classes, authentication_classes
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-10-23 21:51:30 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2023-11-01 03:32:03 +00:00
|
|
|
from authentication.signature_auth import SignatureAuthentication
|
2023-10-23 21:51:30 +00:00
|
|
|
from files.models import File
|
|
|
|
|
|
|
|
|
2023-10-24 22:44:58 +00:00
|
|
|
@swagger_auto_schema(method='GET', auto_schema=None)
|
2023-10-23 21:51:30 +00:00
|
|
|
@api_view(['GET'])
|
2023-11-01 03:32:03 +00:00
|
|
|
@permission_classes([IsAuthenticated])
|
|
|
|
@authentication_classes([SignatureAuthentication])
|
|
|
|
def media_urls(request, hash_path):
|
2023-10-23 21:51:30 +00:00
|
|
|
try:
|
2023-11-01 03:32:03 +00:00
|
|
|
file = File.objects.filter(connected_items__owner__in=request.user.friends_or_self()).distinct().get(
|
|
|
|
file=hash_path)
|
|
|
|
|
2023-11-01 01:42:34 +00:00
|
|
|
return HttpResponse(status=status.HTTP_200_OK,
|
|
|
|
content_type=file.mime_type,
|
|
|
|
headers={
|
2023-11-01 03:32:03 +00:00
|
|
|
'X-Accel-Redirect': f'/redirect_media/{hash_path}',
|
2023-11-01 01:42:34 +00:00
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
}) # TODO Expires and Cache-Control
|
2023-10-23 21:51:30 +00:00
|
|
|
|
|
|
|
except File.DoesNotExist:
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
|
|
|
|
urlpatterns = [
|
2023-11-01 03:32:03 +00:00
|
|
|
path('<path:hash_path>', media_urls),
|
2023-10-23 21:51:30 +00:00
|
|
|
]
|