This commit is contained in:
Julien Constant 2023-04-05 19:40:55 +02:00
parent dfe892d418
commit f52ec66946
2 changed files with 22 additions and 3 deletions

View File

@ -26,7 +26,6 @@ from django.utils.translation import gettext_lazy as _
from django.http import HttpResponse from django.http import HttpResponse
from wsgiref.util import FileWrapper from wsgiref.util import FileWrapper
from django.urls import reverse from django.urls import reverse
from django.core.exceptions import PermissionDenied
from django import forms from django import forms
import os import os
@ -34,7 +33,14 @@ import os
from ajax_select import make_ajax_field from ajax_select import make_ajax_field
from core.models import SithFile, RealGroup, Notification from core.models import SithFile, RealGroup, Notification
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, can_view, not_found from core.views import (
CanViewMixin,
CanEditMixin,
CanEditPropMixin,
can_view,
forbidden,
not_found,
)
from counter.models import Counter from counter.models import Counter
@ -55,9 +61,14 @@ def send_file(request, file_id, file_class=SithFile, file_attr="file"):
).exists() ).exists()
) )
): ):
raise PermissionDenied return forbidden(request, _("You are not allowed to view this file"))
name = f.__getattribute__(file_attr).name name = f.__getattribute__(file_attr).name
filepath = os.path.join(settings.MEDIA_ROOT, name) filepath = os.path.join(settings.MEDIA_ROOT, name)
# check if file exists on disk
if not os.path.exists(filepath.encode("utf-8")):
return not_found(request, _("File not found"))
with open(filepath.encode("utf-8"), "rb") as filename: with open(filepath.encode("utf-8"), "rb") as filename:
wrapper = FileWrapper(filename) wrapper = FileWrapper(filename)
response = HttpResponse(wrapper, content_type=f.mime_type) response = HttpResponse(wrapper, content_type=f.mime_type)

View File

@ -2519,6 +2519,14 @@ msgstr "Laverie"
msgid "Files" msgid "Files"
msgstr "Fichiers" msgstr "Fichiers"
#: core/views/files.py:70
msgid "File not found"
msgstr "Fichier introuvable"
#: core/views/files.py:64
msgid "You are not allowed to view this file"
msgstr "Vous n'êtes pas autorisé à voir ce fichier"
#: core/templates/core/base.jinja:202 core/templates/core/user_tools.jinja:109 #: core/templates/core/base.jinja:202 core/templates/core/user_tools.jinja:109
msgid "Pedagogy" msgid "Pedagogy"
msgstr "Pédagogie" msgstr "Pédagogie"