From c8cd066d25806e7c4080b419c632d65d07172775 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sat, 6 Jun 2020 23:06:34 +0200 Subject: [PATCH] try encrypt again if fail --- app/pgp_utils.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/pgp_utils.py b/app/pgp_utils.py index dd5fd4b4..638a0bfc 100644 --- a/app/pgp_utils.py +++ b/app/pgp_utils.py @@ -26,12 +26,15 @@ def load_public_key(public_key: str) -> str: def encrypt_file(data: BytesIO, fingerprint: str) -> str: r = gpg.encrypt_file(data, fingerprint, always_trust=True) if not r.ok: - # save the content for debugging - random_file_name = random_string(20) + ".eml" - full_path = f"/tmp/{random_file_name}" - with open(full_path, "wb") as f: - f.write(data.getbuffer()) - LOG.error("Log to %s", full_path) - raise PGPException("Cannot encrypt") + LOG.error("Try encrypt again %s", fingerprint) + r = gpg.encrypt_file(data, fingerprint, always_trust=True) + if not r.ok: + # save the content for debugging + random_file_name = random_string(20) + ".eml" + full_path = f"/tmp/{random_file_name}" + with open(full_path, "wb") as f: + f.write(data.getbuffer()) + LOG.error("Log to %s", full_path) + raise PGPException("Cannot encrypt") return str(r)