diff --git a/filer/admin/clipboardadmin.py b/filer/admin/clipboardadmin.py index 1d3c12f17..69017a724 100644 --- a/filer/admin/clipboardadmin.py +++ b/filer/admin/clipboardadmin.py @@ -145,31 +145,31 @@ def ajax_upload(request, folder_id=None): height: int = max(1, file_obj.height) width: int = max(1, file_obj.width) pixels: int = width * height - aspect: float = file_obj.width / file_obj.height + aspect: float = width / height res_x: int = int((FILER_MAX_IMAGE_PIXELS * aspect) ** 0.5) res_y: int = int(res_x / aspect) if pixels > 2 * FILER_MAX_IMAGE_PIXELS: - msg = _( - "Image size (%(pixels)d pixels) exceeds limit of %(max_pixels)d " - "pixels by a factor of two or more. Resize image to (%(width)d, " - "%(height)d) resolution or lower." - ).format(pixels=pixels, max_pixels=FILER_MAX_IMAGE_PIXELS, - width=res_x, height=res_y) + if pixels > 2 * FILER_MAX_IMAGE_PIXELS: + msg = _( + "Image size (%(pixels)d million pixels) exceeds limit of " + "%(max_pixels)d million pixels by a factor of two or more. Resize " + "image to %(width)d x %(height)d) resolution or lower." + ) % dict(pixels=pixels / 1000000, max_pixels=FILER_MAX_IMAGE_PIXELS / 1000000, + width=res_x, height=res_y) message = str(msg) add_message(request, ERROR, message) return JsonResponse({'error': message}) if pixels > FILER_MAX_IMAGE_PIXELS: msg = _( - "Image size (%(pixels)d pixels) exceeds limit of %(max_pixels)d " - "pixels. Consider resizing image to (%(width)d, %(height)d) resolution " + "Image size (%(pixels)d million pixels) exceeds limit of %(max_pixels)d " + "million pixels. Consider resizing image to %(width)d x %(height)d resolution " "or lower." - ).format(pixels=pixels, max_pixels=FILER_MAX_IMAGE_PIXELS, + ) % dict(pixels=pixels / 1000000, max_pixels=FILER_MAX_IMAGE_PIXELS / 1000000, width=res_x, height=res_y) message = str(msg) add_message(request, WARNING, message) - # return JsonResponse({'warning': message}) file_obj.folder = folder file_obj.save() else: diff --git a/tests/test_admin.py b/tests/test_admin.py index 3d39763cf..6a117c266 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -8,6 +8,7 @@ from django.contrib.admin import helpers from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission +from django.contrib.messages import get_messages, ERROR, WARNING from django.forms.models import model_to_dict as model_to_dict_django from django.http import HttpRequest, HttpResponseForbidden from django.test import RequestFactory, TestCase @@ -479,7 +480,7 @@ def test_filer_ajax_upload_file(self): def test_filer_ajax_decompression_bomb(self): from filer.admin import clipboardadmin DEFAULT_MAX_IMAGE_PIXELS = clipboardadmin.FILER_MAX_IMAGE_PIXELS - clipboardadmin.FILER_MAX_IMAGE_PIXELS = 800 * 200 + clipboardadmin.FILER_MAX_IMAGE_PIXELS = 800 * 200 # Triggers error self.assertEqual(Image.objects.count(), 0) folder = Folder.objects.create(name='foo') with open(self.filename, 'rb') as fh: @@ -496,6 +497,33 @@ def test_filer_ajax_decompression_bomb(self): ) self.assertEqual(Image.objects.count(), 0) self.assertIn("error", json.loads(response.content.decode("utf-8"))) + messages = list(get_messages(response.wsgi_request)) + self.assertEqual(len(messages), 1) + self.assertEqual(messages[0].level, ERROR) + + clipboardadmin.FILER_MAX_IMAGE_PIXELS = 800 * 300 # Triggers warning + folder = Folder.objects.create(name='foo') + with open(self.filename, 'rb') as fh: + file_obj = django.core.files.File(fh) + url = reverse( + 'admin:filer-ajax_upload', + kwargs={'folder_id': folder.pk} + ) + '?filename=%s' % self.image_name + response = self.client.post( + url, + data=file_obj.read(), + content_type='image/jpeg', + **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'} + ) + self.assertEqual(response.status_code, 200) + messages = list(get_messages(response.wsgi_request)) + self.assertEqual(len(messages), 2) # One more message + self.assertEqual(messages[1].level, WARNING) + self.assertEqual(Image.objects.count(), 1) + stored_image = Image.objects.first() + self.assertEqual(stored_image.original_filename, self.image_name) + self.assertEqual(stored_image.mime_type, 'image/jpeg') + clipboardadmin.FILER_MAX_IMAGE_PIXELS = DEFAULT_MAX_IMAGE_PIXELS def test_filer_ajax_upload_file_using_content_type(self):