Skip to content

Add Content-Disposition header to file downloads #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions chris_backend/core/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import logging
import uuid
import jwt

from django.contrib.auth.models import User
Expand Down Expand Up @@ -51,8 +52,8 @@ def perform_create(self, serializer):
"""
user = self.request.user
dt = timezone.now() + timezone.timedelta(minutes=10)
token = jwt.encode({'user': user.username, 'exp': dt}, settings.SECRET_KEY,
algorithm='HS256')
token = jwt.encode({'user': user.username, 'nonce': str(uuid.uuid4()), 'exp': dt},
settings.SECRET_KEY, algorithm='HS256')
serializer.save(token=token, owner=user)

def list(self, request, *args, **kwargs):
Expand Down
11 changes: 9 additions & 2 deletions chris_backend/filebrowser/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import logging
from pathlib import Path

from django.http import Http404, FileResponse
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -492,7 +493,10 @@ def get(self, request, *args, **kwargs):
Overriden to be able to make a GET request to an actual file resource.
"""
chris_file = self.get_object()
return FileResponse(chris_file.fname)
f = chris_file.fname
resp = FileResponse(f)
resp['Content-Disposition'] = f'attachment; filename="{Path(f.name).name}"'
return resp


class FileBrowserFileGroupPermissionList(generics.ListCreateAPIView):
Expand Down Expand Up @@ -820,7 +824,10 @@ def get(self, request, *args, **kwargs):
Overriden to be able to make a GET request to an actual file resource.
"""
chris_link_file = self.get_object()
return FileResponse(chris_link_file.fname)
f = chris_link_file.fname
resp = FileResponse(f)
resp['Content-Disposition'] = f'attachment; filename="{Path(f.name).name}"'
return resp


class FileBrowserLinkFileGroupPermissionList(generics.ListCreateAPIView):
Expand Down
7 changes: 6 additions & 1 deletion chris_backend/pacsfiles/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from pathlib import Path

from django.http import FileResponse
from django.contrib.auth.models import User, Group
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -463,4 +465,7 @@ def get(self, request, *args, **kwargs):
Overriden to be able to make a GET request to an actual file resource.
"""
pacs_file = self.get_object()
return FileResponse(pacs_file.fname)
f = pacs_file.fname
resp = FileResponse(f)
resp['Content-Disposition'] = f'attachment; filename="{Path(f.name).name}"'
return resp
6 changes: 5 additions & 1 deletion chris_backend/pipelines/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import logging
from pathlib import Path

from django.http import FileResponse
from django.contrib.auth.models import User
Expand Down Expand Up @@ -202,7 +203,10 @@ def get(self, request, *args, **kwargs):
Overriden to be able to make a GET request to an actual file resource.
"""
source_file = self.get_object()
return FileResponse(source_file.fname)
f = source_file.fname
resp = FileResponse(f)
resp['Content-Disposition'] = f'attachment; filename="{Path(f.name).name}"'
return resp


class PipelinePluginList(generics.ListAPIView):
Expand Down
7 changes: 6 additions & 1 deletion chris_backend/userfiles/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from pathlib import Path

from django.http import FileResponse

from rest_framework import generics, permissions
Expand Down Expand Up @@ -127,4 +129,7 @@ def get(self, request, *args, **kwargs):
Overriden to be able to make a GET request to an actual file resource.
"""
user_file = self.get_object()
return FileResponse(user_file.fname)
f = user_file.fname
resp = FileResponse(f)
resp['Content-Disposition'] = f'attachment; filename="{Path(f.name).name}"'
return resp
Loading