diff --git a/ram/ram/settings.py b/ram/ram/settings.py index 8891744..a4d5da0 100644 --- a/ram/ram/settings.py +++ b/ram/ram/settings.py @@ -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: diff --git a/ram/ram/views.py b/ram/ram/views.py index 045a2a6..d8ba500 100644 --- a/ram/ram/views.py +++ b/ram/ram/views.py @@ -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