Skip to content

Commit

Permalink
Merge pull request #161 from plone/scale-svg
Browse files Browse the repository at this point in the history
Fix: Upload a svg without width and height set
  • Loading branch information
mauritsvanrees authored Jul 26, 2024
2 parents 4714969 + e94e42f commit 2db172f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/161.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix: Upload a svg without width and height set @dobri1408
11 changes: 11 additions & 0 deletions plone/namedfile/utils/svg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 2db172f

Please sign in to comment.