From cefcc3430cbbe604a2857f4da750cd0038d0c1ee Mon Sep 17 00:00:00 2001 From: Falconieri Date: Tue, 26 Feb 2019 11:13:24 +0100 Subject: [PATCH] add : [exportpdf] Picture management, manual --- pymisp/tools/reportlab_generator.py | 43 ++++++++++++++++++++++++----- tests/test_reportlab.py | 18 +++++++++++- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/pymisp/tools/reportlab_generator.py b/pymisp/tools/reportlab_generator.py index b84f3ac..b1d4e06 100644 --- a/pymisp/tools/reportlab_generator.py +++ b/pymisp/tools/reportlab_generator.py @@ -12,6 +12,7 @@ import sys if sys.version_info.major >= 3: from html import escape + # import PIL else: print( "ExportPDF running with Python < 3 : stability and output not guaranteed. Please run exportPDF with at least Python3") @@ -24,8 +25,9 @@ try: from reportlab.pdfbase.pdfmetrics import stringWidth from reportlab.pdfbase.pdfdoc import PDFDictionary, PDFInfo from reportlab.lib import colors + from reportlab.lib.utils import ImageReader - from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer, Table, TableStyle, Flowable + from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer, Table, TableStyle, Flowable, Image from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.units import mm @@ -454,10 +456,12 @@ def create_flowable_table_from_one_attribute(misp_attribute): ["Type", 'type', "None"], ["Value", 'value', "None"]] + IMAGE_TYPE = "attachment" + # Handle the special case of links STANDARD_TYPE = True - if hasattr(misp_attribute, 'type') and ( - getattr(misp_attribute, 'type') == LINK_TYPE or getattr(misp_attribute, 'type') == URL_TYPE): + if hasattr(misp_attribute, 'type') and (getattr(misp_attribute, 'type') in [LINK_TYPE, URL_TYPE, IMAGE_TYPE]): + # getattr(misp_attribute, 'type') == LINK_TYPE or getattr(misp_attribute, 'type') == URL_TYPE): # Special case for links STANDARD_TYPE = False @@ -482,10 +486,35 @@ def create_flowable_table_from_one_attribute(misp_attribute): misp_attribute, item[1]) + "", col2_style, False)]) elif getattr(misp_attribute, 'type') == URL_TYPE: data.append([Paragraph(item[0], col1_style), get_unoverflowable_paragraph( - "" + getattr(misp_attribute, - item[ - 1]) + "", - col2_style, False)]) + "" + getattr(misp_attribute,item[1]) + "",col2_style, False)]) + elif getattr(misp_attribute, 'type') == IMAGE_TYPE: + # Get the image + buf = getattr(misp_attribute, 'data') + + # Scale down (or up ?) the image to fit the maximum frame size + img_size = ImageReader(buf).getSize() + w_scale = FRAME_MAX_WIDTH / img_size[0] + h_scale = FRAME_MAX_HEIGHT / img_size[1] + scale_down = min(w_scale,h_scale) + + print(FRAME_MAX_HEIGHT,FRAME_MAX_WIDTH) + print(img_size) + print(w_scale) + print(h_scale) + print(scale_down) + print(img_size[0]*scale_down, img_size[1]*scale_down) + + # img_width = 88 * mm # max image height + # print(img_size) + FRAME_PICTURE_MAX_WIDTH = 88*mm + FRAME_PICTURE_MAX_HEIGHT = 195*mm + # return Image(path, width=width, height=(width * aspect)) + img = Image(buf,width=FRAME_PICTURE_MAX_WIDTH,height=FRAME_PICTURE_MAX_HEIGHT,kind='bound') + # width, height = img.getSize() + # aspect = height / float(width) + data.append([Paragraph('data', col1_style),img]) + + # Tags item = ["Tags", 'Tag', "None"] diff --git a/tests/test_reportlab.py b/tests/test_reportlab.py index af0dcd7..b2b968f 100644 --- a/tests/test_reportlab.py +++ b/tests/test_reportlab.py @@ -10,7 +10,7 @@ import sys import os import time -manual_testing = False +manual_testing = True class TestMISPEvent(unittest.TestCase): @@ -126,6 +126,21 @@ class TestMISPEvent(unittest.TestCase): reportlab_generator.register_value_to_file(reportlab_generator.convert_event_in_pdf_buffer(self.mispevent, config), self.storage_folder + "config_partial_1.pdf") + def test_image_json(self): + if self.check_python_2(): + self.assertTrue(True) + else: + + config = {} + moduleconfig = ["MISP_base_url_for_dynamic_link", "MISP_name_for_metadata"] + config[moduleconfig[0]] = "http://localhost:8080" + config[moduleconfig[1]] = "My Wonderful CERT" + + self.init_event() + self.mispevent.load_file(self.test_folder + 'image_event.json') + reportlab_generator.register_value_to_file(reportlab_generator.convert_event_in_pdf_buffer(self.mispevent, config), + self.storage_folder + "image_event.pdf") + def test_batch_OSINT_events(self): # Test case ONLY for manual testing. Needs to download a full list of OSINT events ! @@ -152,3 +167,4 @@ class TestMISPEvent(unittest.TestCase): reportlab_generator.convert_event_in_pdf_buffer(self.mispevent), self.storage_folder + curr_file + ".pdf") print("Elapsed time : " + str(time.time() - t)) + # Local run : 1958.930s for 1064 files