Skip to content

Commit 3398e7c

Browse files
committed
Refine the scan_data_as_zip implementation #272
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 4eaf2fd commit 3398e7c

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

component_catalog/api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from django.db import transaction
1212
from django.forms.widgets import HiddenInput
13+
from django.http import FileResponse
1314

1415
import django_filters
1516
from packageurl.contrib import url2purl
@@ -30,7 +31,6 @@
3031
from component_catalog.models import ComponentKeyword
3132
from component_catalog.models import Package
3233
from component_catalog.models import Subcomponent
33-
from component_catalog.views import scan_data_as_zip_response
3434
from dejacode_toolkit.download import DataCollectionException
3535
from dejacode_toolkit.download import collect_package_data
3636
from dejacode_toolkit.scancodeio import ScanCodeIO
@@ -942,8 +942,14 @@ def download_scan_data(self, request, uuid):
942942

943943
project_uuid = scan_infos.get("uuid")
944944
filename = package.filename or package.package_url_filename
945-
filename = f"{filename}_scan.zip"
946-
return scan_data_as_zip_response(scancodeio, project_uuid, filename)
945+
946+
scan_data_as_zip = scancodeio.scan_data_as_zip(project_uuid, filename)
947+
return FileResponse(
948+
scan_data_as_zip,
949+
filename=f"{filename}_scan.zip",
950+
as_attachment=True,
951+
content_type="application/zip",
952+
)
947953

948954
@action(detail=False, methods=["post"], name="Package Add")
949955
def add(self, request):

component_catalog/views.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
# See https://aboutcode.org for more information about AboutCode FOSS projects.
77
#
88

9-
import io
109
import json
11-
import zipfile
1210
from collections import Counter
1311
from operator import itemgetter
1412
from urllib.parse import quote_plus
@@ -1635,27 +1633,6 @@ def component_create_ajax_view(request):
16351633
return HttpResponse(rendered_form)
16361634

16371635

1638-
def scan_data_as_zip_response(scancodeio, project_uuid, filename):
1639-
"""Return a FileResponse of a package scan data (results, summary) as a zip file."""
1640-
scan_results_url = scancodeio.get_scan_action_url(project_uuid, "results")
1641-
scan_results = scancodeio.fetch_scan_data(scan_results_url)
1642-
scan_summary_url = scancodeio.get_scan_action_url(project_uuid, "summary")
1643-
scan_summary = scancodeio.fetch_scan_data(scan_summary_url)
1644-
1645-
in_memory_zip = io.BytesIO()
1646-
with zipfile.ZipFile(in_memory_zip, "a", zipfile.ZIP_DEFLATED, False) as zipf:
1647-
zipf.writestr(f"{filename}_scan.json", json.dumps(scan_results, indent=2))
1648-
zipf.writestr(f"{filename}_summary.json", json.dumps(scan_summary, indent=2))
1649-
in_memory_zip.seek(0)
1650-
1651-
return FileResponse(
1652-
in_memory_zip,
1653-
filename=filename,
1654-
as_attachment=True,
1655-
content_type="application/zip",
1656-
)
1657-
1658-
16591636
@login_required
16601637
def send_scan_data_as_file_view(request, project_uuid, filename):
16611638
dataspace = request.user.dataspace
@@ -1666,8 +1643,13 @@ def send_scan_data_as_file_view(request, project_uuid, filename):
16661643
if not scancodeio.is_available():
16671644
raise Http404("The ScanCode.io service is not available")
16681645

1669-
filename = f"{filename}_scan.zip"
1670-
return scan_data_as_zip_response(scancodeio, project_uuid, filename)
1646+
scan_data_as_zip = scancodeio.scan_data_as_zip(project_uuid, filename)
1647+
return FileResponse(
1648+
scan_data_as_zip,
1649+
filename=f"{filename}_scan.zip",
1650+
as_attachment=True,
1651+
content_type="application/zip",
1652+
)
16711653

16721654

16731655
@login_required

dejacode_toolkit/scancodeio.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# See https://aboutcode.org for more information about AboutCode FOSS projects.
77
#
88

9+
import io
910
import json
11+
import zipfile
1012
from hashlib import md5
1113
from urllib.parse import quote_plus
1214

@@ -262,6 +264,21 @@ def fetch_project_dependencies(self, project_uuid):
262264
api_url = self.get_scan_action_url(project_uuid, "dependencies")
263265
return self.fetch_results(api_url)
264266

267+
def scan_data_as_zip(self, project_uuid, filename):
268+
"""Return a FileResponse of a package scan data as a zip file."""
269+
scan_results_url = self.get_scan_action_url(project_uuid, "results")
270+
scan_results = self.fetch_scan_data(scan_results_url)
271+
scan_summary_url = self.get_scan_action_url(project_uuid, "summary")
272+
scan_summary = self.fetch_scan_data(scan_summary_url)
273+
274+
in_memory_zip = io.BytesIO()
275+
with zipfile.ZipFile(in_memory_zip, "a", zipfile.ZIP_DEFLATED, False) as zipf:
276+
zipf.writestr(f"{filename}_scan.json", json.dumps(scan_results, indent=2))
277+
zipf.writestr(f"{filename}_summary.json", json.dumps(scan_summary, indent=2))
278+
in_memory_zip.seek(0)
279+
280+
return in_memory_zip
281+
265282
# (label, scan_field, model_field, input_type)
266283
SCAN_SUMMARY_FIELDS = [
267284
(

0 commit comments

Comments
 (0)