diff --git a/news/161.bugfix b/news/161.bugfix new file mode 100644 index 00000000..90ad8cac --- /dev/null +++ b/news/161.bugfix @@ -0,0 +1 @@ +Fix: Upload a svg without width and height set @dobri1408 diff --git a/plone/namedfile/utils/svg_utils.py b/plone/namedfile/utils/svg_utils.py index 59c8dd50..33e36176 100644 --- a/plone/namedfile/utils/svg_utils.py +++ b/plone/namedfile/utils/svg_utils.py @@ -7,6 +7,12 @@ log = getLogger(__name__) +def calculate_dimensions_from_viewbox(view_box): + parts = [float(x) for x in view_box.split()] + if len(parts) == 4: + return int(parts[2]), int(parts[3]) + return 1, 1 + def process_svg(data): content_type = None @@ -15,12 +21,17 @@ def process_svg(data): size = len(data) tag = None + view_box = None try: for event, el in et.iterparse(BytesIO(data), ("start",)): tag = el.tag w = dimension_int(el.attrib.get("width")) h = dimension_int(el.attrib.get("height")) + view_box = el.attrib.get("viewBox") break + + if (w == 0 or h == 0) and view_box: + w, h = calculate_dimensions_from_viewbox(view_box) w = w if w > 1 else 1 h = h if h > 1 else 1 except et.ParseError as e: