-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #719 from Amsterdam/feature/sig-3510-extra-filetyp…
…e-check SIG-3510 add image filetype check to Sigmax PDF rendering
- Loading branch information
Showing
8 changed files
with
123 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import base64 | ||
import io | ||
import logging | ||
import os | ||
|
||
from django.core.files.storage import default_storage | ||
from PIL import Image, UnidentifiedImageError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class DataUriImageEncodeService: | ||
@staticmethod | ||
def resize(image, max_size): | ||
# Consider image orientation: | ||
if image.width > image.height: | ||
# landscape | ||
width = max_size | ||
height = int((max_size / image.width) * image.height) | ||
else: | ||
# portrait | ||
width = int((max_size / image.height) * image.width) | ||
height = max_size | ||
|
||
return image.resize(size=(width, height), resample=Image.LANCZOS).convert('RGB') | ||
|
||
@staticmethod | ||
def get_context_data_images(signal, max_size): | ||
jpg_data_uris = [] | ||
for att in signal.attachments.all(): | ||
# Attachment is_image property is currently not reliable | ||
_, ext = os.path.splitext(att.file.name) | ||
if ext.lower() not in ['.gif', '.jpg', '.jpeg', '.png']: | ||
continue # unsupported image format, or not image format | ||
|
||
with io.BytesIO() as buffer: | ||
try: | ||
with default_storage.open(att.file.name) as file: | ||
buffer.write(file.read()) | ||
image = Image.open(buffer) | ||
except UnidentifiedImageError: | ||
# PIL cannot open the attached file it is probably not an image. | ||
msg = f'Cannot open image attachment pk={att.pk}' | ||
logger.warning(msg) | ||
continue | ||
except: # noqa:E722 | ||
# Attachment cannot be opened - log the exception. | ||
msg = f'Cannot open image attachment pk={att.pk}' | ||
logger.warning(msg, exc_info=True) | ||
continue | ||
|
||
if image.width > max_size or image.height > max_size: | ||
image = DataUriImageEncodeService.resize(image, max_size) | ||
|
||
with io.BytesIO() as new_buffer: | ||
new_buffer = io.BytesIO() | ||
image.save(new_buffer, format='JPEG') | ||
encoded = f'data:image/jpg;base64,{base64.b64encode(new_buffer.getvalue()).decode("utf-8")}' | ||
|
||
jpg_data_uris.append(encoded) | ||
|
||
return jpg_data_uris |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from io import BytesIO | ||
from unittest.mock import MagicMock | ||
|
||
from PIL import Image | ||
|
||
from signals.apps.services.images import DataUriImageEncodeService | ||
from signals.apps.signals.factories import AttachmentFactory, SignalFactory | ||
from tests.test import SIAReadWriteUserMixin, SignalsBaseApiTestCase | ||
|
||
|
||
class TestImagesService(SIAReadWriteUserMixin, SignalsBaseApiTestCase): | ||
def setUp(self): | ||
self.signal = SignalFactory.create() | ||
|
||
def test_resize_image_too_wide(self): | ||
too_wide = MagicMock() | ||
too_wide.width = 1600 | ||
too_wide.height = 800 | ||
DataUriImageEncodeService.resize(too_wide, 800) | ||
too_wide.resize.assert_called_with(size=(800, 400), resample=Image.LANCZOS) | ||
|
||
def test_resize_iamge_too_heigh(self): | ||
too_heigh = MagicMock() | ||
too_heigh.width = 800 | ||
too_heigh.height = 1600 | ||
DataUriImageEncodeService.resize(too_heigh, 800) | ||
too_heigh.resize.assert_called_with(size=(400, 800), resample=Image.LANCZOS) | ||
|
||
def test_get_context_data_no_images(self): | ||
AttachmentFactory(_signal=self.signal, file__filename='blah.txt', file__data=b'blah', is_image=False) | ||
jpg_data_uris = DataUriImageEncodeService.get_context_data_images(self.signal, 800) | ||
self.assertEqual(len(jpg_data_uris), 0) | ||
|
||
def test_get_context_data_invalid_images(self): | ||
AttachmentFactory.create(_signal=self.signal, file__filename='blah.jpg', file__data=b'blah', is_image=True) | ||
jpg_data_uris = DataUriImageEncodeService.get_context_data_images(self.signal, 800) | ||
self.assertEqual(len(jpg_data_uris), 0) | ||
|
||
def test_get_context_data_valid_image(self): | ||
image = Image.new("RGB", (100, 100), (0, 0, 0)) | ||
buffer = BytesIO() | ||
image.save(buffer, format='JPEG') | ||
|
||
AttachmentFactory.create(_signal=self.signal, file__filename='blah.jpg', file__data=buffer.getvalue()) | ||
jpg_data_uris = DataUriImageEncodeService.get_context_data_images(self.signal, 80) | ||
self.assertEqual(len(jpg_data_uris), 1) | ||
self.assertEqual(jpg_data_uris[0][:22], 'data:image/jpg;base64,') | ||
self.assertGreater(len(jpg_data_uris[0]), 22) |