From 2a4fe4dceb35db00a48f8cbc11171e026f269678 Mon Sep 17 00:00:00 2001 From: Jonas Winkler Date: Wed, 25 Nov 2020 21:10:50 +0100 Subject: [PATCH] fixed the decryption code, but its still untested. --- .../management/commands/decrypt_documents.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/documents/management/commands/decrypt_documents.py b/src/documents/management/commands/decrypt_documents.py index e4d607fa8..f9b4edcdc 100644 --- a/src/documents/management/commands/decrypt_documents.py +++ b/src/documents/management/commands/decrypt_documents.py @@ -61,18 +61,28 @@ class Command(BaseCommand): document).encode('utf-8'), "green")) old_paths = [document.source_path, document.thumbnail_path] + raw_document = GnuPG.decrypted(document.source_file, passphrase) raw_thumb = GnuPG.decrypted(document.thumbnail_file, passphrase) document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED + ext = os.path.splitext(document.filename)[1] + + if not ext == '.gpg': + raise CommandError( + f"Abort: encrypted file {document.source_path} does not " + f"end with .gpg") + + document.filename = os.path.splitext(document.source_path)[0] + with open(document.source_path, "wb") as f: f.write(raw_document) with open(document.thumbnail_path, "wb") as f: f.write(raw_thumb) - document.save(update_fields=("storage_type",)) + document.save(update_fields=("storage_type", "filename")) for path in old_paths: os.unlink(path)