From 57356d37b254b2162879e25f6ff27891210c475f Mon Sep 17 00:00:00 2001 From: Diogo Ferreira <57763474+neves-nvs@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:36:29 +0100 Subject: [PATCH 1/3] Add move file exception --- sendsafely/exceptions.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sendsafely/exceptions.py b/sendsafely/exceptions.py index 2920a63..3600eec 100644 --- a/sendsafely/exceptions.py +++ b/sendsafely/exceptions.py @@ -44,6 +44,17 @@ def __init__(self, details=None): display = self.message super().__init__(display) +class MoveFileException(Exception): + """Exception raised during file movement. + """ + def __init__(self, details=None): + self.message = "An error occurred during file movement." + self.details = details + display = self.details + if display is None: + display = self.message + super().__init__(display) + class FinalizePackageFailedException(Exception): """Exception raised while finalizing package. From 2bee5b38f95b0b1b362e8063e7bf0ade684b548e Mon Sep 17 00:00:00 2001 From: Diogo Ferreira <57763474+neves-nvs@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:39:30 +0100 Subject: [PATCH 2/3] Add move_file_to_directory method to Package --- sendsafely/Package.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/sendsafely/Package.py b/sendsafely/Package.py index 26a75b2..606d207 100644 --- a/sendsafely/Package.py +++ b/sendsafely/Package.py @@ -13,7 +13,7 @@ import warnings from sendsafely.exceptions import CreatePackageFailedException, FinalizePackageFailedException, DownloadFileException, \ - UploadFileException, DeletePackageException, KeycodeRequiredException, GetPackageInformationFailedException, \ + UploadFileException, MoveFileException, DeletePackageException, KeycodeRequiredException, GetPackageInformationFailedException, \ UploadKeycodeException, AddRecipientFailedException, UpdateRecipientFailedException, UploadMessageException, \ GetPublicKeysFailedException, GetFileInformationException, DeleteFileException, GetPackageMessageException, \ AddFileFailedException @@ -276,6 +276,22 @@ def _add_file(self, filename, filesize, parts=1, directory_id=None): raise AddFileFailedException(details=response["message"]) return response + def move_file_to_directory(self, file_id, directory_id): + """ + Moves the file with the specified id to the directory with the specified ID + """ + endpoint = "/package/" + self.package_id + "/directory/" + directory_id + "/file/" + file_id + url = self.sendsafely.BASE_URL + endpoint + headers = make_headers(self.sendsafely.API_SECRET, self.sendsafely.API_KEY, endpoint) + try: + response = requests.post(url=url, headers=headers).json() + except Exception as e: + raise MoveFileException(details=e) + + if response["response"] != "SUCCESS": + raise MoveFileException(details=response["message"]) + return response + def delete_file_from_package(self, file_id): """ Deletes the file with the specified id from the package with the specified ID From 0735e537d1b7eb8c27f75bf71173111faa415aee Mon Sep 17 00:00:00 2001 From: Diogo Ferreira <57763474+neves-nvs@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:33:41 +0100 Subject: [PATCH 3/3] Refactor move_file operation as method os SendSafely class --- sendsafely/Package.py | 18 +----------------- sendsafely/SendSafely.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/sendsafely/Package.py b/sendsafely/Package.py index 606d207..26a75b2 100644 --- a/sendsafely/Package.py +++ b/sendsafely/Package.py @@ -13,7 +13,7 @@ import warnings from sendsafely.exceptions import CreatePackageFailedException, FinalizePackageFailedException, DownloadFileException, \ - UploadFileException, MoveFileException, DeletePackageException, KeycodeRequiredException, GetPackageInformationFailedException, \ + UploadFileException, DeletePackageException, KeycodeRequiredException, GetPackageInformationFailedException, \ UploadKeycodeException, AddRecipientFailedException, UpdateRecipientFailedException, UploadMessageException, \ GetPublicKeysFailedException, GetFileInformationException, DeleteFileException, GetPackageMessageException, \ AddFileFailedException @@ -276,22 +276,6 @@ def _add_file(self, filename, filesize, parts=1, directory_id=None): raise AddFileFailedException(details=response["message"]) return response - def move_file_to_directory(self, file_id, directory_id): - """ - Moves the file with the specified id to the directory with the specified ID - """ - endpoint = "/package/" + self.package_id + "/directory/" + directory_id + "/file/" + file_id - url = self.sendsafely.BASE_URL + endpoint - headers = make_headers(self.sendsafely.API_SECRET, self.sendsafely.API_KEY, endpoint) - try: - response = requests.post(url=url, headers=headers).json() - except Exception as e: - raise MoveFileException(details=e) - - if response["response"] != "SUCCESS": - raise MoveFileException(details=response["message"]) - return response - def delete_file_from_package(self, file_id): """ Deletes the file with the specified id from the package with the specified ID diff --git a/sendsafely/SendSafely.py b/sendsafely/SendSafely.py index 5a1e3c1..49e941e 100644 --- a/sendsafely/SendSafely.py +++ b/sendsafely/SendSafely.py @@ -14,7 +14,7 @@ from pgpy.constants import KeyFlags, HashAlgorithm, SymmetricKeyAlgorithm, CompressionAlgorithm, PubKeyAlgorithm from sendsafely.Package import Package from sendsafely.exceptions import GetPackagesException, GetUserInformationException, TrustedDeviceException, \ - DeletePackageException, GetPackageInformationFailedException, GetKeycodeFailedException + DeletePackageException, GetPackageInformationFailedException, GetKeycodeFailedException, MoveFileException from sendsafely.utilities import make_headers, _get_string_from_file @@ -268,3 +268,19 @@ def get_sent_packages(self, row_index=0, page_size=100): row_index += page_size except Exception as e: GetPackagesException(details=str(e)) + + def move_file(self, package_id, file_id, destination_directory_id): + """ + Moves the file with the specified id to the directory with the specified ID + """ + endpoint = "/package/" + package_id + "/directory/" + destination_directory_id + "/file/" + file_id + url = self.BASE_URL + endpoint + headers = make_headers(self.API_SECRET, self.API_KEY, endpoint) + try: + response = requests.post(url=url, headers=headers).json() + except Exception as e: + raise MoveFileException(details=e) + + if response["response"] != "SUCCESS": + raise MoveFileException(details=response["message"]) + return response