diff --git a/gns3server/api/routes/controller/images.py b/gns3server/api/routes/controller/images.py index c4a06a4f..736c44c3 100644 --- a/gns3server/api/routes/controller/images.py +++ b/gns3server/api/routes/controller/images.py @@ -148,11 +148,6 @@ async def upload_image( if os.path.commonprefix([base_images_directory, full_path]) != base_images_directory: raise ControllerForbiddenError(f"Cannot write image, '{image_path}' is forbidden") - image = await images_repo.get_image(image_path) - if image: - log.warning(f"Image '{image_path}' already exists") - return image - try: allow_raw_image = Config.instance().settings.Server.allow_raw_images image = await write_image(image_path, full_path, request.stream(), images_repo, allow_raw_image=allow_raw_image) diff --git a/gns3server/utils/images.py b/gns3server/utils/images.py index 2638cfe7..794abf6a 100644 --- a/gns3server/utils/images.py +++ b/gns3server/utils/images.py @@ -342,6 +342,12 @@ async def write_image( allow_raw_image=False ) -> models.Image: + db_image = await images_repo.get_image(image_path) + if db_image and os.path.exists(image_path): + # the image already exists in the database and on disk + log.info(f"Image {image_path} already exists") + return db_image + image_dir, image_name = os.path.split(image_filename) log.info(f"Writing image file to '{image_path}'") # Store the file under its final name only when the upload is completed @@ -381,6 +387,10 @@ async def write_image( except OSError: log.warning(f"Could not remove '{tmp_path}'") + if db_image: + # the image already exists in the database, no need to add it again + return db_image + return await images_repo.add_image( image_name, image_type,