0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-17 05:13:50 +01:00

Add try/finally block to close t_byte_source

This commit is contained in:
Erik Johnston 2017-10-13 15:34:08 +01:00
parent 6b725cf56a
commit 1b6b0b1e66

View file

@ -367,12 +367,16 @@ class MediaRepository(object):
)) ))
if t_byte_source: if t_byte_source:
output_path = yield self.write_to_file_and_backup( try:
t_byte_source, output_path = yield self.write_to_file_and_backup(
self.filepaths.local_media_thumbnail_rel( t_byte_source,
media_id, t_width, t_height, t_type, t_method self.filepaths.local_media_thumbnail_rel(
media_id, t_width, t_height, t_type, t_method
)
) )
) finally:
t_byte_source.close()
logger.info("Stored thumbnail in file %r", output_path) logger.info("Stored thumbnail in file %r", output_path)
t_len = os.path.getsize(output_path) t_len = os.path.getsize(output_path)
@ -395,12 +399,16 @@ class MediaRepository(object):
)) ))
if t_byte_source: if t_byte_source:
output_path = yield self.write_to_file_and_backup( try:
t_byte_source, output_path = yield self.write_to_file_and_backup(
self.filepaths.remote_media_thumbnail_rel( t_byte_source,
server_name, file_id, t_width, t_height, t_type, t_method self.filepaths.remote_media_thumbnail_rel(
server_name, file_id, t_width, t_height, t_type, t_method
)
) )
) finally:
t_byte_source.close()
logger.info("Stored thumbnail in file %r", output_path) logger.info("Stored thumbnail in file %r", output_path)
t_len = os.path.getsize(output_path) t_len = os.path.getsize(output_path)
@ -464,18 +472,6 @@ class MediaRepository(object):
# Now we generate the thumbnails for each dimension, store it # Now we generate the thumbnails for each dimension, store it
for (t_width, t_height, t_type), t_method in thumbnails.iteritems(): for (t_width, t_height, t_type), t_method 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, t_type,
))
# Work out the correct file name for thumbnail # Work out the correct file name for thumbnail
if server_name: if server_name:
file_path = self.filepaths.remote_media_thumbnail_rel( file_path = self.filepaths.remote_media_thumbnail_rel(
@ -490,8 +486,29 @@ class MediaRepository(object):
media_id, t_width, t_height, t_type, t_method media_id, t_width, t_height, t_type, t_method
) )
# Write to disk # Generate the thumbnail
output_path = yield self.write_to_file_and_backup(t_byte_source, file_path) 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, t_type,
))
if not t_byte_source:
continue
try:
# Write to disk
output_path = yield self.write_to_file_and_backup(
t_byte_source, file_path,
)
finally:
t_byte_source.close()
t_len = os.path.getsize(output_path) t_len = os.path.getsize(output_path)
# Write to database # Write to database