Make sure that the temporary image file is removed after uploading an image

This commit is contained in:
grossmj 2022-04-27 16:00:02 +07:00
parent cd096ac6e6
commit 89c67790ae

View File

@ -335,13 +335,23 @@ async def write_image(
if duplicate_image and os.path.dirname(duplicate_image.path) == os.path.dirname(image_path):
raise InvalidImageError(f"Image {duplicate_image.filename} with "
f"same checksum already exists in the same directory")
except InvalidImageError:
os.remove(tmp_path)
raise
os.chmod(tmp_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
if not image_dir:
directory = default_images_directory(image_type)
os.makedirs(directory, exist_ok=True)
image_path = os.path.abspath(os.path.join(directory, image_filename))
shutil.move(tmp_path, image_path)
return await images_repo.add_image(image_name, image_type, image_size, image_path, checksum, checksum_algorithm="md5")
if not image_dir:
directory = default_images_directory(image_type)
os.makedirs(directory, exist_ok=True)
image_path = os.path.abspath(os.path.join(directory, image_filename))
shutil.move(tmp_path, image_path)
os.chmod(image_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
finally:
try:
os.remove(tmp_path)
except OSError:
log.warning(f"Could not remove '{tmp_path}'")
return await images_repo.add_image(
image_name,
image_type,
image_size,
image_path,
checksum,
checksum_algorithm="md5"
)