1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-01 04:38:12 +00:00

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): if duplicate_image and os.path.dirname(duplicate_image.path) == os.path.dirname(image_path):
raise InvalidImageError(f"Image {duplicate_image.filename} with " raise InvalidImageError(f"Image {duplicate_image.filename} with "
f"same checksum already exists in the same directory") f"same checksum already exists in the same directory")
except InvalidImageError: if not image_dir:
os.remove(tmp_path) directory = default_images_directory(image_type)
raise os.makedirs(directory, exist_ok=True)
os.chmod(tmp_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC) image_path = os.path.abspath(os.path.join(directory, image_filename))
if not image_dir: shutil.move(tmp_path, image_path)
directory = default_images_directory(image_type) os.chmod(image_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
os.makedirs(directory, exist_ok=True) finally:
image_path = os.path.abspath(os.path.join(directory, image_filename)) try:
shutil.move(tmp_path, image_path) os.remove(tmp_path)
return await images_repo.add_image(image_name, image_type, image_size, image_path, checksum, checksum_algorithm="md5") 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"
)