toolshed/backend/files/media_urls.py

30 lines
914 B
Python
Raw Normal View History

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
from rest_framework.decorators import api_view
from rest_framework.response import Response
from files.models import File
2023-10-24 22:44:58 +00:00
# TODO check file permissions here
@swagger_auto_schema(method='GET', auto_schema=None)
2023-10-23 21:51:30 +00:00
@api_view(['GET'])
def media_urls(request, id, format=None):
try:
file = File.objects.get(file=id)
return Response(status=status.HTTP_200_OK,
content_type=file.mime_type,
headers={
'X-Accel-Redirect': f'/redirect_media/{id}',
'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 = [
path('<path:id>', media_urls),
]