diff --git a/lookyloo/lookyloo.py b/lookyloo/lookyloo.py index 9a3fd7a9..a4303ba0 100644 --- a/lookyloo/lookyloo.py +++ b/lookyloo/lookyloo.py @@ -644,21 +644,24 @@ class Lookyloo(): '''Get the screenshot(s) of the rendered page''' return self._get_raw(capture_uuid, 'png', all_files=False) - def get_screenshot_thumbnail(self, capture_uuid: str, for_datauri=False) -> Union[str, BytesIO]: - '''Get the thumbnail of the rendered page''' + def get_screenshot_thumbnail(self, capture_uuid: str, for_datauri: bool=False, width: int=64) -> Union[str, BytesIO]: + '''Get the thumbnail of the rendered page. Always crop to a square.''' to_return = BytesIO() - size = 64, 64 + size = width, width try: s = self.get_screenshot(capture_uuid) - with Image.open(s) as screenshot: - c_screenshot = screenshot.crop((0, 0, screenshot.width, screenshot.width)) - c_screenshot.thumbnail(size) - c_screenshot.save(to_return, 'png') + orig_screenshot = Image.open(s) + to_thumbnail = orig_screenshot.crop((0, 0, orig_screenshot.width, orig_screenshot.width)) except Image.DecompressionBombError as e: # The image is most probably too big: https://pillow.readthedocs.io/en/stable/reference/Image.html self.logger.warning(f'Unable to generate the screenshot thumbnail of {capture_uuid}: image too big ({e}).') - # TODO: Default image + error_img: Path = get_homedir() / 'website' / 'web' / 'static' / 'error_screenshot.png' + to_thumbnail = Image.open(error_img) + to_thumbnail.thumbnail(size) + to_thumbnail.save(to_return, 'png') + + to_return.seek(0) if for_datauri: return base64.b64encode(to_return.getvalue()).decode() else: diff --git a/website/web/__init__.py b/website/web/__init__.py index 54b99463..892dcce8 100644 --- a/website/web/__init__.py +++ b/website/web/__init__.py @@ -280,6 +280,13 @@ def image(tree_uuid: str): as_attachment=True, attachment_filename='image.png') +@app.route('/tree//thumbnail/', defaults={'width': 64}, methods=['GET']) +@app.route('/tree//thumbnail/', methods=['GET']) +def thumbnail(tree_uuid: str, width: int): + to_return = lookyloo.get_screenshot_thumbnail(tree_uuid, for_datauri=False, width=width) + return send_file(to_return, mimetype='image/png') + + @app.route('/tree//html', methods=['GET']) def html(tree_uuid: str): to_return = lookyloo.get_html(tree_uuid) diff --git a/website/web/static/tree.js b/website/web/static/tree.js index 531522c9..52cb7fe6 100644 --- a/website/web/static/tree.js +++ b/website/web/static/tree.js @@ -492,7 +492,7 @@ function update(root, computed_node_width=0) { .attr('id', 'screenshot_thumbnail') .attr("width", thumbnail_size) .attr("height", thumbnail_size) - .attr("xlink:href", screenshot_thumbnail ? `data:image/png;base64,${screenshot_thumbnail}` : '/static/error_screenshot.png') + .attr("xlink:href", `data:image/png;base64,${screenshot_thumbnail}`) .attr('cursor', 'pointer') .on('mouseover', (event, d) => { d3.select('#tooltip') diff --git a/website/web/templates/tree.html b/website/web/templates/tree.html index 3ebf3d9a..65f32589 100644 --- a/website/web/templates/tree.html +++ b/website/web/templates/tree.html @@ -18,7 +18,7 @@ />