2016-01-14 19:47:57 +00:00
|
|
|
import os
|
2016-01-23 02:57:29 +00:00
|
|
|
import time
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
|
|
|
|
|
|
from documents.models import Document
|
|
|
|
|
from paperless.db import GnuPG
|
|
|
|
|
|
2016-02-11 22:05:38 +00:00
|
|
|
from ...mixins import Renderable
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-02-11 22:05:38 +00:00
|
|
|
|
|
|
|
|
class Command(Renderable, BaseCommand):
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
help = """
|
|
|
|
|
Decrypt and rename all files in our collection into a given target
|
|
|
|
|
directory. Note that we don't export any of the parsed data since
|
|
|
|
|
that can always be re-collected via the consumer.
|
|
|
|
|
""".replace(" ", "")
|
|
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
|
parser.add_argument("target")
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self.verbosity = 0
|
|
|
|
|
self.target = None
|
|
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
|
|
self.verbosity = options["verbosity"]
|
|
|
|
|
self.target = options["target"]
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(self.target):
|
|
|
|
|
raise CommandError("That path doesn't exist")
|
|
|
|
|
|
|
|
|
|
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: ")
|
|
|
|
|
|
|
|
|
|
for document in Document.objects.all():
|
|
|
|
|
|
2016-02-15 22:38:18 +00:00
|
|
|
target = os.path.join(self.target, document.file_name)
|
2016-01-14 19:47:57 +00:00
|
|
|
|
2016-02-14 16:09:52 +00:00
|
|
|
print("Exporting: {}".format(target))
|
2016-01-14 19:47:57 +00:00
|
|
|
|
|
|
|
|
with open(target, "wb") as f:
|
2016-01-30 01:18:52 +00:00
|
|
|
f.write(GnuPG.decrypted(document.source_file))
|
2016-01-23 03:22:15 +00:00
|
|
|
t = int(time.mktime(document.created.timetuple()))
|
|
|
|
|
os.utime(target, times=(t, t))
|