Skip to content

Commit 24084e2

Browse files
committed
fix: serve JSON and text downloads inline so they open in the browser
download_project_file() forced every input and output file download with as_attachment=True, so JSON results always downloaded as a separate file instead of opening in the browser. Serve .json and .txt files inline with their content type; other formats keep forcing a download. HTML is deliberately not served inline since generated attribution documents may embed third-party package data. Closes #2210 Signed-off-by: Manoj Gowda <manojgowdabs18@gmail.com>
1 parent 4186863 commit 24084e2

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

scanpipe/tests/test_views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,18 @@ def test_scanpipe_views_project_details_download_output_view(self):
386386
response.headers["Content-Disposition"],
387387
)
388388

389+
def test_scanpipe_views_project_details_download_output_view_json_inline(self):
390+
# https://github.com/aboutcode-org/scancode.io/issues/2210
391+
json_file = self.project1.output_path / "results.json"
392+
json_file.write_text('{"headers": []}')
393+
url = reverse("project_download_output", args=[self.project1.slug, "results.json"])
394+
response = self.client.get(url)
395+
self.assertEqual("application/json", response.headers["Content-Type"])
396+
self.assertEqual(
397+
'inline; filename="results.json"',
398+
response.headers["Content-Disposition"],
399+
)
400+
389401
def test_scanpipe_views_project_details_delete_input_view(self):
390402
random_uuid = str(uuid.uuid4())
391403
url = reverse("project_delete_input", args=[self.project1.slug, random_uuid])

scanpipe/views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,11 @@ def download_project_file(request, slug, filename, path_type):
15321532
if not file_path.exists():
15331533
raise Http404(f"{file_path} not found")
15341534

1535-
return FileResponse(file_path.open("rb"), as_attachment=True)
1535+
# JSON and plain text render safely in browsers: serve those inline so
1536+
# that e.g. JSON results open directly in the browser instead of always
1537+
# forcing a download.
1538+
as_attachment = file_path.suffix not in (".json", ".txt")
1539+
return FileResponse(file_path.open("rb"), as_attachment=as_attachment)
15361540

15371541

15381542
@conditional_login_required

0 commit comments

Comments
 (0)