Closes #2522 To reduce the strain on the API service, we moved the media file serving to the Nginx web server. The API is still handling the authentication, but delegates the serving using the `X-Accel-Redirect` header. BREAKING CHANGE: The media file serving is now handled by Nginx instead of the API service. The `storage.path` field is now used in the Nginx configuration, so make sure to update the Nginx configuration file if you change it.
28 lines
852 B
Python
28 lines
852 B
Python
import os
|
|
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import get_object_or_404
|
|
from rest_framework import viewsets
|
|
from rest_framework.decorators import action
|
|
from rest_framework.serializers import IntegerField
|
|
|
|
from ..models import File
|
|
from ..serializers import FileSerializer
|
|
|
|
|
|
class FileViewSet(viewsets.ModelViewSet):
|
|
queryset = File.objects.all()
|
|
serializer_class = FileSerializer
|
|
model_permission_name = "file"
|
|
|
|
@action(detail=True, methods=["GET"])
|
|
def download(self, request, pk=None): # pylint: disable=invalid-name
|
|
pk = IntegerField().to_internal_value(data=pk)
|
|
|
|
file = get_object_or_404(File, pk=pk)
|
|
|
|
response = HttpResponse()
|
|
response["Content-Type"] = file.mime
|
|
response["X-Accel-Redirect"] = os.path.join("/api/_media", file.filepath)
|
|
return response
|