comparison sat/plugins/plugin_xep_0264.py @ 2562:26edcf3a30eb

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