Add support for X-Accel-Redirect

This commit is contained in:
2026-01-05 00:04:44 +01:00
parent ede8741473
commit 9a469378df
2 changed files with 16 additions and 6 deletions

View File

@@ -206,6 +206,9 @@ ROLLING_STOCK_TYPES = [
FEATURED_ITEMS_MAX = 6
# If True, use X-Accel-Redirect (Nginx)
USE_X_ACCEL_REDIRECT = False
try:
from ram.local_settings import *
except ImportError:

View File

@@ -9,6 +9,7 @@ from django.apps import apps
from django.conf import settings
from django.http import (
Http404,
HttpResponse,
HttpResponseBadRequest,
HttpResponseForbidden,
FileResponse,
@@ -89,17 +90,23 @@ class DownloadFile(View):
if doc.private and not request.user.is_staff:
break
file_path = doc.file.path
if not os.path.exists(file_path):
file = doc.file
if not os.path.exists(file.path):
break
response = FileResponse(
open(file_path, "rb"), as_attachment=True
)
if getattr(settings, "USE_X_ACCEL_REDIRECT", False):
response = HttpResponse()
response["Content-Type"] = ""
response["X-Accel-Redirect"] = file.url
else:
response = FileResponse(
open(file.path, "rb"), as_attachment=True
)
response["Content-Disposition"] = (
'{}; filename="{}"'.format(
disposition,
smart_str(os.path.basename(file_path))
smart_str(os.path.basename(file.path))
)
)
return response