Skip to content

Fix: Upload a svg without width and height set #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading