This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first stages of getting thumbnails back
- Loading branch information
1 parent
5c41e71
commit 52f15b4
Showing
7 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.2 on 2016-03-05 00:40 | ||
from __future__ import unicode_literals | ||
|
||
import gnupg | ||
import os | ||
import re | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
|
||
from django.conf import settings | ||
from django.db import migrations | ||
|
||
|
||
class GnuPG(object): | ||
""" | ||
A handy singleton to use when handling encrypted files. | ||
""" | ||
|
||
gpg = gnupg.GPG(gnupghome=settings.GNUPG_HOME) | ||
|
||
@classmethod | ||
def decrypted(cls, file_handle): | ||
return cls.gpg.decrypt_file( | ||
file_handle, passphrase=settings.PASSPHRASE).data | ||
|
||
@classmethod | ||
def encrypted(cls, file_handle): | ||
return cls.gpg.encrypt_file( | ||
file_handle, | ||
recipients=None, | ||
passphrase=settings.PASSPHRASE, | ||
symmetric=True | ||
).data | ||
|
||
|
||
def move_documents_and_create_thumbnails(apps, schema_editor): | ||
|
||
documents = os.listdir(os.path.join(settings.MEDIA_ROOT, "documents")) | ||
|
||
if not documents: | ||
return | ||
|
||
print("\n") | ||
|
||
for f in sorted(documents): | ||
|
||
if not f.endswith("gpg"): | ||
continue | ||
|
||
print(" * Generating a thumbnail for {}".format(f)) | ||
|
||
thumb_temp = tempfile.mkdtemp( | ||
prefix="paperless", dir=settings.SCRATCH_DIR) | ||
orig_temp = tempfile.mkdtemp( | ||
prefix="paperless", dir=settings.SCRATCH_DIR) | ||
|
||
orig_source = os.path.join(settings.MEDIA_ROOT, "documents", f) | ||
orig_target = os.path.join(orig_temp, f.replace(".gpg", "")) | ||
|
||
with open(orig_source, "rb") as encrypted: | ||
with open(orig_target, "wb") as unencrypted: | ||
unencrypted.write(GnuPG.decrypted(encrypted)) | ||
|
||
subprocess.Popen(( | ||
settings.CONVERT_BINARY, | ||
"-scale", "500x500", | ||
orig_target, | ||
os.path.join(thumb_temp, "convert-%04d.jpg") | ||
)).wait() | ||
|
||
thumb_source = os.path.join(thumb_temp, "convert-0000.jpg") | ||
thumb_target = os.path.join( | ||
settings.MEDIA_ROOT, | ||
"documents", | ||
"thumbnails", | ||
re.sub(r"(\d+)\.\w+(\.gpg)", "\\1.jpg\\2", f) | ||
) | ||
with open(thumb_source, "rb") as unencrypted: | ||
with open(thumb_target, "wb") as encrypted: | ||
encrypted.write(GnuPG.encrypted(unencrypted)) | ||
|
||
shutil.rmtree(thumb_temp) | ||
shutil.rmtree(orig_temp) | ||
|
||
shutil.move( | ||
os.path.join(settings.MEDIA_ROOT, "documents", f), | ||
os.path.join(settings.MEDIA_ROOT, "documents", "originals", f), | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('documents', '0011_auto_20160303_1929'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(move_documents_and_create_thumbnails), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters