fix(api): ensure non ascii paths are handled by X-Accel-Redirect (#2861)

This commit is contained in:
Jonas L 2024-01-01 12:58:19 +01:00 committed by GitHub
parent 6f84328380
commit 0ce63f3bf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import os
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.utils.encoding import iri_to_uri
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.serializers import IntegerField from rest_framework.serializers import IntegerField
@ -23,5 +24,9 @@ class FileViewSet(viewsets.ModelViewSet):
response = HttpResponse() response = HttpResponse()
response["Content-Type"] = file.mime response["Content-Type"] = file.mime
response["X-Accel-Redirect"] = os.path.join("/api/_media", file.filepath)
# HTTP headers must be USASCII encoded, or Nginx might not find the file and
# will return a 404.
redirect_uri = iri_to_uri(os.path.join("/api/_media", file.filepath))
response["X-Accel-Redirect"] = redirect_uri
return response return response