Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Properly handle thumbnail errors

Roland Geidera3c5fe189d72
diff --git a/wger/nutrition/api/serializers.py b/wger/nutrition/api/serializers.py
index c46fe0a29..8cfa02394 100644
--- a/wger/nutrition/api/serializers.py
+++ b/wger/nutrition/api/serializers.py
@@ -13,10 +13,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
+# Standard Library
+import logging
+
# Django
from django.conf import settings
# Third Party
+from easy_thumbnails.exceptions import EasyThumbnailsError
from easy_thumbnails.files import get_thumbnailer
from rest_framework import serializers
@@ -32,6 +36,9 @@ from wger.nutrition.models import (
)
+logger = logging.getLogger(__name__)
+
+
class IngredientWeightUnitSerializer(serializers.ModelSerializer):
"""
IngredientWeightUnit serializer
@@ -195,7 +202,11 @@ class IngredientInfoSerializer(serializers.ModelSerializer):
thumbnailer = get_thumbnailer(obj.image.image)
for alias in aliases:
opts = settings.THUMBNAIL_ALIASES[''][alias]
- thumb = thumbnailer.get_thumbnail(opts)
+ try:
+ thumb = thumbnailer.get_thumbnail(opts)
+ except (EasyThumbnailsError, OSError, ValueError):
+ logger.warning('Could not generate thumbnails for image %s', obj.image.pk)
+ return None
result[alias] = request.build_absolute_uri(thumb.url)
return result
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index cb972359c..e38dce29a 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -15,6 +15,7 @@
# Standard Library
import datetime
+import io
import json
from unittest.mock import (
MagicMock,
@@ -22,10 +23,12 @@ from unittest.mock import (
)
# Django
+from django.core.files.base import ContentFile
from django.test import SimpleTestCase
from django.urls import reverse
# Third Party
+from PIL import Image as PILImage
from rest_framework import status
# wger
@@ -895,3 +898,24 @@ class IngredientSyncViewSetTestCase(WgerTestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'], [])
self.assertIsNone(response.data['next'])
+
+ def test_unprocessable_image_does_not_break_page(self):
+ """An image that cannot be thumbnailed yields `thumbnails: None`, not a 500."""
+
+ # An extreme aspect ratio makes easy_thumbnails scale one dimension down
+ # to zero, which raises a ValueError from Pillow during thumbnailing
+ source = PILImage.new('RGB', (1, 800))
+ buffer = io.BytesIO()
+ source.save(buffer, format='PNG')
+
+ image = Image(ingredient=Ingredient.objects.get(pk=1), size=buffer.getbuffer().nbytes)
+ image.image.save('bad.png', ContentFile(buffer.getvalue()), save=False)
+ image.width = source.width
+ image.height = source.height
+ image.save()
+
+ response = self.client.get(self.url)
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ entry = next(i for i in response.data['results'] if i['id'] == 1)
+ self.assertIsNone(entry['thumbnails'])