diff --git a/backend/modules/fleet/tests/test_work_order_images_endpoint.py b/backend/modules/fleet/tests/test_work_order_images_endpoint.py index c6fe9ba..472224a 100644 --- a/backend/modules/fleet/tests/test_work_order_images_endpoint.py +++ b/backend/modules/fleet/tests/test_work_order_images_endpoint.py @@ -351,7 +351,7 @@ class WorkOrderImagesEndpointTests(TestCase): download_response = self.client.get(f"/api/fleet/reports/generated-archives/{generated.pk}/download/") self.assertEqual(download_response.status_code, 200) self.assertEqual(download_response['Content-Type'], 'application/zip') - archive_bytes = b''.join(download_response.streaming_content) + archive_bytes = download_response.content if hasattr(download_response, 'content') else b''.join(download_response.streaming_content) archive = zipfile.ZipFile(BytesIO(archive_bytes)) self.assertIn('MT150726.SN-Test_servisni_zadatak.docx', archive.namelist()) @@ -392,7 +392,7 @@ class WorkOrderImagesEndpointTests(TestCase): download_response = self.client.get(f"/api/fleet/reports/generated-archives/{generated.pk}/download/") self.assertEqual(download_response.status_code, 200) self.assertEqual(download_response['Content-Type'], 'application/zip') - archive_bytes = b''.join(download_response.streaming_content) + archive_bytes = download_response.content if hasattr(download_response, 'content') else b''.join(download_response.streaming_content) archive = zipfile.ZipFile(BytesIO(archive_bytes)) names = archive.namelist() diff --git a/backend/modules/fleet/views.py b/backend/modules/fleet/views.py index b7b83b7..170b7b6 100644 --- a/backend/modules/fleet/views.py +++ b/backend/modules/fleet/views.py @@ -3063,15 +3063,22 @@ def generated_archive_download(request, archive_id): raise DRFValidationError({'detail': 'ZIP arhiva nije dostupna ili je istekla.'}) generated_archive.file.open('rb') + try: + archive_bytes = generated_archive.file.read() + finally: + generated_archive.file.close() + if not archive_bytes: + raise DRFValidationError({'detail': 'ZIP arhiva je prazna ili nedostupna.'}) filename = generated_archive.filename or _generated_archive_filename_for_user( request.user, year=generated_archive.year, month=generated_archive.month, archive_type=generated_archive.archive_type, ) - response = FileResponse(generated_archive.file, content_type='application/zip') + response = HttpResponse(archive_bytes, content_type='application/zip') response['Content-Disposition'] = f'attachment; filename="{filename}"' response['Cache-Control'] = 'private, max-age=3600' + response['Content-Length'] = str(len(archive_bytes)) return response