Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 4.0.x] [Fixes #10040] Remove auto-generated thumbnail for documents #10052

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions geonode/documents/migrations/0036_clean_document_thumbnails.py
Original file line number Diff line number Diff line change
@@ -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),
]
13 changes: 0 additions & 13 deletions geonode/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
59 changes: 12 additions & 47 deletions geonode/documents/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
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__)

Expand Down Expand Up @@ -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(
Expand Down