feat!: use nginx to serve media files (#2860)

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.
This commit is contained in:
Jonas L 2023-12-30 18:59:15 +01:00 committed by GitHub
parent 8406d520d7
commit 4603c1759f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 53 additions and 10 deletions

View file

@ -1,7 +1,6 @@
import os
from django.conf import settings
from django.http import FileResponse
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework import viewsets
from rest_framework.decorators import action
@ -21,5 +20,8 @@ class FileViewSet(viewsets.ModelViewSet):
pk = IntegerField().to_internal_value(data=pk)
file = get_object_or_404(File, pk=pk)
path = os.path.join(settings.CONFIG.storage.path, file.filepath)
return FileResponse(open(path, "rb"), content_type=file.mime)
response = HttpResponse()
response["Content-Type"] = file.mime
response["X-Accel-Redirect"] = os.path.join("/api/_media", file.filepath)
return response