Projet

Général

Profil

0001-misc-add-a-strip_metadata-filter-51483.patch

Frédéric Péters, 13 avril 2021 07:52

Télécharger (5,23 ko)

Voir les différences:

Subject: [PATCH] misc: add a |strip_metadata filter (#51483)

 tests/test_workflows.py           | 15 +++++++++++++++
 wcs/qommon/evalutils.py           | 26 ++------------------------
 wcs/qommon/templatetags/qommon.py |  7 +++++++
 wcs/qommon/upload_storage.py      | 21 ++++++++++++++++++++-
 4 files changed, 44 insertions(+), 25 deletions(-)
tests/test_workflows.py
4408 4408
        == open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
4409 4409
    )
4410 4410

  
4411
    # check |strip_metadata filter
4412
    formdata = formdef.data_class()()
4413
    formdata.data = {'00': upload}
4414
    formdata.just_created()
4415
    formdata.store()
4416

  
4417
    two_pubs.substitutions.feed(formdata)
4418
    item.fields = [{'field_id': 'bo1', 'value': '{{form_var_file|strip_metadata}}'}]
4419
    item.perform(formdata)
4420

  
4421
    assert formdata.data['bo1'].base_filename == 'test.jpeg'
4422
    assert formdata.data['bo1'].content_type == 'image/jpeg'
4423
    assert b'JFIF' in formdata.data['bo1'].get_content()
4424
    assert b'<exif:XResolution>' not in formdata.data['bo1'].get_content()
4425

  
4411 4426
    # check with a template string, into a string field
4412 4427
    two_pubs.substitutions.feed(formdata)
4413 4428
    item.fields = [{'field_id': 'bo2', 'value': '{{form_var_file}}'}]
wcs/qommon/evalutils.py
21 21
"""
22 22
import base64
23 23
import datetime
24
import io
25 24
import time
26 25

  
27 26
from django.utils.encoding import force_bytes
......
31 30

  
32 31
from .misc import get_as_datetime
33 32

  
34
try:
35
    from PIL import Image
36
except ImportError:
37
    Image = None
38

  
39

  
40 33
today = datetime.date.today
41 34
now = datetime.datetime.now
42 35

  
......
154 147
    upload = FileField.convert_value_from_anything(content)
155 148
    UploadStorage().save(upload)
156 149

  
157
    if strip_metadata and Image:
158
        try:
159
            image = Image.open(io.BytesIO(upload.get_content()))
160
        except OSError:
161
            pass
162
        else:
163
            image_without_exif = Image.new(image.mode, image.size)
164
            image_without_exif.putdata(image.getdata())
165
            content = io.BytesIO()
166
            image_without_exif.save(content, image.format)
167
            upload = FileField.convert_value_from_anything(
168
                {
169
                    'filename': upload.base_filename,
170
                    'content_type': upload.content_type,
171
                    'content': content.getvalue(),
172
                }
173
            )
150
    if strip_metadata:
151
        upload = upload.strip_metadata()
174 152
    if filename:
175 153
        upload.base_filename = filename
176 154
    if content_type:
wcs/qommon/templatetags/qommon.py
772 772
    if isinstance(value, (LazyFormDefObjectsManager, LazyList)):
773 773
        return not list(value)
774 774
    return value is None
775

  
776

  
777
@register.filter
778
def strip_metadata(value):
779
    if hasattr(value, 'get_value'):
780
        value = value.get_value()  # unlazy
781
    return value.strip_metadata()
wcs/qommon/upload_storage.py
24 24
from quixote.http_request import Upload
25 25

  
26 26
from .errors import ConnectionError
27
from .misc import can_thumbnail, file_digest, json_loads
27
from .misc import Image, can_thumbnail, file_digest, json_loads
28 28
from .storage import atomic_write
29 29

  
30 30

  
......
72 72
            filename = os.path.join(get_publisher().app_dir, 'uploads', self.qfilename)
73 73
            with open(filename, 'rb') as fd:
74 74
                return fd.read()
75
        if self.fp:
76
            get_storage_object(getattr(self, 'storage', None)).save(self)
77
            return self.get_content()
75 78
        return None
76 79

  
77 80
    def get_base64_content(self):
......
96 99
            self, backoffice=backoffice
97 100
        )
98 101

  
102
    def strip_metadata(self):
103
        if Image is None:
104
            return self
105
        try:
106
            image = Image.open(io.BytesIO(self.get_content()))
107
        except OSError:
108
            return self
109

  
110
        image_without_exif = Image.new(image.mode, image.size)
111
        image_without_exif.putdata(image.getdata())
112
        content = io.BytesIO()
113
        image_without_exif.save(content, image.format)
114
        new_file = PicklableUpload(self.base_filename, self.content_type)
115
        new_file.receive([content.getvalue()])
116
        return new_file
117

  
99 118

  
100 119
class UploadStorageError(Exception):
101 120
    pass
102
-