evalutils: strip exif medatada using utility function (#41679)

This commit is contained in:
Nicolas Roche 2020-04-17 00:57:08 +02:00 committed by Thomas NOEL
parent 919a2069f6
commit 3b81d6f4fb
2 changed files with 75 additions and 8 deletions

View File

@ -9,6 +9,11 @@ import zipfile
import mock
try:
from PIL import Image
except ImportError:
Image = None
from django.utils import six
from django.utils.encoding import force_bytes, force_text
from django.utils.six import BytesIO, StringIO
@ -3804,6 +3809,37 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
assert formdata.data['bo1'].content_type == 'application/vnd.oasis.opendocument.text'
assert formdata.data['bo1'].get_content() == open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb').read()
# check striping metadata
two_pubs.substitutions.feed(formdata)
item.fields = [{'field_id': 'bo1',
'value': '=utils.attachment(form_var_file_raw,' +
' filename="new_"+form_var_file,' +
' content_type="my content type",' +
' strip_metadata=True)'}]
formdata = formdef.data_class()()
formdata.data = {'00': upload}
formdata.just_created()
formdata.store()
two_pubs.substitutions.feed(formdata)
item.perform(formdata)
formdata = formdef.data_class().get(formdata.id)
assert formdata.data['bo1'].base_filename == 'new_test.jpeg'
assert formdata.data['bo1'].content_type == 'my content type'
assert formdata.data['bo1'].qfilename != formdata.data['00'].qfilename
assert formdata.data['00'].get_content().find(b'<exif:XResolution>')
assert not Image or formdata.data['bo1'].get_content().find(b'<exif:XResolution>') == -1
formdata = formdef.data_class()()
formdata.data = {'00': upload2}
formdata.just_created()
formdata.store()
two_pubs.substitutions.feed(formdata)
item.perform(formdata)
formdata = formdef.data_class().get(formdata.id)
assert formdata.data['bo1'].base_filename == 'new_test2.odt'
assert formdata.data['bo1'].content_type == 'my content type'
assert formdata.data['bo1'].qfilename == formdata.data['00'].qfilename
# check storing response as attachment
two_pubs.substitutions.feed(formdata)
item = WebserviceCallStatusItem()

View File

@ -19,15 +19,23 @@ Collection of utility functions to be used in the context of calls to
eval(). They are made available in the "utils" namespace of
get_global_eval_dict.
"""
import base64
import datetime
import time
from django.utils.encoding import force_bytes
from django.utils.six import BytesIO
from wcs.qommon import force_str
from wcs.qommon.upload_storage import UploadStorage
from .misc import get_as_datetime
try:
from PIL import Image
except ImportError:
Image = None
today = datetime.date.today
now = datetime.datetime.now
@ -134,15 +142,38 @@ def add_days(date, count):
return make_date(date) + datetime.timedelta(days=count)
def attachment(content, filename='', content_type='application/octet-stream'):
def attachment(content, filename='', content_type=None, strip_metadata=False):
'''Serialize content as an attachment'''
import base64
from wcs.fields import FileField
if isinstance(content, (bytes, str)):
content = {
'filename': filename,
'b64_content': force_str(base64.b64encode(force_bytes(content))),
}
upload = FileField.convert_value_from_anything(content)
UploadStorage().save(upload)
return {
'filename': filename,
'content_type': content_type,
'b64_content': force_str(base64.b64encode(force_bytes(content))),
}
if strip_metadata and Image:
try:
image = Image.open(BytesIO(upload.get_content()))
except OSError:
pass
else:
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(image.getdata())
content = BytesIO()
image_without_exif.save(content, image.format)
upload = FileField.convert_value_from_anything({
'filename': upload.base_filename,
'content_type': upload.content_type,
'content': content.getvalue(),
})
if filename:
upload.base_filename = filename
if content_type:
upload.content_type = content_type
UploadStorage().save(upload)
return upload
def dict_from_prefix(prefix, in_dict):