Repository: mozmeao/snippets-service

Issue:
Disable WebP support



Discussion:

Resolving patch:
diff --git a/snippets/base/models.py b/snippets/base/models.py
--- a/snippets/base/models.py
+++ b/snippets/base/models.py
@@ -1,5 +1,4 @@
 import copy
-import io
 import hashlib
 import json
 import os
@@ -11,7 +10,6 @@
 
 from django.conf import settings
 from django.core import validators as django_validators
-from django.core.files.uploadedfile import InMemoryUploadedFile
 from django.urls import reverse
 from django.db import models
 from django.db.models.manager import Manager
@@ -22,7 +20,6 @@
 from django.utils.html import format_html
 
 import bleach
-from PIL import Image
 from jinja2 import Markup
 from jinja2.utils import LRUCache
 
@@ -569,7 +566,7 @@ class Icon(models.Model):
         height_field='height',
         width_field='width',
         validators=[validators.validate_image_format],
-        help_text=('PNG and WebP only. Note that updating the image will '
+        help_text=('PNG only. Note that updating the image will '
                    'update all snippets using this image.'),
     )
 
@@ -602,17 +599,6 @@ def snippets(self):
 
         return ASRSnippet.objects.filter(pk__in=all_snippets)
 
-    def save(self, *args, **kwargs):
-        image = Image.open(self.image.open())
-
-        if image.format != 'WEBP':
-            output = io.BytesIO()
-            image.save(output, 'WEBP', quality=100, lossless=True, method=6)
-            self.image = InMemoryUploadedFile(
-                output, 'ImageField', '{}.webp'.format(self.image.name), 'image/webp', None, None)
-
-        return super().save(*args, **kwargs)
-
 
 class Template(models.Model):
     snippet = models.OneToOneField('ASRSnippet', related_name='template_relation',
diff --git a/snippets/base/validators.py b/snippets/base/validators.py
--- a/snippets/base/validators.py
+++ b/snippets/base/validators.py
@@ -106,7 +106,7 @@ def validate_regex(regex_str):
 
 
 def validate_image_format(image):
-    img = Image.open(image)
-    if img.format not in ['PNG', 'WEBP']:
-        raise ValidationError('Upload only PNG or WebP images.')
+    img = Image.open(image.open())
+    if img.format not in ['PNG']:
+        raise ValidationError('Upload only PNG images.')
     return image


Test patch:
diff --git a/snippets/base/tests/__init__.py b/snippets/base/tests/__init__.py
--- a/snippets/base/tests/__init__.py
+++ b/snippets/base/tests/__init__.py
@@ -141,7 +141,7 @@ class IconFactory(factory.django.DjangoModelFactory):
     name = factory.Sequence(lambda n: 'Icon {0}'.format(n))
     creator = factory.SubFactory(UserFactory)
     image = factory.django.ImageField(width=192, height=192,
-                                      format='WEBP', filename='example.webp')
+                                      format='PNG', filename='example.png')
 
     class Meta:
         model = models.Icon
diff --git a/snippets/base/tests/test_models.py b/snippets/base/tests/test_models.py
--- a/snippets/base/tests/test_models.py
+++ b/snippets/base/tests/test_models.py
@@ -1,11 +1,8 @@
-import io
 from django.conf import settings
 from django.core.files import File
-from django.core.files.uploadedfile import InMemoryUploadedFile
 from django.test.utils import override_settings
 from django.urls import reverse
 
-from PIL import Image
 from jinja2 import Markup
 from unittest.mock import Mock, patch
 from pyquery import PyQuery as pq
@@ -372,30 +369,6 @@ def test_url_without_cdn_url(self):
             settings_mock.SITE_URL = 'http://second-example.com/'
             self.assertEqual(test_file.url, 'http://second-example.com/foo')
 
-    def test_dont_convert_to_webp_if_already_webp(self):
-        with patch('snippets.base.models.InMemoryUploadedFile',
-                   wraps=InMemoryUploadedFile) as imuf:
-            IconFactory()
-        self.assertFalse(imuf.called)
-
-    def test_convert_to_webp_if_not_already_webp(self):
-        """Creates a Icon using the factory and then replaces the image with a PNG
-        image. We expect a WebP image after save().
-
-        """
-        new_icon = io.BytesIO()
-        Image.new('RGB', (30, 30), color='red').save(new_icon, 'PNG')
-
-        icon = IconFactory()
-        icon.image.file = File(new_icon)
-        with patch('snippets.base.models.InMemoryUploadedFile',
-                   wraps=InMemoryUploadedFile) as imuf:
-            icon.save()
-        self.assertTrue(imuf.called)
-        icon.refresh_from_db()
-        image = Image.open(icon.image.open())
-        self.assertEqual(image.format, 'WEBP')
-
 
 class ASRSnippetTests(TestCase):
     def test_render(self):
diff --git a/snippets/base/tests/test_validators.py b/snippets/base/tests/test_validators.py
--- a/snippets/base/tests/test_validators.py
+++ b/snippets/base/tests/test_validators.py
@@ -3,6 +3,7 @@
 from unittest.mock import patch
 
 from django.core.exceptions import ValidationError
+from django.core.files.uploadedfile import InMemoryUploadedFile
 
 from PIL import Image
 
@@ -85,19 +86,14 @@ def test_valid_image_png(self):
         img = Image.new('RGB', (30, 30), color='red')
         fle = io.BytesIO()
         img.save(fle, 'PNG')
+        image = InMemoryUploadedFile(fle, 'ImageField', 'foo.png', 'image/png', None, None)
 
-        self.assertEqual(validate_image_format(fle), fle)
-
-    def test_valid_image_webp(self):
-        img = Image.new('RGB', (30, 30), color='red')
-        fle = io.BytesIO()
-        img.save(fle, 'WEBP')
-
-        self.assertEqual(validate_image_format(fle), fle)
+        self.assertEqual(validate_image_format(image), image)
 
     def test_invalid_image(self):
         img = Image.new('RGB', (30, 30), color='red')
         fle = io.BytesIO()
         img.save(fle, 'JPEG')
+        image = InMemoryUploadedFile(fle, 'ImageField', 'foo.jpg', 'image/jpeg', None, None)
 
-        self.assertRaises(ValidationError, validate_image_format, fle)
+        self.assertRaises(ValidationError, validate_image_format, image)