From f7e554a3c1eb76c432290d9e57759eb150dcafff Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Sat, 28 Nov 2020 11:24:54 +0100 Subject: [PATCH] Adjusted the exporter and importer so that they take archived documents into account. --- .../management/commands/document_exporter.py | 19 +++++++++-- .../management/commands/document_importer.py | 32 +++++++++++++++---- src/documents/settings.py | 1 + 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py index 971481ff8..f1ee74038 100644 --- a/src/documents/management/commands/document_exporter.py +++ b/src/documents/management/commands/document_exporter.py @@ -7,7 +7,8 @@ from django.core import serializers from django.core.management.base import BaseCommand, CommandError from documents.models import Document, Correspondent, Tag, DocumentType -from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME +from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \ + EXPORTER_ARCHIVE_NAME from paperless.db import GnuPG from ...mixins import Renderable @@ -54,7 +55,6 @@ class Command(Renderable, BaseCommand): document = document_map[document_dict["pk"]] unique_filename = f"{document.pk:07}_{document.file_name}" - file_target = os.path.join(self.target, unique_filename) thumbnail_name = unique_filename + "-thumbnail.png" @@ -63,6 +63,14 @@ class Command(Renderable, BaseCommand): document_dict[EXPORTER_FILE_NAME] = unique_filename document_dict[EXPORTER_THUMBNAIL_NAME] = thumbnail_name + if os.path.exists(document.archive_path): + archive_name = \ + f"{document.pk:07}_archive_{document.archive_file_name}" + archive_target = os.path.join(self.target, archive_name) + document_dict[EXPORTER_ARCHIVE_NAME] = archive_name + else: + archive_target = None + print(f"Exporting: {file_target}") t = int(time.mktime(document.created.timetuple())) @@ -76,11 +84,18 @@ class Command(Renderable, BaseCommand): f.write(GnuPG.decrypted(document.thumbnail_file)) os.utime(thumbnail_target, times=(t, t)) + if archive_target: + with open(archive_target, "wb") as f: + f.write(GnuPG.decrypted(document.archive_path)) + os.utime(archive_target, times=(t, t)) else: shutil.copy(document.source_path, file_target) shutil.copy(document.thumbnail_path, thumbnail_target) + if archive_target: + shutil.copy(document.archive_path, archive_target) + manifest += json.loads( serializers.serialize("json", Correspondent.objects.all())) diff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.py index 5f50f08f6..ca8c8bf06 100644 --- a/src/documents/management/commands/document_importer.py +++ b/src/documents/management/commands/document_importer.py @@ -7,8 +7,8 @@ from django.core.management import call_command from django.core.management.base import BaseCommand, CommandError from documents.models import Document -from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME -from paperless.db import GnuPG +from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \ + EXPORTER_ARCHIVE_NAME from ...file_handling import generate_filename, create_source_path_directory from ...mixins import Renderable @@ -79,23 +79,41 @@ class Command(Renderable, BaseCommand): 'appear to be in the source directory.'.format(doc_file) ) + if EXPORTER_ARCHIVE_NAME in record: + archive_file = record[EXPORTER_ARCHIVE_NAME] + if not os.path.exists(os.path.join(self.source, archive_file)): + raise CommandError( + f"The manifest file refers to {archive_file} which " + f"does not appear to be in the source directory." + ) + def _import_files_from_manifest(self): - storage_type = Document.STORAGE_TYPE_UNENCRYPTED + os.makedirs(settings.ORIGINALS_DIR, exist_ok=True) + os.makedirs(settings.THUMBNAIL_DIR, exist_ok=True) + os.makedirs(settings.ARCHIVE_DIR, exist_ok=True) for record in self.manifest: if not record["model"] == "documents.document": continue - doc_file = record[EXPORTER_FILE_NAME] - thumb_file = record[EXPORTER_THUMBNAIL_NAME] document = Document.objects.get(pk=record["pk"]) + doc_file = record[EXPORTER_FILE_NAME] document_path = os.path.join(self.source, doc_file) + + thumb_file = record[EXPORTER_THUMBNAIL_NAME] thumbnail_path = os.path.join(self.source, thumb_file) - document.storage_type = storage_type + if EXPORTER_ARCHIVE_NAME in record: + archive_file = record[EXPORTER_ARCHIVE_NAME] + archive_path = os.path.join(self.source, archive_file) + else: + archive_path = None + + document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED + document.filename = generate_filename(document) if os.path.isfile(document.source_path): @@ -106,5 +124,7 @@ class Command(Renderable, BaseCommand): print(f"Moving {document_path} to {document.source_path}") shutil.copy(document_path, document.source_path) shutil.copy(thumbnail_path, document.thumbnail_path) + if archive_path: + shutil.copy(archive_path, document.archive_path) document.save() diff --git a/src/documents/settings.py b/src/documents/settings.py index 20b0b2023..c591d397d 100644 --- a/src/documents/settings.py +++ b/src/documents/settings.py @@ -2,3 +2,4 @@ # for exporting/importing commands EXPORTER_FILE_NAME = "__exported_file_name__" EXPORTER_THUMBNAIL_NAME = "__exported_thumbnail_name__" +EXPORTER_ARCHIVE_NAME = "__exported_archive_name__"