fix up the migration for encrypted documents.

This commit is contained in:
jonaswinkler 2020-12-16 21:08:41 +01:00
parent cf3fa50b55
commit 5c310c51d4

View File

@ -6,13 +6,17 @@ import magic
from django.conf import settings
from django.db import migrations, models
from paperless.db import GnuPG
STORAGE_TYPE_UNENCRYPTED = "unencrypted"
STORAGE_TYPE_GPG = "gpg"
def source_path(self):
if self.filename:
fname = str(self.filename)
else:
fname = "{:07}.{}".format(self.pk, self.file_type)
if self.storage_type == self.STORAGE_TYPE_GPG:
if self.storage_type == STORAGE_TYPE_GPG:
fname += ".gpg"
return os.path.join(
@ -26,9 +30,16 @@ def add_mime_types(apps, schema_editor):
documents = Document.objects.all()
for d in documents:
d.mime_type = magic.from_file(source_path(d), mime=True)
if d.storage_type == STORAGE_TYPE_GPG:
f = GnuPG.decrypted(open(source_path(d), "rb"))
else:
f = open(source_path(d), "rb")
d.mime_type = magic.from_buffer(f.read(1024), mime=True)
d.save()
f.close()
def add_file_extensions(apps, schema_editor):
Document = apps.get_model("documents", "Document")