diff --git a/gns3server/controller/drawing.py b/gns3server/controller/drawing.py index b0c727ac..2675ce1b 100644 --- a/gns3server/controller/drawing.py +++ b/gns3server/controller/drawing.py @@ -72,8 +72,8 @@ class Drawing: try: return data.decode() except UnicodeError: - width, height = get_size(data) - return "\n\n".format(b64=base64.b64encode(data).decode(), extension=filename.split(".")[1], width=width, height=width) + width, height, filetype = get_size(data) + return "\n\n".format(b64=base64.b64encode(data).decode(), filetype=filetype, width=width, height=width) except OSError: log.warning("Image file %s missing", filename) return "" diff --git a/gns3server/utils/picture.py b/gns3server/utils/picture.py index abde33d2..34823113 100644 --- a/gns3server/utils/picture.py +++ b/gns3server/utils/picture.py @@ -15,16 +15,22 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import io import struct +from xml.etree.ElementTree import ElementTree - -def get_size(data): +def get_size(data, default_width=0, default_height=0): """ Get image size :param data: A buffer with image content - :return: Tuple (width, height) + :return: Tuple (width, height, filetype) """ + + height = default_height + width = default_width + filetype = None + # Original version: # https://github.com/shibukawa/imagesize_py # @@ -38,21 +44,20 @@ def get_size(data): # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - height = 0 - width = 0 - size = len(data) # handle GIFs if size >= 10 and data[:6] in (b'GIF87a', b'GIF89a'): # Check to see if content_type is correct try: width, height = struct.unpack("= 24 and data.startswith(b'\211PNG\r\n\032\n') and data[12:16] == b'IHDR': try: width, height = struct.unpack(">LL", data[16:24]) + filetype = "png" except struct.error: raise ValueError("Invalid PNG file") # Maybe this is for an older PNG version. @@ -60,12 +65,14 @@ def get_size(data): # Check to see if we have the right content type try: width, height = struct.unpack(">LL", data[8:16]) + filetype = "png" except struct.error: raise ValueError("Invalid PNG file") # handle JPEGs elif size >= 2 and data.startswith(b'\377\330'): try: - fhandle.seek(0) # Read 0xff next + # Not very efficient to copy data to a buffer + fhandle = io.BytesIO(data) size = 2 ftype = 0 while not 0xc0 <= ftype <= 0xcf: @@ -78,13 +85,44 @@ def get_size(data): # We are at a SOFn block fhandle.seek(1, 1) # Skip `precision' byte. height, width = struct.unpack('>HH', fhandle.read(4)) + filetype = "jpg" except struct.error: raise ValueError("Invalid JPEG file") - # handle JPEG2000s - elif size >= 12 and data.startswith(b'\x00\x00\x00\x0cjP \r\n\x87\n'): - fhandle.seek(48) + # End of https://github.com/shibukawa/imagesize_py + + # handle SVG + elif size >= 10 and data.startswith(b'LL', fhandle.read(8)) - except struct.error: - raise ValueError("Invalid JPEG2000 file") - return width, height + width = _svg_convert_size(root.attrib["width"]) + height = _svg_convert_size(root.attrib["height"]) + except IndexError: + raise ValueError("Invalid SVG file") + + return width, height, filetype + +def _svg_convert_size(size): + """ + Convert svg size to the px version + + :param size: String with the size + """ + + # https://www.w3.org/TR/SVG/coords.html#Units + conversion_table = { + "pt": 1.25, + "pc": 15, + "mm": 3.543307, + "cm": 35.43307, + "in": 90 + } + if len(size) > 3: + if size[-2:] in conversion_table: + return round(float(size[:-2]) * conversion_table[size[-2:]]) + + return round(float(size)) diff --git a/tests/resources/gns3_icon_128x128.png b/tests/resources/gns3_icon_128x128.png new file mode 100644 index 00000000..b7fc2a20 Binary files /dev/null and b/tests/resources/gns3_icon_128x128.png differ diff --git a/tests/resources/gns3_icon_128x64.gif b/tests/resources/gns3_icon_128x64.gif new file mode 100644 index 00000000..28cb2d50 Binary files /dev/null and b/tests/resources/gns3_icon_128x64.gif differ diff --git a/tests/resources/gns3_icon_128x64.jpg b/tests/resources/gns3_icon_128x64.jpg new file mode 100644 index 00000000..8aa412c3 Binary files /dev/null and b/tests/resources/gns3_icon_128x64.jpg differ diff --git a/tests/resources/gns3_icon_128x64.png b/tests/resources/gns3_icon_128x64.png new file mode 100644 index 00000000..f228684d Binary files /dev/null and b/tests/resources/gns3_icon_128x64.png differ diff --git a/tests/utils/test_picture.py b/tests/utils/test_picture.py new file mode 100644 index 00000000..d2764c40 --- /dev/null +++ b/tests/utils/test_picture.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +from gns3server.utils.picture import get_size + + +def test_get_size(): + with open("tests/resources/nvram_iou", "rb") as f: + res = get_size(f.read(), default_width=100, default_height=50) + assert res == (100, 50, None) + with open("tests/resources/gns3_icon_128x64.gif", "rb") as f: + res = get_size(f.read()) + assert res == (128, 64, "gif") + with open("tests/resources/gns3_icon_128x64.jpg", "rb") as f: + res = get_size(f.read()) + assert res == (128, 64, "jpg") + with open("tests/resources/gns3_icon_128x64.png", "rb") as f: + res = get_size(f.read()) + assert res == (128, 64, "png") + with open("gns3server/symbols/dslam.svg", "rb") as f: + res = get_size(f.read()) + assert res == (50, 53, "svg") + # Symbol using size with cm + with open("gns3server/symbols/cloud.svg", "rb") as f: + res = get_size(f.read()) + assert res == (159, 71, "svg")