annotate src/plugins/plugin_xep_0264.py @ 2513:2d3c9dcec384

plugin XEP-0264: thumbnails handling implementation
author Goffi <goffi@goffi.org>
date Fri, 02 Mar 2018 17:53:31 +0100
parents
children 327bbbe793ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2513
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for managing xep-0264
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # (at your option) any later version.
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.i18n import _
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.constants import Const as C
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.internet import threads
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.python.failure import Failure
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from zope.interface import implements
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel import disco, iwokkel
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.core import exceptions
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 import hashlib
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from PIL import Image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 raise exceptions.MissingModule(u"Missing module pillow, please download/install it from https://python-pillow.github.io")
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from twisted.words.protocols.xmlstream import XMPPHandler
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 except ImportError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from wokkel.subprotocols import XMPPHandler
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45 MIME_TYPE = u'image/jpeg'
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 SAVE_FORMAT = u'JPEG' # (cf. Pillow documentation)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 NS_THUMBS = 'urn:xmpp:thumbs:1'
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 PLUGIN_INFO = {
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_NAME: "XEP-0264",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52 C.PI_IMPORT_NAME: "XEP-0264",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_TYPE: "XEP",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_MODES: C.PLUG_MODE_BOTH,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55 C.PI_PROTOCOLS: ["XEP-0264"],
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 C.PI_DEPENDENCIES: ["XEP-0234"],
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 C.PI_MAIN: "XEP_0264",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 C.PI_HANDLER: "yes",
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 C.PI_DESCRIPTION: _("""Thumbnails handling""")
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60 }
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 class XEP_0264(object):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 SIZE_SMALL = (250, 250)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 SIZE_MEDIUM = (1024, 1024)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def __init__(self, host):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 log.info(_(u"Plugin XEP_0264 initialization"))
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.host = host
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 host.trigger.add("XEP-0234_buildFileElement", self._addFileThumbnails)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 host.trigger.add("XEP-0234_parseFileElement", self._getFileThumbnails)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def getHandler(self, client):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return XEP_0264_handler()
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 ## triggers ##
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def _addFileThumbnails(self, file_elt, extra_args):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 thumbnails = extra_args[u'extra'][C.KEY_THUMBNAILS]
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 except KeyError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 return
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 for thumbnail in thumbnails:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 thumbnail_elt = file_elt.addElement((NS_THUMBS, u'thumbnail'))
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 thumbnail_elt['uri'] = u'cid:' + thumbnail['id']
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 thumbnail_elt['media-type'] = MIME_TYPE
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 width, height = thumbnail['size']
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 thumbnail_elt['width'] = unicode(width)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 thumbnail_elt['height'] = unicode(height)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def _getFileThumbnails(self, file_elt, file_data):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 thumbnails = []
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 for thumbnail_elt in file_elt.elements(NS_THUMBS, u'thumbnail'):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 uri = thumbnail_elt['uri']
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 if uri.startswith ('cid:'):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 thumbnail = {'id': uri[4:]}
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 width = thumbnail_elt.getAttribute('width')
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 height = thumbnail_elt.getAttribute('height')
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if width and height:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 thumbnail['size'] = int(width), int(height)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
102 except ValueError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
103 pass
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
104 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
105 thumbnail['mime_type'] = thumbnail_elt['media-type']
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
106 except KeyError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
107 pass
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
108 thumbnails.append(thumbnail)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
109
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
110 if thumbnails:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
111 file_data.setdefault('extra', {})[C.KEY_THUMBNAILS] = thumbnails
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
112
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
113 ## thumbnails generation ##
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
114
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def getThumbId(self, image_uid, size):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """return an ID unique for image/size combination
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
117
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
118 @param image_uid(unicode): unique id of the image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
119 can be a hash
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
120 @param size(tuple(int)): requested size of thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
121 @return (unicode): unique id for this image/size
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
122 """
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
123 return hashlib.sha256(repr((image_uid, size))).hexdigest()
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
124
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
125 def _blockingGenThumb(self, source_path, size=None, max_age=None, image_uid=None):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
126 """Generate a thumbnail for image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
127
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
128 This is a blocking method and must be executed in a thread
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
129 params are the same as for [generateThumbnail]
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
130 """
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
131 if size is None:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
132 size = self.SIZE_SMALL
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
133 try:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
134 img = Image.open(source_path)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
135 except IOError:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
136 return Failure(exceptions.DataError(u"Can't open image"))
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
137
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
138 img.thumbnail(size)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
139 uid = self.getThumbId(image_uid or source_path, size)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
140
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
141 with self.host.common_cache.cacheData(
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
142 PLUGIN_INFO[C.PI_IMPORT_NAME],
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
143 uid,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
144 MIME_TYPE,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
145 max_age,
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
146 ) as f:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
147 img.save(f, SAVE_FORMAT)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
148
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return img.size, uid
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
150
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def generateThumbnail(self, source_path, size=None, max_age=None, image_uid=None):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
152 """Generate a thumbnail of image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
153
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
154 @param source_path(unicode): absolute path to source image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
155 @param size(int, None): max size of the thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
156 can be one of self.SIZE_*
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
157 None to use default value (i.e. self.SIZE_SMALL)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
158 @param max_age(int, None): same as for [memory.cache.Cache.cacheData])
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
159 @param image_uid(unicode, None): unique ID to identify the image
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
160 use hash whenever possible
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
161 if None, source_path will be used
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
162 @return D(tuple[tuple[int,int], unicode]): tuple with:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
163 - size of the thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
164 - unique Id of the thumbnail
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
165 """
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
166
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
167 d = threads.deferToThread(
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self._blockingGenThumb, source_path, size, max_age, image_uid=image_uid)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
169 d.addErrback(lambda failure_:
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
170 log.error(u"thumbnail generation error: {}".format(failure_)))
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
171 return d
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
172
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
173
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
174 class XEP_0264_handler(XMPPHandler):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
175 implements(iwokkel.IDisco)
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
176
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
178 return [disco.DiscoFeature(NS_THUMBS)]
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
179
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
2d3c9dcec384 plugin XEP-0264: thumbnails handling implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
181 return []