From 4bbf7c2ed2ae55fe88be3901d1b482228b67bd0d Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Thu, 8 Apr 2021 21:27:05 +0200 Subject: [PATCH 1/2] Update symbol_handler.py --- .../handlers/api/controller/symbol_handler.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/gns3server/handlers/api/controller/symbol_handler.py b/gns3server/handlers/api/controller/symbol_handler.py index 42d34635..2bc1244e 100644 --- a/gns3server/handlers/api/controller/symbol_handler.py +++ b/gns3server/handlers/api/controller/symbol_handler.py @@ -19,6 +19,7 @@ import os import aiohttp import asyncio import urllib.parse +import xml.etree.ElementTree as ET from gns3server.web.route import Route from gns3server.controller import Controller @@ -44,6 +45,42 @@ class SymbolHandler: controller = Controller.instance() response.json(controller.symbols.list()) + @Route.get( + r"/symbols/{symbol_id:.+}/dimensions", + description="Get the symbol dimensions", + status_codes={ + 200: "Symbol dimensions returned" + }) + async def raw(request, response): + + controller = Controller.instance() + symbol_id = urllib.parse.unquote(request.match_info["symbol_id"]) + try: + file_content = open(controller.symbols.get_path(symbol_id), 'r').read() + svg_root = ET.fromstring(file_content) + + svg_width = svg_root.get('width') + if svg_width is not None: + try: + width = int(float(svg_width)) + except: + log.warning("Could not get width for symbol with id: {}".format(symbol_id)) + width = 0 + + svg_height = svg_root.get('height') + if svg_height is not None: + try: + height = int(float(svg_height)) + except: + log.warning("Could not get height for symbol with id: {}".format(symbol_id)) + height = 0 + + symbol_dimensions = { 'width': width, 'height': height } + response.json(symbol_dimensions) + except (KeyError, OSError) as e: + log.warning("Could not get symbol file: {}".format(e)) + response.set_status(404) + @Route.get( r"/symbols/{symbol_id:.+}/raw", description="Get the symbol file", From 30cbfe21543677dac82e4099802d89b4f09277f0 Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Thu, 8 Apr 2021 23:29:46 +0200 Subject: [PATCH 2/2] Update picture.py --- .../handlers/api/controller/symbol_handler.py | 23 ++----------------- gns3server/utils/picture.py | 2 +- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/gns3server/handlers/api/controller/symbol_handler.py b/gns3server/handlers/api/controller/symbol_handler.py index 2bc1244e..cb7139f7 100644 --- a/gns3server/handlers/api/controller/symbol_handler.py +++ b/gns3server/handlers/api/controller/symbol_handler.py @@ -19,11 +19,10 @@ import os import aiohttp import asyncio import urllib.parse -import xml.etree.ElementTree as ET from gns3server.web.route import Route from gns3server.controller import Controller - +from gns3server.utils.picture import get_size import logging log = logging.getLogger(__name__) @@ -56,25 +55,7 @@ class SymbolHandler: controller = Controller.instance() symbol_id = urllib.parse.unquote(request.match_info["symbol_id"]) try: - file_content = open(controller.symbols.get_path(symbol_id), 'r').read() - svg_root = ET.fromstring(file_content) - - svg_width = svg_root.get('width') - if svg_width is not None: - try: - width = int(float(svg_width)) - except: - log.warning("Could not get width for symbol with id: {}".format(symbol_id)) - width = 0 - - svg_height = svg_root.get('height') - if svg_height is not None: - try: - height = int(float(svg_height)) - except: - log.warning("Could not get height for symbol with id: {}".format(symbol_id)) - height = 0 - + width, height, _ = controller.symbols.get_size(symbol_id) symbol_dimensions = { 'width': width, 'height': height } response.json(symbol_dimensions) except (KeyError, OSError) as e: diff --git a/gns3server/utils/picture.py b/gns3server/utils/picture.py index 3e9fd416..2bdb956b 100644 --- a/gns3server/utils/picture.py +++ b/gns3server/utils/picture.py @@ -92,7 +92,7 @@ def get_size(data, default_width=0, default_height=0): # End of https://github.com/shibukawa/imagesize_py # handle SVG - elif size >= 10 and data.startswith(b'= 10 and (data.startswith(b'