Skip to content

Commit a201813

Browse files
committed
generate zipped json report
1 parent 22992be commit a201813

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

conf/default/reporting.conf.default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ drive_credentials_location = data/google_creds.json
4949
enabled = yes
5050
indent = 4
5151
encoding = latin-1
52+
store_compressed = no
5253

5354
# Community
5455
[reporthtml]

modules/reporting/jsondump.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66

7+
from lib.cuckoo.common.utils import create_zip
78
from lib.cuckoo.common.abstracts import Report
89
from lib.cuckoo.common.exceptions import CuckooReportError
910
from lib.cuckoo.common.path_utils import path_write_file
@@ -17,7 +18,6 @@
1718

1819
HAVE_ORJSON = False
1920

20-
2121
class JsonDump(Report):
2222
"""Saves analysis results in JSON format."""
2323

@@ -48,5 +48,13 @@ def run(self, results):
4848
else:
4949
with open(path, "w") as report:
5050
json.dump(results, report, sort_keys=False, indent=int(indent), ensure_ascii=False)
51+
52+
# useful if you frequently fetch zipped reports to not compress in memory all the time
53+
if self.options.get("store_compressed") and os.path.exists(path):
54+
zip_path = path + ".zip"
55+
zipped_io = create_zip(path)
56+
with open(zip_path, "wb") as f:
57+
f.write(zipped_io.getvalue())
58+
5159
except (UnicodeError, TypeError, IOError) as e:
5260
raise CuckooReportError(f"Failed to generate JSON report: {e}")

web/apiv2/views.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,14 +1256,22 @@ def tasks_report(request, task_id, report_format="json", make_zip=False):
12561256
fname = "%s_report.%s" % (task_id, ext)
12571257

12581258
if make_zip:
1259-
mem_zip = create_zip(files=report_path)
1260-
if mem_zip is False:
1261-
resp = {"error": True, "error_value": "Can't create zip archive for report file"}
1262-
return Response(resp)
1263-
1264-
resp = StreamingHttpResponse(mem_zip, content_type="application/zip")
1265-
resp["Content-Length"] = len(mem_zip.getvalue())
1266-
resp["Content-Disposition"] = f"attachment; filename={report_format}.zip"
1259+
if os.path.exists(report_path + ".zip"):
1260+
report_path += ".zip"
1261+
resp = StreamingHttpResponse(
1262+
FileWrapper(open(report_path, "rb"), 8096), content_type="application/zip"
1263+
)
1264+
resp["Content-Length"] = os.path.getsize(report_path)
1265+
resp["Content-Disposition"] = "attachment; filename=" + fname
1266+
else:
1267+
mem_zip = create_zip(files=report_path)
1268+
if mem_zip is False:
1269+
resp = {"error": True, "error_value": "Can't create zip archive for report file"}
1270+
return Response(resp)
1271+
1272+
resp = StreamingHttpResponse(mem_zip, content_type="application/zip")
1273+
resp["Content-Length"] = len(mem_zip.getvalue())
1274+
resp["Content-Disposition"] = f"attachment; filename={report_format}.zip"
12671275
else:
12681276
resp = StreamingHttpResponse(
12691277
FileWrapper(open(report_path, "rb"), 8096), content_type=content or "application/octet-stream;"

0 commit comments

Comments
 (0)