Skip to content

Commit

Permalink
chore(api): use FileResponse for download view
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Jul 1, 2024
1 parent ba1e215 commit 2b1b29b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions weblate/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.db.models import Model, Q
from django.http import Http404, HttpResponse
from django.http import FileResponse, Http404
from django.shortcuts import get_object_or_404
from django.utils.html import format_html
from django.utils.translation import gettext
Expand Down Expand Up @@ -194,8 +194,10 @@ def download_file(self, filename, content_type, component=None):
filename = f"{basename}.zip"
else:
try:
with open(filename, "rb") as handle:
response = HttpResponse(handle.read(), content_type=content_type)
response = FileResponse(
open(filename, "rb"), # noqa: SIM115
content_type=content_type,
)
except FileNotFoundError as error:
raise Http404("File not found") from error
filename = os.path.basename(filename)
Expand Down

0 comments on commit 2b1b29b

Please sign in to comment.