Commit 2bd429c8 authored by Julien Muchembled's avatar Julien Muchembled

Fix possible resource leak when resizing images

This is similar to commit a0d26594
("Fix possible resource leak with ImageMagick conversion").
parent ff8c53a8
...@@ -311,8 +311,6 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -311,8 +311,6 @@ class Image(TextConvertableMixin, File, OFSImage):
def _resize(self, quality, width, height, format, resolution, frame): def _resize(self, quality, width, height, format, resolution, frame):
"""Resize and resample photo.""" """Resize and resample photo."""
newimg = StringIO()
parameter_list = ['convert'] parameter_list = ['convert']
parameter_list.extend(['-colorspace', 'RGB']) parameter_list.extend(['-colorspace', 'RGB'])
if format not in VALID_TRANSPARENT_IMAGE_FORMAT_LIST: if format not in VALID_TRANSPARENT_IMAGE_FORMAT_LIST:
...@@ -336,26 +334,15 @@ class Image(TextConvertableMixin, File, OFSImage): ...@@ -336,26 +334,15 @@ class Image(TextConvertableMixin, File, OFSImage):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True) close_fds=True)
imgin, imgout, err = process.stdin, process.stdout, process.stderr try:
# XXX: The only portable way is to pass what stdin.write can accept,
def writeData(stream, data): # which is a string for PIPE.
if isinstance(data, str): image, err = process.communicate(str(self.getData()))
stream.write(str(self.getData())) finally:
else: del process
# Use PData structure to prevent if image:
# consuming too much memory return StringIO(image)
while data is not None: raise ConversionError('Image conversion failed (%s).' % err)
stream.write(data.data)
data = data.next
writeData(imgin, self.getData())
imgin.close()
newimg.write(imgout.read())
imgout.close()
if not newimg.tell():
raise ConversionError('Image conversion failed (%s).' % err.read())
newimg.seek(0)
return newimg
def _getDisplayData(self, format, quality, resolution, frame, image_size): def _getDisplayData(self, format, quality, resolution, frame, image_size):
"""Return raw photo data for given display.""" """Return raw photo data for given display."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment