hooks.py 2.29 KB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
1
2
3
"""File related hooks

:organization: Logilab
4
:copyright: 2003-2022 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Nicolas Chauvat's avatar
Nicolas Chauvat committed
5
6
7
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""

Rémi Cardona's avatar
Rémi Cardona committed
8
9
import os

Sylvain Thénault's avatar
Sylvain Thénault committed
10
from cubicweb.server import hook
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
11
from cubicweb.sobjects.notification import ContentAddedView
12
from cubicweb.predicates import is_instance
Nicolas Chauvat's avatar
Nicolas Chauvat committed
13

14
from cubicweb_file.entities import UnResizeable
Nicolas Chauvat's avatar
Nicolas Chauvat committed
15

Rémi Cardona's avatar
Rémi Cardona committed
16

Sylvain Thénault's avatar
Sylvain Thénault committed
17
class UpdateFileHook(hook.Hook):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
18
19
    """a file has been updated, check data_format/data_encoding consistency
    """
Arthur Lutz's avatar
Arthur Lutz committed
20
    __regid__ = 'updatefilehook'
21
    __select__ = hook.Hook.__select__ & is_instance('File')
22
    events = ('before_add_entity', 'before_update_entity',)
23
24
    order = -1  # should be run before other hooks
    category = 'hash'
25

Arthur Lutz's avatar
Arthur Lutz committed
26
    def __call__(self):
27
28
        edited = self.entity.cw_edited
        if 'data' in edited:
Arthur Lutz's avatar
Arthur Lutz committed
29
            self.entity.set_format_and_encoding()
30
31
32
            maxsize = self._cw.vreg.config['image-max-size']
            if maxsize and self.entity.data_format.startswith('image/'):
                iimage = self.entity.cw_adapt_to('IImage')
33
                try:
34
                    edited['data'] = iimage.resize(maxsize)
35
                except UnResizeable:
36
                    # if the resize fails for some reason, do nothing
37
38
                    # (original image will be stored)
                    pass
Nicolas Chauvat's avatar
Nicolas Chauvat committed
39

40
            if self._cw.vreg.config['compute-hash']:
41
                data = edited['data']
42
                if data is not None:
43
44
                    hashdata = self.entity.compute_hash(data.getvalue())
                    edited['data_hash'] = hashdata
45
46
47
48
49
50
51
52
53
54

            # thumbnail cache invalidation
            if 'update' in self.event and 'data' in edited:
                thumb = self.entity.cw_adapt_to('IThumbnail')
                if not thumb:
                    return
                thumbpath = thumb.thumbnail_path()
                if thumbpath:
                    try:
                        os.unlink(thumbpath)
Rémi Cardona's avatar
Rémi Cardona committed
55
                    except Exception as exc:
56
57
58
59
                        self.warning(
                            'could not invalidate thumbnail file `%s` '
                            '(cause: %s)',
                            thumbpath, exc)
60

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
61
62

class FileAddedView(ContentAddedView):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
63
    """get notified from new files"""
64
    __select__ = is_instance('File')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
65
    content_attr = 'description'