From 6c7ca34213fb79a7349bd00afda780fd39498f83 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 18:46:20 +0200 Subject: [PATCH] [Fixes #10138] Render pdf thumbnails from top margin (#10139) (#10141) * render pdf thumbnails from top margin * fix flake Co-authored-by: Giovanni Allegri --- geonode/base/models.py | 5 +++-- .../management/commands/sync_geonode_documents.py | 12 ++++++++++-- geonode/documents/tasks.py | 13 ++++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/geonode/base/models.py b/geonode/base/models.py index f991d0fcb67..99207f07269 100644 --- a/geonode/base/models.py +++ b/geonode/base/models.py @@ -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' @@ -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}") diff --git a/geonode/documents/management/commands/sync_geonode_documents.py b/geonode/documents/management/commands/sync_geonode_documents.py index 58a8e548a61..1d8a1becddd 100644 --- a/geonode/documents/management/commands/sync_geonode_documents.py +++ b/geonode/documents/management/commands/sync_geonode_documents.py @@ -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: diff --git a/geonode/documents/tasks.py b/geonode/documents/tasks.py index 99e125e5649..76a3c2c2126 100644 --- a/geonode/documents/tasks.py +++ b/geonode/documents/tasks.py @@ -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 @@ -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:] @@ -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]) @@ -113,6 +121,9 @@ 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: @@ -120,7 +131,7 @@ def create_document_thumbnail(self, object_id): 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.")