Skip to content

Commit

Permalink
[Fixes #10138] Render pdf thumbnails from top margin (#10139) (#10141)
Browse files Browse the repository at this point in the history
* render pdf thumbnails from top margin

* fix flake

Co-authored-by: Giovanni Allegri <giohappy@gmail.com>
  • Loading branch information
github-actions[bot] and giohappy committed Oct 13, 2022
1 parent 57b84fc commit 3fc43a7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ def has_thumbnail(self):

# Note - you should probably broadcast layer#post_save() events to ensure
# that indexing (or other listeners) are notified
def save_thumbnail(self, filename, image):
def save_thumbnail(self, filename, image, **kwargs):
upload_path = get_unique_upload_path(filename)
# force convertion to JPEG output file
upload_path = f'{os.path.splitext(upload_path)[0]}.jpg'
Expand All @@ -1745,7 +1745,8 @@ def save_thumbnail(self, filename, image):
# Optimize the Thumbnail size and resolution
_default_thumb_size = settings.THUMBNAIL_SIZE
im = Image.open(storage_manager.open(actual_name))
cover = ImageOps.fit(im, (_default_thumb_size['width'], _default_thumb_size['height'])).convert("RGB")
centering = kwargs.get("centering", (0.5, 0.5))
cover = ImageOps.fit(im, (_default_thumb_size['width'], _default_thumb_size['height']), centering=centering).convert("RGB")

# Saving the thumb into a temporary directory on file system
tmp_location = os.path.abspath(f"{settings.MEDIA_ROOT}/{upload_path}")
Expand Down
12 changes: 10 additions & 2 deletions geonode/documents/management/commands/sync_geonode_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ def add_arguments(self, parser):
action='store_true',
dest="updatethumbnails",
default=False,
help="Update the document thumbnails.")
help="Generate thumbnails for documents. Only documents without a thumbnail will be considered, unless --force is used.")

parser.add_argument(
'--force',
action='store_true',
dest="force",
default=False,
help="Force the update of thumbnails for all documents.")

def handle(self, *args, **options):
updatethumbnails = options.get('updatethumbnails')
force = options.get('force')
for doc in Document.objects.all():
if updatethumbnails:
if doc.thumbnail_url is None or doc.thumbnail_url == '':
if (doc.thumbnail_url is None or doc.thumbnail_url == '') or force:
try:
create_document_thumbnail(doc.id)
except Exception:
Expand Down
13 changes: 12 additions & 1 deletion geonode/documents/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@

class DocumentRenderer():
FILETYPES = ['pdf']
# See https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.fit
CROP_CENTERING = {
'pdf': (0.0, 0.0)
}

def __init__(self) -> None:
pass
Expand All @@ -59,6 +63,9 @@ def render_pdf(self, filename):
logger.warning(f'Cound not generate thumbnail for {filename}: {e}')
return None

def preferred_crop_centering(self, filename):
return self.CROP_CENTERING.get(self._get_filetype(filename))

def _get_filetype(self, filname):
return os.path.splitext(filname)[1][1:]

Expand Down Expand Up @@ -92,6 +99,7 @@ def create_document_thumbnail(self, object_id):

image_file = None
thumbnail_content = None
centering = (0.5, 0.5)

if document.is_image:
dname = storage_manager.path(document.files[0])
Expand All @@ -113,14 +121,17 @@ def create_document_thumbnail(self, object_id):
elif doc_renderer.supports(document.files[0]):
try:
thumbnail_content = doc_renderer.render(document.files[0])
preferred_centering = doc_renderer.preferred_crop_centering(document.files[0])
if preferred_centering is not None:
centering = preferred_centering
except Exception as e:
print(e)
if not thumbnail_content:
logger.warning(f"Thumbnail for document #{object_id} empty.")
ResourceBase.objects.filter(id=document.id).update(thumbnail_url=None)
else:
filename = f'document-{document.uuid}-thumb.jpg'
document.save_thumbnail(filename, thumbnail_content)
document.save_thumbnail(filename, thumbnail_content, centering=centering)
logger.debug(f"Thumbnail for document #{object_id} created.")


Expand Down

0 comments on commit 3fc43a7

Please sign in to comment.