Skip to content
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

Add support for SVG files with large header #148

Merged
merged 4 commits into from
Aug 28, 2023
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
6 changes: 6 additions & 0 deletions news/147.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixed the issue where SVG images containing extensive metadata were not being displayed
correctly (resulting in a width/height of 1px). This problem could occur when the
<svg> tag exceeded the MAX_INFO_BYTES limit.

Fixes `issue 147 <https://github.com/plone/plone.namedfile/issues/147>`_.
[mliebischer]
3 changes: 2 additions & 1 deletion plone/namedfile/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ def _setData(self, data):
super()._setData(data)
firstbytes = self.getFirstBytes()
res = getImageInfo(firstbytes)
if res == ("image/jpeg", -1, -1) or res == ("image/tiff", -1, -1):
if res == ("image/jpeg", -1, -1) or res == ("image/tiff", -1, -1) or \
res == ("image/svg+xml", -1, -1):
# header was longer than firstbytes
start = len(firstbytes)
length = max(0, MAX_INFO_BYTES - start)
Expand Down
4 changes: 2 additions & 2 deletions plone/namedfile/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os


def getFile(filename):
def getFile(filename, length=None):
"""return contents of the file with the given name"""
filename = os.path.join(os.path.dirname(__file__), filename)
with open(filename, "rb") as data_file:
return data_file.read()
return data_file.read(length)
661 changes: 661 additions & 0 deletions plone/namedfile/tests/image_large_header.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion plone/namedfile/tests/test_blobfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from zope.component import provideUtility
from zope.interface.verify import verifyClass

import os
import struct
import transaction
import unittest
Expand Down Expand Up @@ -79,7 +80,7 @@ def testInterface(self):
self.assertTrue(INamedBlobImage.implementedBy(NamedBlobImage))
self.assertTrue(verifyClass(INamedBlobFile, NamedBlobImage))

def testDataMutatorWithLargeHeader(self):
def testDataMutatorWithLargeJPGHeader(self):
from plone.namedfile.file import IMAGE_INFO_BYTES

bogus_header_length = struct.pack(">H", IMAGE_INFO_BYTES * 2)
Expand All @@ -93,6 +94,23 @@ def testDataMutatorWithLargeHeader(self):
image._setData(data)
self.assertEqual(image.getImageSize(), (1024, 680))

def testDataMutatorWithLargeSVGHeader(self):
from plone.namedfile.file import IMAGE_INFO_BYTES

to_big_header_data = b'd' * (IMAGE_INFO_BYTES * 2)

data = (
b'<svg xmlns="http://www.w3.org/2000/svg" '
b'width="1024px" '
b'height="680px" '
b'foobar="' + to_big_header_data + b'">'
b'</svg>"'
)
image = self._makeImage()
image._setData(data)
self.assertEqual(image.getImageSize(), (1024, 680))
self.assertGreater(len(to_big_header_data), IMAGE_INFO_BYTES)


class TestImageFunctional(unittest.TestCase):

Expand Down
23 changes: 21 additions & 2 deletions plone/namedfile/tests/test_svg.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import unittest

from plone.namedfile.file import NamedImage
from plone.namedfile.tests import getFile
from plone.namedfile.utils import get_contenttype
from plone.namedfile.utils.svg_utils import dimension_int
from plone.namedfile.utils.svg_utils import process_svg

import unittest


class TestSvg(unittest.TestCase):
def test_get_contenttype(self):
Expand All @@ -23,6 +23,25 @@ def test_process_svg(self):
self.assertEqual(width, 158)
self.assertEqual(height, 40)

def test_process_svg__indicate_header_truncation(self):
""" Check that we can detect SVG files where the file header was
larger than the requested first bytes. process_svg() should
return -1 as dimensions to indicate the truncation."""

truncated_data = getFile("image_large_header.svg", length=1024)
content_type, width, height = process_svg(truncated_data)
self.assertEqual(content_type, "image/svg+xml")
self.assertEqual(width, -1)
self.assertEqual(height, -1)

def test_process_svg__can_handle_large_header(self):

data = getFile("image_large_header.svg")
content_type, width, height = process_svg(data)
self.assertEqual(content_type, "image/svg+xml")
self.assertEqual(width, 1041)
self.assertEqual(height, 751)

def test_dimension_int(self):

self.assertEqual(dimension_int("auto"), 0)
Expand Down
7 changes: 4 additions & 3 deletions plone/namedfile/utils/svg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ def process_svg(data):
w = dimension_int(el.attrib.get("width"))
h = dimension_int(el.attrib.get("height"))
break
except et.ParseError:
w = w if w > 1 else 1
h = h if h > 1 else 1
except et.ParseError as e:
log.debug(f"Failed to parse SVG dimensions: {e}")
pass

if tag == "{http://www.w3.org/2000/svg}svg" or (
size == 1024 and b"http://www.w3.org/2000/svg" in data
):
content_type = "image/svg+xml"
w = w if w > 1 else 1
h = h if h > 1 else 1

return content_type, w, h

Expand Down
Loading