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 #10041] Review the thumbnail scaling process #10069

Merged
merged 1 commit into from
Sep 29, 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
3 changes: 1 addition & 2 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,8 +1741,7 @@ def save_thumbnail(self, filename, image):
url = storage_manager.url(upload_path)
try:
# Optimize the Thumbnail size and resolution
_default_thumb_size = getattr(
settings, 'THUMBNAIL_GENERATOR_DEFAULT_SIZE', {'width': 240, 'height': 200})
_default_thumb_size = settings.THUMBNAIL_SIZE
im = Image.open(storage_manager.open(actual_name))
im.thumbnail(
(_default_thumb_size['width'], _default_thumb_size['height']),
Expand Down
118 changes: 0 additions & 118 deletions geonode/documents/renderers.py

This file was deleted.

13 changes: 10 additions & 3 deletions geonode/documents/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import io

from PIL import Image

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 (generate_thumbnail_content)

logger = get_task_logger(__name__)

Expand Down Expand Up @@ -61,9 +64,13 @@ def create_document_thumbnail(self, object_id):
image_file = storage_manager.open(dname, 'rb')

try:
thumbnail_content = generate_thumbnail_content(image_file)
image = Image.open(image_file)
with io.BytesIO() as output:
image.save(output, format='PNG')
thumbnail_content = output.getvalue()
output.close()
except Exception as e:
logger.debug(f"Could not generate thumbnail, setting thumbnail_url to None: {e}")
logger.debug(f"Could not generate thumbnail: {e}")
finally:
if image_file is not None:
image_file.close()
Expand Down
38 changes: 38 additions & 0 deletions geonode/documents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from io import BytesIO

from unittest.mock import patch
from urllib.parse import urlparse

from django.urls import reverse
from django.conf import settings
Expand Down Expand Up @@ -95,6 +96,7 @@ def tearDownClass(cls):
def setUp(self):
super().setUp()
create_models('map')
self.project_root = os.path.abspath(os.path.dirname(__file__))
self.imgfile = io.BytesIO(
b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
Expand Down Expand Up @@ -266,6 +268,42 @@ def test_replace_document(self):
# Remove document
d.delete()

def test_non_image_documents_thumbnail(self):
self.client.login(username='admin', password='admin')
try:
with open(os.path.join(f"{self.project_root}", "tests/data/text.txt"), "rb") as f:
data = {
'title': "Non img File Doc",
'doc_file': f,
'extension': 'txt'
}
self.client.post(reverse('document_upload'), data=data)
d = Document.objects.get(title='Non img File Doc')
self.assertIsNone(d.thumbnail_url)
finally:
Document.objects.filter(title='Non img File Doc').delete()

def test_image_documents_thumbnail(self):
self.client.login(username='admin', password='admin')
try:
with open(os.path.join(f"{self.project_root}", "tests/data/img.gif"), "rb") as f:
data = {
'title': "img File Doc",
'doc_file': f,
'extension': 'gif',
}
with self.settings(THUMBNAIL_SIZE={'width': 400, 'height': 200}):
self.client.post(reverse('document_upload'), data=data)
d = Document.objects.get(title='img File Doc')
self.assertIsNotNone(d.thumbnail_url)
thumb_file = os.path.join(
settings.MEDIA_ROOT, f"thumbs/{os.path.basename(urlparse(d.thumbnail_url).path)}"
)
file = Image.open(thumb_file)
self.assertEqual(file.size, (400, 200))
finally:
Document.objects.filter(title='img File Doc').delete()

def test_upload_document_form_size_limit(self):
form_data = {
'title': 'GeoNode Map',
Expand Down
1 change: 1 addition & 0 deletions geonode/documents/tests/data/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST
2 changes: 1 addition & 1 deletion geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ def get_geonode_catalogue_service():
'THUMBNAIL_GENERATOR', 'geonode.thumbs.thumbnails.create_gs_thumbnail_geonode')

THUMBNAIL_SIZE = {
'width': int(os.environ.get('THUMBNAIL_GENERATOR_DEFAULT_SIZE_WIDTH', 240)),
'width': int(os.environ.get('THUMBNAIL_GENERATOR_DEFAULT_SIZE_WIDTH', 500)),
'height': int(os.environ.get('THUMBNAIL_GENERATOR_DEFAULT_SIZE_HEIGHT', 200))
}

Expand Down