mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Update import & export to handle encryption toggle
This commit is contained in:
		@@ -1,8 +1,8 @@
 | 
			
		||||
import json
 | 
			
		||||
import os
 | 
			
		||||
import time
 | 
			
		||||
import shutil
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
from django.core import serializers
 | 
			
		||||
 | 
			
		||||
@@ -45,9 +45,6 @@ class Command(Renderable, BaseCommand):
 | 
			
		||||
        if not os.access(self.target, os.W_OK):
 | 
			
		||||
            raise CommandError("That path doesn't appear to be writable")
 | 
			
		||||
 | 
			
		||||
        if not settings.PASSPHRASE:
 | 
			
		||||
            settings.PASSPHRASE = input("Please enter the passphrase: ")
 | 
			
		||||
 | 
			
		||||
        if options["legacy"]:
 | 
			
		||||
            self.dump_legacy()
 | 
			
		||||
        else:
 | 
			
		||||
@@ -73,13 +70,20 @@ class Command(Renderable, BaseCommand):
 | 
			
		||||
            print("Exporting: {}".format(file_target))
 | 
			
		||||
 | 
			
		||||
            t = int(time.mktime(document.created.timetuple()))
 | 
			
		||||
            with open(file_target, "wb") as f:
 | 
			
		||||
                f.write(GnuPG.decrypted(document.source_file))
 | 
			
		||||
                os.utime(file_target, times=(t, t))
 | 
			
		||||
            if document.storage_type == Document.STORAGE_TYPE_GPG:
 | 
			
		||||
 | 
			
		||||
            with open(thumbnail_target, "wb") as f:
 | 
			
		||||
                f.write(GnuPG.decrypted(document.thumbnail_file))
 | 
			
		||||
                os.utime(thumbnail_target, times=(t, t))
 | 
			
		||||
                with open(file_target, "wb") as f:
 | 
			
		||||
                    f.write(GnuPG.decrypted(document.source_file))
 | 
			
		||||
                    os.utime(file_target, times=(t, t))
 | 
			
		||||
 | 
			
		||||
                with open(thumbnail_target, "wb") as f:
 | 
			
		||||
                    f.write(GnuPG.decrypted(document.thumbnail_file))
 | 
			
		||||
                    os.utime(thumbnail_target, times=(t, t))
 | 
			
		||||
 | 
			
		||||
            else:
 | 
			
		||||
 | 
			
		||||
                shutil.copy(document.source_path, file_target)
 | 
			
		||||
                shutil.copy(document.thumbnail_path, thumbnail_target)
 | 
			
		||||
 | 
			
		||||
        manifest += json.loads(
 | 
			
		||||
            serializers.serialize("json", Correspondent.objects.all()))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
import json
 | 
			
		||||
import os
 | 
			
		||||
import shutil
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
@@ -46,12 +47,6 @@ class Command(Renderable, BaseCommand):
 | 
			
		||||
 | 
			
		||||
        self._check_manifest()
 | 
			
		||||
 | 
			
		||||
        if not settings.PASSPHRASE:
 | 
			
		||||
            raise CommandError(
 | 
			
		||||
                "You need to define a passphrase before continuing.  Please "
 | 
			
		||||
                "consult the documentation for setting up Paperless."
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        # Fill up the database with whatever is in the manifest
 | 
			
		||||
        call_command("loaddata", manifest_path)
 | 
			
		||||
 | 
			
		||||
@@ -99,14 +94,21 @@ class Command(Renderable, BaseCommand):
 | 
			
		||||
            document_path = os.path.join(self.source, doc_file)
 | 
			
		||||
            thumbnail_path = os.path.join(self.source, thumb_file)
 | 
			
		||||
 | 
			
		||||
            with open(document_path, "rb") as unencrypted:
 | 
			
		||||
                with open(document.source_path, "wb") as encrypted:
 | 
			
		||||
                    print("Encrypting {} and saving it to {}".format(
 | 
			
		||||
                        doc_file, document.source_path))
 | 
			
		||||
                    encrypted.write(GnuPG.encrypted(unencrypted))
 | 
			
		||||
            if document.storage_type == Document.STORAGE_TYPE_GPG:
 | 
			
		||||
 | 
			
		||||
            with open(thumbnail_path, "rb") as unencrypted:
 | 
			
		||||
                with open(document.thumbnail_path, "wb") as encrypted:
 | 
			
		||||
                    print("Encrypting {} and saving it to {}".format(
 | 
			
		||||
                        thumb_file, document.thumbnail_path))
 | 
			
		||||
                    encrypted.write(GnuPG.encrypted(unencrypted))
 | 
			
		||||
                with open(document_path, "rb") as unencrypted:
 | 
			
		||||
                    with open(document.source_path, "wb") as encrypted:
 | 
			
		||||
                        print("Encrypting {} and saving it to {}".format(
 | 
			
		||||
                            doc_file, document.source_path))
 | 
			
		||||
                        encrypted.write(GnuPG.encrypted(unencrypted))
 | 
			
		||||
 | 
			
		||||
                with open(thumbnail_path, "rb") as unencrypted:
 | 
			
		||||
                    with open(document.thumbnail_path, "wb") as encrypted:
 | 
			
		||||
                        print("Encrypting {} and saving it to {}".format(
 | 
			
		||||
                            thumb_file, document.thumbnail_path))
 | 
			
		||||
                        encrypted.write(GnuPG.encrypted(unencrypted))
 | 
			
		||||
 | 
			
		||||
            else:
 | 
			
		||||
 | 
			
		||||
                shutil.copy(document_path, document.source_path)
 | 
			
		||||
                shutil.copy(thumbnail_path, document.thumbnail_path)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user