mirror of
https://github.com/daniviga/django-ram.git
synced 2026-02-04 01:50:39 +01:00
Add support for X-Accel-Redirect
This commit is contained in:
@@ -206,6 +206,9 @@ ROLLING_STOCK_TYPES = [
|
|||||||
|
|
||||||
FEATURED_ITEMS_MAX = 6
|
FEATURED_ITEMS_MAX = 6
|
||||||
|
|
||||||
|
# If True, use X-Accel-Redirect (Nginx)
|
||||||
|
USE_X_ACCEL_REDIRECT = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from ram.local_settings import *
|
from ram.local_settings import *
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from django.apps import apps
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import (
|
from django.http import (
|
||||||
Http404,
|
Http404,
|
||||||
|
HttpResponse,
|
||||||
HttpResponseBadRequest,
|
HttpResponseBadRequest,
|
||||||
HttpResponseForbidden,
|
HttpResponseForbidden,
|
||||||
FileResponse,
|
FileResponse,
|
||||||
@@ -89,17 +90,23 @@ class DownloadFile(View):
|
|||||||
if doc.private and not request.user.is_staff:
|
if doc.private and not request.user.is_staff:
|
||||||
break
|
break
|
||||||
|
|
||||||
file_path = doc.file.path
|
file = doc.file
|
||||||
if not os.path.exists(file_path):
|
if not os.path.exists(file.path):
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if getattr(settings, "USE_X_ACCEL_REDIRECT", False):
|
||||||
|
response = HttpResponse()
|
||||||
|
response["Content-Type"] = ""
|
||||||
|
response["X-Accel-Redirect"] = file.url
|
||||||
|
else:
|
||||||
response = FileResponse(
|
response = FileResponse(
|
||||||
open(file_path, "rb"), as_attachment=True
|
open(file.path, "rb"), as_attachment=True
|
||||||
)
|
)
|
||||||
|
|
||||||
response["Content-Disposition"] = (
|
response["Content-Disposition"] = (
|
||||||
'{}; filename="{}"'.format(
|
'{}; filename="{}"'.format(
|
||||||
disposition,
|
disposition,
|
||||||
smart_str(os.path.basename(file_path))
|
smart_str(os.path.basename(file.path))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
|||||||
Reference in New Issue
Block a user