From 6c9d1559f1f2b16df03c48eb06bf139d80389773 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Wed, 21 Nov 2018 20:06:02 -0300 Subject: [PATCH] images: clean temporary files on gif processing --- litecord/images.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/litecord/images.py b/litecord/images.py index f5bc897..5d6cece 100644 --- a/litecord/images.py +++ b/litecord/images.py @@ -168,6 +168,13 @@ def _invalid(kwargs: dict): return Icon(None, None, '') +def _try_unlink(path: str): + try: + os.remove(path) + except FileNotFoundError: + pass + + class IconManager: """Main icon manager.""" def __init__(self, app): @@ -249,7 +256,9 @@ class IconManager: image.size, target) # insert image info on input_handler + # close it to make it ready for consumption by gifsicle input_handler.write(raw_data) + input_handler.close() # call gifsicle under subprocess log.debug('input: {}', input_path) @@ -270,6 +279,13 @@ class IconManager: output_handler = open(output_path, 'rb') data_fd.write(output_handler.read()) + # close unused handlers + output_handler.close() + + # delete the files we created with mkstemp + _try_unlink(input_path) + _try_unlink(output_path) + # reseek, save to raw_data, reseek again. # TODO: remove raw_data altogether as its inefficient # to have two representations of the same bytes @@ -277,6 +293,7 @@ class IconManager: raw_data = data_fd.read() data_fd.seek(0) + return data_fd, raw_data