add : [exportpdf] Picture management, manual

pull/352/head
Falconieri 2019-02-26 11:13:24 +01:00
parent f36d8908a7
commit cefcc3430c
2 changed files with 53 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import sys
if sys.version_info.major >= 3: if sys.version_info.major >= 3:
from html import escape from html import escape
# import PIL
else: else:
print( print(
"ExportPDF running with Python < 3 : stability and output not guaranteed. Please run exportPDF with at least Python3") "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.pdfmetrics import stringWidth
from reportlab.pdfbase.pdfdoc import PDFDictionary, PDFInfo from reportlab.pdfbase.pdfdoc import PDFDictionary, PDFInfo
from reportlab.lib import colors 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.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import mm from reportlab.lib.units import mm
@ -454,10 +456,12 @@ def create_flowable_table_from_one_attribute(misp_attribute):
["Type", 'type', "None"], ["Type", 'type', "None"],
["Value", 'value', "None"]] ["Value", 'value', "None"]]
IMAGE_TYPE = "attachment"
# Handle the special case of links # Handle the special case of links
STANDARD_TYPE = True STANDARD_TYPE = True
if hasattr(misp_attribute, 'type') and ( 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): # getattr(misp_attribute, 'type') == LINK_TYPE or getattr(misp_attribute, 'type') == URL_TYPE):
# Special case for links # Special case for links
STANDARD_TYPE = False STANDARD_TYPE = False
@ -482,10 +486,35 @@ def create_flowable_table_from_one_attribute(misp_attribute):
misp_attribute, item[1]) + "</a></font>", col2_style, False)]) misp_attribute, item[1]) + "</a></font>", col2_style, False)])
elif getattr(misp_attribute, 'type') == URL_TYPE: elif getattr(misp_attribute, 'type') == URL_TYPE:
data.append([Paragraph(item[0], col1_style), get_unoverflowable_paragraph( data.append([Paragraph(item[0], col1_style), get_unoverflowable_paragraph(
"<font color=" + BAD_LINK_COLOR + "><a href=" + WARNING_MESSAGE_URL + ">" + getattr(misp_attribute, "<font color=" + BAD_LINK_COLOR + "><a href=" + WARNING_MESSAGE_URL + ">" + getattr(misp_attribute,item[1]) + "</a></font>",col2_style, False)])
item[ elif getattr(misp_attribute, 'type') == IMAGE_TYPE:
1]) + "</a></font>", # Get the image
col2_style, False)]) 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 # Tags
item = ["Tags", 'Tag', "None"] item = ["Tags", 'Tag', "None"]

View File

@ -10,7 +10,7 @@ import sys
import os import os
import time import time
manual_testing = False manual_testing = True
class TestMISPEvent(unittest.TestCase): 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), reportlab_generator.register_value_to_file(reportlab_generator.convert_event_in_pdf_buffer(self.mispevent, config),
self.storage_folder + "config_partial_1.pdf") 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): def test_batch_OSINT_events(self):
# Test case ONLY for manual testing. Needs to download a full list of OSINT events ! # 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), reportlab_generator.convert_event_in_pdf_buffer(self.mispevent),
self.storage_folder + curr_file + ".pdf") self.storage_folder + curr_file + ".pdf")
print("Elapsed time : " + str(time.time() - t)) print("Elapsed time : " + str(time.time() - t))
# Local run : 1958.930s for 1064 files