Projet

Général

Profil

0001-upload_storage-add-can_thumbnails-method-on-Picklabl.patch

Thomas Noël, 05 février 2020 14:58

Télécharger (5,96 ko)

Voir les différences:

Subject: [PATCH] upload_storage: add can_thumbnails method on PicklableUpload
 (#39517)

 wcs/fields.py                |  4 ++--
 wcs/forms/common.py          | 39 ++++++++++++++++++++----------------
 wcs/qommon/upload_storage.py | 11 +++++++++-
 3 files changed, 34 insertions(+), 20 deletions(-)
wcs/fields.py
38 38
from .qommon import _, force_str
39 39
from .qommon import evalutils
40 40
from .qommon.form import *
41
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, can_thumbnail, xml_node_text
41
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, xml_node_text
42 42
from .qommon.template import Template, TemplateError
43 43
from .qommon import get_cfg, get_logger
44 44

  
......
1041 1041
        t = TemplateIO(html=True)
1042 1042
        t += htmltext('<div class="file-field">')
1043 1043
        t += htmltext('<a download="%s" href="[download]?f=%s">') % (value.base_filename, self.id)
1044
        if include_image_thumbnail and can_thumbnail(value.content_type) and not hasattr(value, 'storage_attrs'):
1044
        if include_image_thumbnail and hasattr(value, 'can_thumbnail') and value.can_thumbnail():
1045 1045
            t += htmltext('<img alt="" src="[download]?f=%s&thumbnail=1"/>') % self.id
1046 1046
        t += htmltext('<span>%s</span>') % value
1047 1047
        t += htmltext('</a></div>')
wcs/forms/common.py
67 67
        if component and component != file.base_filename:
68 68
            raise errors.TraversalError()
69 69

  
70
        if hasattr(file, 'storage_attrs'):  # remote storage
71
            if self.thumbnails:
70
        response = get_response()
71

  
72
        if self.thumbnails:
73
            if hasattr(file, 'can_thumbnail') and file.can_thumbnail():
74
                try:
75
                    thumbnail = misc.get_thumbnail(file.get_filename(),
76
                            content_type=file.content_type)
77
                    response.set_content_type('image/png')
78
                    return thumbnail
79
                except misc.ThumbnailError:
80
                    raise errors.TraversalError()
81
            else:
72 82
                raise errors.TraversalError()
83

  
84
        if hasattr(file, 'storage_attrs'):  # remote storage
73 85
            if not file.storage_attrs.get('redirect_url'):
74 86
                raise errors.TraversalError()
75 87
            redirect_url = sign_url_auto_orig(file.storage_attrs['redirect_url'])
76 88
            return redirect(redirect_url)
77 89

  
78
        response = get_response()
79 90
        if file.content_type:
80 91
            response.set_content_type(file.content_type)
81 92
        else:
......
91 102
            response.set_header('content-disposition',
92 103
                    '%s; filename="%s"' % (content_disposition, file.base_filename))
93 104

  
94
        if self.thumbnails and misc.can_thumbnail(file.content_type):
95
            try:
96
                thumbnail = misc.get_thumbnail(file.get_filename(),
97
                        content_type=file.content_type)
98
                response.set_content_type('image/png')
99
                return thumbnail
100
            except misc.ThumbnailError:
101
                pass
102

  
103 105
        return file.get_file_pointer().read()
104 106

  
105 107

  
......
681 683
        if not hasattr(file, 'content_type'):
682 684
            raise errors.TraversalError()
683 685

  
684
        if hasattr(file, 'storage_attrs'):  # remote storage
685
            if get_request().form.get('thumbnail') == '1':
686
        file_url = 'files/%s/' % fn
687

  
688
        if get_request().form.get('thumbnail') == '1':
689
            if hasattr(file, 'can_thumbnail') and file.can_thumbnail():
690
                file_url += 'thumbnail/'
691
            else:
686 692
                raise errors.TraversalError()
693

  
694
        if hasattr(file, 'storage_attrs'):  # remote storage
687 695
            if not file.storage_attrs.get('redirect_url'):
688 696
                raise errors.TraversalError()
689 697
            redirect_url = sign_url_auto_orig(file.storage_attrs['redirect_url'])
690 698
            return redirect(redirect_url)
691 699

  
692
        file_url = 'files/%s/' % fn
693
        if get_request().form.get('thumbnail') == '1':
694
            file_url += 'thumbnail/'
695 700
        if getattr(file, 'base_filename'):
696 701
            file_url += file.base_filename
697 702
        return redirect(file_url)
wcs/qommon/upload_storage.py
24 24
from django.utils.six import StringIO
25 25

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

  
30 30

  
......
77 77
            return base64.encodestring(content)
78 78
        return b''
79 79

  
80
    def can_thumbnail(self):
81
        return get_storage_object(getattr(self, 'storage', None)).can_thumbnail(self)
82

  
80 83

  
81 84
class UploadStorageError(Exception):
82 85
    pass
......
119 122
            upload.fp.seek(0)
120 123
            atomic_write(filepath, upload.fp, async_op=False)
121 124

  
125
    def can_thumbnail(self, upload):
126
        return can_thumbnail(upload.content_type)
127

  
122 128

  
123 129
class RemoteOpaqueUploadStorage(object):
124 130
    def __init__(self, ws, **kwargs):
......
181 187
    def save(self, upload):
182 188
        pass
183 189

  
190
    def can_thumbnail(self, upload):
191
        return False
192

  
184 193

  
185 194
def get_storage_object(storage):
186 195
    if not storage or storage == 'default':
187
-