Skip to content

Commit

Permalink
Also factor out creating the scale or handling an svg.
Browse files Browse the repository at this point in the history
Makes the __call__ method smaller.
And makes it easier to close the possibly open blob file.
  • Loading branch information
mauritsvanrees committed Dec 23, 2021
1 parent 5fb1c8b commit b4f8020
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions plone/namedfile/scaling.py
Expand Up @@ -247,6 +247,35 @@ def create_scale(self, data, direction, height, width, **parameters):
data, direction=direction, height=height, width=width, **parameters
)

def handle_image(
self, orig_value, orig_data, direction, height, width, **parameters
):
"""Return a scaled image, its mimetype format, and width and height."""
if getattr(orig_value, "contentType", "") == "image/svg+xml":
# No need to scale, we can simply use the original data,
# but report a different width and height.
if isinstance(orig_data, (six.text_type)):
orig_data = safe_encode(orig_data)
if isinstance(orig_data, (bytes)):
orig_data = BytesIO(orig_data)
result = orig_data.read(), "svg+xml", (width, height)
return result
try:
result = self.create_scale(
orig_data, direction=direction, height=height, width=width, **parameters
)
except (ConflictError, KeyboardInterrupt):
raise
except Exception:
logger.exception(
'Could not scale "{0!r}" of {1!r}'.format(
orig_value,
self.url(),
),
)
return
return result

def __call__(
self,
fieldname=None,
Expand Down Expand Up @@ -291,34 +320,18 @@ def __call__(
return

parameters = self.update_parameters(**parameters)

if not getattr(orig_value, "contentType", "") == "image/svg+xml":
try:
result = self.create_scale(
orig_data,
direction=direction,
height=height,
width=width,
**parameters
)
except (ConflictError, KeyboardInterrupt):
raise
except Exception:
logger.exception(
'Could not scale "{0!r}" of {1!r}'.format(
orig_value, self.url(),
),
)
return
if result is None:
return
else:
if isinstance(orig_data, (six.text_type)):
orig_data = safe_encode(orig_data)
if isinstance(orig_data, (bytes)):
orig_data = BytesIO(orig_data)

result = orig_data.read(), "svg+xml", (width, height)
try:
result = self.handle_image(
orig_value, orig_data, direction, height, width, **parameters
)
finally:
# Make sure the file is closed to avoid error:
# ZODB-5.5.1-py3.7.egg/ZODB/blob.py:339: ResourceWarning:
# unclosed file <_io.FileIO ... mode='rb' closefd=True>
if isinstance(orig_data, BlobFile):
orig_data.close()
if result is None:
return

# Note: the format may differ from the original.
# For example a TIFF may have been turned into a PNG.
Expand All @@ -329,12 +342,6 @@ def __call__(
)
value.fieldname = self.fieldname

# make sure the file is closed to avoid error:
# ZODB-5.5.1-py3.7.egg/ZODB/blob.py:339: ResourceWarning:
# unclosed file <_io.FileIO ... mode='rb' closefd=True>
if isinstance(orig_data, BlobFile):
orig_data.close()

return value, format_, dimensions


Expand Down

0 comments on commit b4f8020

Please sign in to comment.