|
164 | 164 | from dojo.user.utils import get_configuration_permissions_codenames
|
165 | 165 | from dojo.utils import (
|
166 | 166 | async_delete,
|
| 167 | + generate_file_response, |
167 | 168 | get_setting,
|
168 | 169 | get_system_setting,
|
169 | 170 | )
|
@@ -646,21 +647,8 @@ def download_file(self, request, file_id, pk=None):
|
646 | 647 | {"error": "File ID not associated with Engagement"},
|
647 | 648 | status=status.HTTP_404_NOT_FOUND,
|
648 | 649 | )
|
649 |
| - # Get the path of the file in media root |
650 |
| - file_path = f"{settings.MEDIA_ROOT}/{file_object.file.url.lstrip(settings.MEDIA_URL)}" |
651 |
| - file_handle = open(file_path, "rb") |
652 | 650 | # send file
|
653 |
| - response = FileResponse( |
654 |
| - file_handle, |
655 |
| - content_type=f"{mimetypes.guess_type(file_path)}", |
656 |
| - status=status.HTTP_200_OK, |
657 |
| - ) |
658 |
| - response["Content-Length"] = file_object.file.size |
659 |
| - response[ |
660 |
| - "Content-Disposition" |
661 |
| - ] = f'attachment; filename="{file_object.file.name}"' |
662 |
| - |
663 |
| - return response |
| 651 | + return generate_file_response(file_object) |
664 | 652 |
|
665 | 653 |
|
666 | 654 | class RiskAcceptanceViewSet(
|
@@ -1156,21 +1144,8 @@ def download_file(self, request, file_id, pk=None):
|
1156 | 1144 | {"error": "File ID not associated with Finding"},
|
1157 | 1145 | status=status.HTTP_404_NOT_FOUND,
|
1158 | 1146 | )
|
1159 |
| - # Get the path of the file in media root |
1160 |
| - file_path = f"{settings.MEDIA_ROOT}/{file_object.file.url.lstrip(settings.MEDIA_URL)}" |
1161 |
| - file_handle = open(file_path, "rb") |
1162 | 1147 | # send file
|
1163 |
| - response = FileResponse( |
1164 |
| - file_handle, |
1165 |
| - content_type=f"{mimetypes.guess_type(file_path)}", |
1166 |
| - status=status.HTTP_200_OK, |
1167 |
| - ) |
1168 |
| - response["Content-Length"] = file_object.file.size |
1169 |
| - response[ |
1170 |
| - "Content-Disposition" |
1171 |
| - ] = f'attachment; filename="{file_object.file.name}"' |
1172 |
| - |
1173 |
| - return response |
| 1148 | + return generate_file_response(file_object) |
1174 | 1149 |
|
1175 | 1150 | @extend_schema(
|
1176 | 1151 | request=serializers.FindingNoteSerializer,
|
@@ -2320,21 +2295,8 @@ def download_file(self, request, file_id, pk=None):
|
2320 | 2295 | {"error": "File ID not associated with Test"},
|
2321 | 2296 | status=status.HTTP_404_NOT_FOUND,
|
2322 | 2297 | )
|
2323 |
| - # Get the path of the file in media root |
2324 |
| - file_path = f"{settings.MEDIA_ROOT}/{file_object.file.url.lstrip(settings.MEDIA_URL)}" |
2325 |
| - file_handle = open(file_path, "rb") |
2326 | 2298 | # send file
|
2327 |
| - response = FileResponse( |
2328 |
| - file_handle, |
2329 |
| - content_type=f"{mimetypes.guess_type(file_path)}", |
2330 |
| - status=status.HTTP_200_OK, |
2331 |
| - ) |
2332 |
| - response["Content-Length"] = file_object.file.size |
2333 |
| - response[ |
2334 |
| - "Content-Disposition" |
2335 |
| - ] = f'attachment; filename="{file_object.file.name}"' |
2336 |
| - |
2337 |
| - return response |
| 2299 | + return generate_file_response(file_object) |
2338 | 2300 |
|
2339 | 2301 |
|
2340 | 2302 | # Authorization: authenticated, configuration
|
|
0 commit comments