1515from packageurl .contrib import url2purl
1616from packageurl .contrib .django .filters import PackageURLFilter
1717from rest_framework import serializers
18+ from rest_framework import status
1819from rest_framework .decorators import action
1920from rest_framework .fields import ListField
2021from rest_framework .response import Response
2930from component_catalog .models import ComponentKeyword
3031from component_catalog .models import Package
3132from component_catalog .models import Subcomponent
33+ from component_catalog .views import scan_data_as_zip_response
3234from dejacode_toolkit .download import DataCollectionException
3335from dejacode_toolkit .download import collect_package_data
3436from dejacode_toolkit .scancodeio import ScanCodeIO
@@ -919,18 +921,9 @@ def about(self, request, uuid):
919921 package = self .get_object ()
920922 return Response ({"about_data" : package .as_about_yaml ()})
921923
922- # TODO: Remove duplication with send_scan_data_as_file_view
923924 @action (detail = True )
924925 def download_scan_data (self , request , uuid ):
925- import io
926- import json
927- import zipfile
928-
929- from django .http import FileResponse
930-
931- from rest_framework import status
932- from rest_framework .response import Response
933-
926+ """Download package scan data: results and sumary, as a zip file."""
934927 package = self .get_object ()
935928 dataspace = request .user .dataspace
936929
@@ -948,25 +941,9 @@ def download_scan_data(self, request, uuid):
948941 return Response (message , status = status .HTTP_400_BAD_REQUEST )
949942
950943 project_uuid = scan_infos .get ("uuid" )
951-
952- scan_results_url = scancodeio .get_scan_action_url (project_uuid , "results" )
953- scan_results = scancodeio .fetch_scan_data (scan_results_url )
954- scan_summary_url = scancodeio .get_scan_action_url (project_uuid , "summary" )
955- scan_summary = scancodeio .fetch_scan_data (scan_summary_url )
956-
957944 filename = package .filename or package .package_url_filename
958- in_memory_zip = io .BytesIO ()
959- with zipfile .ZipFile (in_memory_zip , "a" , zipfile .ZIP_DEFLATED , False ) as zipf :
960- zipf .writestr (f"{ filename } _scan.json" , json .dumps (scan_results , indent = 2 ))
961- zipf .writestr (f"{ filename } _summary.json" , json .dumps (scan_summary , indent = 2 ))
962- in_memory_zip .seek (0 )
963-
964- return FileResponse (
965- in_memory_zip ,
966- filename = filename ,
967- as_attachment = True ,
968- content_type = "application/zip" ,
969- )
945+ filename = f"{ filename } _scan.zip"
946+ return scan_data_as_zip_response (scancodeio , project_uuid , filename )
970947
971948 @action (detail = False , methods = ["post" ], name = "Package Add" )
972949 def add (self , request ):
0 commit comments