From 0ce63f3bf0448580170024cdde96ee351ee5c358 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Mon, 1 Jan 2024 12:58:19 +0100 Subject: [PATCH] fix(api): ensure non ascii paths are handled by X-Accel-Redirect (#2861) --- api/libretime_api/storage/views/file.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/api/libretime_api/storage/views/file.py b/api/libretime_api/storage/views/file.py index 70d85e4fc..2341b5c2a 100644 --- a/api/libretime_api/storage/views/file.py +++ b/api/libretime_api/storage/views/file.py @@ -2,6 +2,7 @@ import os from django.http import HttpResponse from django.shortcuts import get_object_or_404 +from django.utils.encoding import iri_to_uri from rest_framework import viewsets from rest_framework.decorators import action from rest_framework.serializers import IntegerField @@ -23,5 +24,9 @@ class FileViewSet(viewsets.ModelViewSet): response = HttpResponse() 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