diff --git a/src/documents/migrations/1003_mime_types.py b/src/documents/migrations/1003_mime_types.py index 1038d57b3..e5e613735 100644 --- a/src/documents/migrations/1003_mime_types.py +++ b/src/documents/migrations/1003_mime_types.py @@ -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")