Skip to content

Commit 0da19a7

Browse files
committed
[api] Improve filename handling in Content-Disposition header for /download API
1 parent 02c2b2d commit 0da19a7

File tree

1 file changed

+9
-1
lines changed
  • apps/filebrowser/src/filebrowser

1 file changed

+9
-1
lines changed

apps/filebrowser/src/filebrowser/api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import mimetypes
2323
import posixpath
2424
from io import BytesIO as string_io
25+
from urllib.parse import quote
2526

2627
from django.core.files.uploadhandler import StopUpload
2728
from django.core.paginator import EmptyPage, Paginator
@@ -224,7 +225,14 @@ def download(request):
224225
request.GET.get('disposition') if request.GET.get('disposition') == 'inline' and _can_inline_display(path) else 'attachment'
225226
)
226227

227-
response['Content-Disposition'] = f'{content_disposition}; filename="{stats["name"]}"'
228+
# Extract filename for HDFS and OFS for now because the path stats object has a bug in fetching name field
229+
# TODO: Fix this super old bug when refactoring the underlying HDFS filesystem code
230+
filename = os.path.basename(path) if request.fs._get_scheme(path).lower() in ('hdfs', 'ofs') else stats['name']
231+
232+
# Set the filename in the Content-Disposition header with proper encoding for special characters
233+
encoded_filename = quote(filename)
234+
response['Content-Disposition'] = f"{content_disposition}; filename*=UTF-8\'\'{encoded_filename}"
235+
228236
response["Last-Modified"] = http_date(stats['mtime'])
229237
response["Content-Length"] = stats['size']
230238

0 commit comments

Comments
 (0)