mirror of
https://github.com/daniviga/django-ram.git
synced 2026-02-03 17:40:39 +01:00
Compare commits
2 Commits
file-acces
...
v0.19.7
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a469378df | |||
| ede8741473 |
@@ -1,4 +1,4 @@
|
||||
from ram.utils import git_suffix
|
||||
|
||||
__version__ = "0.19.6"
|
||||
__version__ = "0.19.7"
|
||||
__version__ += git_suffix(__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:
|
||||
|
||||
@@ -28,11 +28,15 @@ handler404 = Render404.as_view()
|
||||
|
||||
urlpatterns = [
|
||||
path("", lambda r: redirect("portal/")),
|
||||
path("admin/", admin.site.urls),
|
||||
path("tinymce/", include("tinymce.urls")),
|
||||
path("tinymce/upload_image", UploadImage.as_view(), name="upload_image"),
|
||||
path(
|
||||
"media/files/<path:filename>",
|
||||
DownloadFile.as_view(),
|
||||
name="download_file",
|
||||
),
|
||||
path("portal/", include("portal.urls")),
|
||||
path("admin/", admin.site.urls),
|
||||
path("media/files/<path:filename>", DownloadFile.as_view(), name="download_file"),
|
||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
||||
# Enable the "/dcc" routing only if the "driver" app is active
|
||||
@@ -56,6 +60,7 @@ if settings.DEBUG:
|
||||
if settings.REST_ENABLED:
|
||||
from django.views.generic import TemplateView
|
||||
from rest_framework.schemas import get_schema_view
|
||||
|
||||
urlpatterns += [
|
||||
path(
|
||||
"swagger/",
|
||||
|
||||
@@ -9,6 +9,7 @@ from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.http import (
|
||||
Http404,
|
||||
HttpResponse,
|
||||
HttpResponseBadRequest,
|
||||
HttpResponseForbidden,
|
||||
FileResponse,
|
||||
@@ -76,7 +77,7 @@ class UploadImage(View):
|
||||
|
||||
|
||||
class DownloadFile(View):
|
||||
def get(self, request, filename):
|
||||
def get(self, request, filename, disposition="inline"):
|
||||
# Clean up the filename to prevent directory traversal attacks
|
||||
filename = os.path.basename(filename)
|
||||
|
||||
@@ -87,15 +88,26 @@ class DownloadFile(View):
|
||||
try:
|
||||
doc = model.objects.get(file__endswith=filename)
|
||||
if doc.private and not request.user.is_staff:
|
||||
raise Http404("File not found")
|
||||
break
|
||||
|
||||
file_path = doc.file.path
|
||||
if not os.path.exists(file_path):
|
||||
raise Http404("File not found")
|
||||
file = doc.file
|
||||
if not os.path.exists(file.path):
|
||||
break
|
||||
|
||||
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 = FileResponse(open(file_path, "rb"), as_attachment=True)
|
||||
response["Content-Disposition"] = (
|
||||
f'attachment; filename="{smart_str(os.path.basename(file_path))}"'
|
||||
'{}; filename="{}"'.format(
|
||||
disposition,
|
||||
smart_str(os.path.basename(file.path))
|
||||
)
|
||||
)
|
||||
return response
|
||||
except model.DoesNotExist:
|
||||
|
||||
Reference in New Issue
Block a user