pull/4/merge
Erik Johnston 7 years ago
parent 505371414f
commit 0e28281a02
  1. 62
      synapse/rest/media/v1/media_repository.py
  2. 13
      synapse/rest/media/v1/thumbnailer.py

@ -131,10 +131,9 @@ class MediaRepository(object):
fname = os.path.join(self.primary_base_path, path)
# Write to the main repository
yield make_deferred_yieldable(
threads.deferToThread,
yield make_deferred_yieldable(threads.deferToThread(
self._write_file_synchronously, source, fname,
)
))
# Write to backup repository
yield self.copy_to_backup(source, path)
@ -157,11 +156,10 @@ class MediaRepository(object):
# We can either wait for successful writing to the backup repository
# or write in the background and immediately return
if self.synchronous_backup_media_store:
yield make_deferred_yieldable(
threads.deferToThread,
yield make_deferred_yieldable(threads.deferToThread(
self._write_file_synchronously, source, backup_fname,
close_source=True,
)
))
else:
preserve_fn(threads.deferToThread)(
self._write_file_synchronously, source, backup_fname,
@ -378,11 +376,10 @@ class MediaRepository(object):
input_path = self.filepaths.local_media_filepath(media_id)
thumbnailer = Thumbnailer(input_path)
t_byte_source = yield make_deferred_yieldable(
threads.deferToThread,
t_byte_source = yield make_deferred_yieldable(threads.deferToThread(
self._generate_thumbnail,
thumbnailer, t_width, t_height, t_method, t_type
)
))
if t_byte_source:
t_width, t_height = t_byte_source.dimensions
@ -409,11 +406,10 @@ class MediaRepository(object):
input_path = self.filepaths.remote_media_filepath(server_name, file_id)
thumbnailer = Thumbnailer(input_path)
t_byte_source = yield make_deferred_yieldable(
threads.deferToThread,
t_byte_source = yield make_deferred_yieldable(threads.deferToThread(
self._generate_thumbnail,
thumbnailer, t_width, t_height, t_method, t_type
)
))
if t_byte_source:
t_width, t_height = t_byte_source.dimensions
@ -478,34 +474,32 @@ class MediaRepository(object):
thumbnails = {}
for r_width, r_height, r_method, r_type in requirements:
if r_method == "crop":
thumbnails.setdefault[(r_width, r_height)] = (r_method, r_type)
thumbnails.setdefault((r_width, r_height), (r_method, r_type))
elif r_method == "scale":
t_width, t_height = thumbnailer.aspect(t_width, t_height)
t_width, t_height = thumbnailer.aspect(r_width, r_height)
t_width = min(m_width, t_width)
t_height = min(m_height, t_height)
thumbnails[(t_width, t_height)] = (r_method, r_type)
# Now we generate the thumbnails for each dimension, store it
for (r_width, r_height), (r_method, r_type) in thumbnails.iteritems():
t_byte_source = thumbnailer.crop(t_width, t_height, t_type)
if r_type == "crop":
t_byte_source = yield make_deferred_yieldable(
threads.deferToThread, thumbnailer.crop,
r_width, r_height, r_type,
)
for (t_width, t_height), (t_method, t_type) in thumbnails.iteritems():
# Generate the thumbnail
if t_type == "crop":
t_byte_source = yield make_deferred_yieldable(threads.deferToThread(
thumbnailer.crop,
r_width, r_height, t_type,
))
else:
t_byte_source = yield make_deferred_yieldable(
threads.deferToThread, thumbnailer.scale,
r_width, r_height, r_type,
)
t_width, t_height = t_byte_source.dimensions
t_byte_source = yield make_deferred_yieldable(threads.deferToThread(
thumbnailer.scale,
r_width, r_height, t_type,
))
# Work out the correct file name for thumbnail
if server_name:
file_path = self.filepaths.remote_media_thumbnail_rel(
server_name, file_id, t_width, t_height, t_type, t_method
)
server_name, file_id, t_width, t_height, t_type, t_method
)
elif url_cache:
file_path = self.filepaths.url_cache_thumbnail_rel(
media_id, t_width, t_height, t_type, t_method
@ -515,14 +509,16 @@ class MediaRepository(object):
media_id, t_width, t_height, t_type, t_method
)
# Write to disk
output_path = yield self.write_to_file(t_byte_source, file_path)
t_len = os.path.getsize(output_path)
# Write to database
if server_name:
yield self.store.store_remote_media_thumbnail(
server_name, media_id, file_id,
t_width, t_height, t_type, t_method, t_len
)
server_name, media_id, file_id,
t_width, t_height, t_type, t_method, t_len
)
else:
yield self.store.store_local_thumbnail(
media_id, t_width, t_height, t_type, t_method, t_len

@ -54,7 +54,7 @@ class Thumbnailer(object):
"""Rescales the image to the given dimensions.
Returns:
ImageIO: the bytes of the encoded image ready to be written to disk
BytesIO: the bytes of the encoded image ready to be written to disk
"""
scaled = self.image.resize((width, height), Image.ANTIALIAS)
return self._encode_image(scaled, output_type)
@ -71,7 +71,7 @@ class Thumbnailer(object):
max_height: The larget possible height.
Returns:
ImageIO: the bytes of the encoded image ready to be written to disk
BytesIO: the bytes of the encoded image ready to be written to disk
"""
if width * self.height > height * self.width:
scaled_height = (width * self.height) // self.width
@ -92,13 +92,6 @@ class Thumbnailer(object):
return self._encode_image(cropped, output_type)
def _encode_image(self, output_image, output_type):
output_bytes_io = ImageIO(output_image.size)
output_bytes_io = BytesIO()
output_image.save(output_bytes_io, self.FORMATS[output_type], quality=80)
output_image.close()
return output_bytes_io
class ImageIO(BytesIO):
def __init__(self, dimensions):
super(ImageIO, self).__init__()
self.dimensions = dimensions

Loading…
Cancel
Save