toolshed/backend/files/media_urls.py
jedi 0cdbc3e4a2
All checks were successful
continuous-integration/drone/push Build is passing
add mime_type header to /media endpoint
2023-10-24 23:09:17 +02:00

25 lines
714 B
Python

from django.urls import path
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from files.models import File
@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}'
}) # TODO Expires and Cache-Control
except File.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
urlpatterns = [
path('<path:id>', media_urls),
]