diff --git a/geonode/documents/migrations/0036_clean_document_thumbnails.py b/geonode/documents/migrations/0036_clean_document_thumbnails.py new file mode 100644 index 00000000000..edfd6847a66 --- /dev/null +++ b/geonode/documents/migrations/0036_clean_document_thumbnails.py @@ -0,0 +1,33 @@ +import logging + +from django.db import migrations + +from ..models import Document + +logger = logging.getLogger(__name__) + + +def set_null_to_non_image_docs_thumbnails(apps, _): + "Sets thumbnail_url to null for documents which are not images" + try: + # update thumbnail urls + for document in Document.objects.all(): + if not document.is_image: + document.thumbnail_url=None + document.save() + # Remove thumbnail links + link_model = apps.get_model('base', 'Link') + link_model.objects.filter(resource__thumbnail_url__isnull=True, name='Thumbnail').delete() + except Exception as e: + logger.exception(e) + + +class Migration(migrations.Migration): + + dependencies = [ + ('documents', '0033_remove_document_doc_type'), + ] + + operations = [ + migrations.RunPython(set_null_to_non_image_docs_thumbnails, migrations.RunPython.noop), + ] diff --git a/geonode/documents/models.py b/geonode/documents/models.py index b9c20121f38..938d736efd8 100644 --- a/geonode/documents/models.py +++ b/geonode/documents/models.py @@ -23,7 +23,6 @@ from django.conf import settings from django.db import models from django.urls import reverse -from django.contrib.staticfiles import finders from django.utils.functional import classproperty from django.utils.translation import ugettext_lazy as _ from django.contrib.contenttypes.models import ContentType @@ -101,18 +100,6 @@ def name_long(self): else: return f'{self.title} ({self.id})' - def find_placeholder(self): - placeholder = 'documents/{0}-placeholder.png' - if finders.find(placeholder.format(self.extension), False): - return finders.find(placeholder.format(self.extension), False) - elif self.is_audio: - return finders.find(placeholder.format('audio'), False) - elif self.is_image: - return finders.find(placeholder.format('image'), False) - elif self.is_video: - return finders.find(placeholder.format('video'), False) - return finders.find(placeholder.format('generic'), False) - @property def href(self): if self.doc_url: diff --git a/geonode/documents/tasks.py b/geonode/documents/tasks.py index e91a6ca30b1..1cd21e6bb74 100644 --- a/geonode/documents/tasks.py +++ b/geonode/documents/tasks.py @@ -16,18 +16,14 @@ # along with this program. If not, see . # ######################################################################### -import os - from celery.utils.log import get_task_logger from geonode.celery_app import app from geonode.storage.manager import storage_manager +from ..base.models import ResourceBase from .models import Document -from .renderers import ( - render_document, - generate_thumbnail_content, - ConversionError) +from .renderers import (generate_thumbnail_content) logger = get_task_logger(__name__) @@ -56,60 +52,29 @@ def create_document_thumbnail(self, object_id): logger.error(f"Document #{object_id} does not exist.") raise - image_path = None image_file = None + thumbnail_content = None if document.is_image: dname = storage_manager.path(document.files[0]) if storage_manager.exists(dname): image_file = storage_manager.open(dname, 'rb') - elif document.is_video or document.is_audio: - image_file = open(document.find_placeholder(), 'rb') - elif document.is_file: - dname = storage_manager.path(document.files[0]) - try: - document_location = storage_manager.path(dname) - except NotImplementedError as e: - logger.debug(e) - - document_location = storage_manager.url(dname) - try: - image_path = render_document(document_location) - if image_path is not None: - try: - image_file = open(image_path, 'rb') - except Exception as e: - logger.debug(f"Failed to render document #{object_id}: {e}") - else: - logger.debug(f"Failed to render document #{object_id}") - except ConversionError as e: - logger.debug(f"Could not convert document #{object_id}: {e}.") - except NotImplementedError as e: - logger.debug(f"Failed to render document #{object_id}: {e}") - - thumbnail_content = None - try: try: thumbnail_content = generate_thumbnail_content(image_file) except Exception as e: - logger.debug(f"Could not generate thumbnail, falling back to 'placeholder': {e}") - thumbnail_content = generate_thumbnail_content(document.find_placeholder()) - except Exception as e: - logger.error(f"Could not generate thumbnail: {e}") - return - finally: - if image_file is not None: - image_file.close() - - if image_path is not None: - os.remove(image_path) + logger.debug(f"Could not generate thumbnail, setting thumbnail_url to None: {e}") + finally: + if image_file is not None: + image_file.close() if not thumbnail_content: logger.warning(f"Thumbnail for document #{object_id} empty.") - filename = f'document-{document.uuid}-thumb.png' - document.save_thumbnail(filename, thumbnail_content) - logger.debug(f"Thumbnail for document #{object_id} created.") + ResourceBase.objects.filter(id=document.id).update(thumbnail_url=None) + else: + filename = f'document-{document.uuid}-thumb.png' + document.save_thumbnail(filename, thumbnail_content) + logger.debug(f"Thumbnail for document #{object_id} created.") @app.task(