Adjusted the exporter and importer so that they take archived documents into account.

This commit is contained in:
jonaswinkler 2020-11-28 11:24:54 +01:00
parent 5b020bb8d2
commit f7e554a3c1
3 changed files with 44 additions and 8 deletions

View File

@ -7,7 +7,8 @@ from django.core import serializers
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from documents.models import Document, Correspondent, Tag, DocumentType 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 paperless.db import GnuPG
from ...mixins import Renderable from ...mixins import Renderable
@ -54,7 +55,6 @@ class Command(Renderable, BaseCommand):
document = document_map[document_dict["pk"]] document = document_map[document_dict["pk"]]
unique_filename = f"{document.pk:07}_{document.file_name}" unique_filename = f"{document.pk:07}_{document.file_name}"
file_target = os.path.join(self.target, unique_filename) file_target = os.path.join(self.target, unique_filename)
thumbnail_name = unique_filename + "-thumbnail.png" thumbnail_name = unique_filename + "-thumbnail.png"
@ -63,6 +63,14 @@ class Command(Renderable, BaseCommand):
document_dict[EXPORTER_FILE_NAME] = unique_filename document_dict[EXPORTER_FILE_NAME] = unique_filename
document_dict[EXPORTER_THUMBNAIL_NAME] = thumbnail_name 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}") print(f"Exporting: {file_target}")
t = int(time.mktime(document.created.timetuple())) t = int(time.mktime(document.created.timetuple()))
@ -76,11 +84,18 @@ class Command(Renderable, BaseCommand):
f.write(GnuPG.decrypted(document.thumbnail_file)) f.write(GnuPG.decrypted(document.thumbnail_file))
os.utime(thumbnail_target, times=(t, t)) 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: else:
shutil.copy(document.source_path, file_target) shutil.copy(document.source_path, file_target)
shutil.copy(document.thumbnail_path, thumbnail_target) shutil.copy(document.thumbnail_path, thumbnail_target)
if archive_target:
shutil.copy(document.archive_path, archive_target)
manifest += json.loads( manifest += json.loads(
serializers.serialize("json", Correspondent.objects.all())) serializers.serialize("json", Correspondent.objects.all()))

View File

@ -7,8 +7,8 @@ from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from documents.models import Document from documents.models import Document
from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \
from paperless.db import GnuPG EXPORTER_ARCHIVE_NAME
from ...file_handling import generate_filename, create_source_path_directory from ...file_handling import generate_filename, create_source_path_directory
from ...mixins import Renderable from ...mixins import Renderable
@ -79,23 +79,41 @@ class Command(Renderable, BaseCommand):
'appear to be in the source directory.'.format(doc_file) '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): 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: for record in self.manifest:
if not record["model"] == "documents.document": if not record["model"] == "documents.document":
continue continue
doc_file = record[EXPORTER_FILE_NAME]
thumb_file = record[EXPORTER_THUMBNAIL_NAME]
document = Document.objects.get(pk=record["pk"]) document = Document.objects.get(pk=record["pk"])
doc_file = record[EXPORTER_FILE_NAME]
document_path = os.path.join(self.source, doc_file) document_path = os.path.join(self.source, doc_file)
thumb_file = record[EXPORTER_THUMBNAIL_NAME]
thumbnail_path = os.path.join(self.source, thumb_file) 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) document.filename = generate_filename(document)
if os.path.isfile(document.source_path): if os.path.isfile(document.source_path):
@ -106,5 +124,7 @@ class Command(Renderable, BaseCommand):
print(f"Moving {document_path} to {document.source_path}") print(f"Moving {document_path} to {document.source_path}")
shutil.copy(document_path, document.source_path) shutil.copy(document_path, document.source_path)
shutil.copy(thumbnail_path, document.thumbnail_path) shutil.copy(thumbnail_path, document.thumbnail_path)
if archive_path:
shutil.copy(archive_path, document.archive_path)
document.save() document.save()

View File

@ -2,3 +2,4 @@
# for exporting/importing commands # for exporting/importing commands
EXPORTER_FILE_NAME = "__exported_file_name__" EXPORTER_FILE_NAME = "__exported_file_name__"
EXPORTER_THUMBNAIL_NAME = "__exported_thumbnail_name__" EXPORTER_THUMBNAIL_NAME = "__exported_thumbnail_name__"
EXPORTER_ARCHIVE_NAME = "__exported_archive_name__"