Skip to content

Commit

Permalink
add fallback media serving
Browse files Browse the repository at this point in the history
  • Loading branch information
jeriox committed Sep 22, 2024
1 parent 98fc2f1 commit f1ee9bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ephios/plugins/files/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import urlsplit

from django.conf import settings
from django.http import HttpResponse
from django.http import FileResponse, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.views import View
from guardian.mixins import LoginRequiredMixin
Expand All @@ -15,9 +15,12 @@ def get(self, request, *args, **kwargs):
if (loc := urlsplit(settings.GET_USERCONTENT_URL()).netloc) and request.get_host() != loc:
return redirect(settings.GET_USERCONTENT_URL() + request.path)
document = get_object_or_404(Document, id=kwargs["pk"])
response = HttpResponse()
if settings.FALLBACK_MEDIA_SERVING:
response = FileResponse(document.file)
else:
response = HttpResponse()
response["X-Accel-Redirect"] = document.file.url
response["Content-Disposition"] = (
"attachment; filename=" + os.path.split(document.file.name)[1]
)
response["X-Accel-Redirect"] = document.file.url
return response
1 change: 1 addition & 0 deletions ephios/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@

STATIC_URL = env.str("STATIC_URL", default="/static/")
MEDIA_URL = env.str("MEDIA_URL", default="/usercontent/")
FALLBACK_MEDIA_SERVING = env.bool("FALLBACK_MEDIA_SERVING", default=False)

STATICFILES_DIRS = (os.path.join(BASE_DIR, "ephios/static"),)
STATICFILES_FINDERS = (
Expand Down

0 comments on commit f1ee9bd

Please sign in to comment.