Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0054.py @ 3040:fee60f17ebac
jp: jp asyncio port:
/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\
This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).
During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:56:41 +0200 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3039:a1bc34f90fa5 | 3040:fee60f17ebac |
---|---|
42 from PIL import Image | 42 from PIL import Image |
43 except: | 43 except: |
44 raise exceptions.MissingModule( | 44 raise exceptions.MissingModule( |
45 "Missing module pillow, please download/install it from https://python-pillow.github.io" | 45 "Missing module pillow, please download/install it from https://python-pillow.github.io" |
46 ) | 46 ) |
47 from io import StringIO | 47 import io |
48 | 48 |
49 try: | 49 try: |
50 from twisted.words.protocols.xmlstream import XMPPHandler | 50 from twisted.words.protocols.xmlstream import XMPPHandler |
51 except ImportError: | 51 except ImportError: |
52 from wokkel.subprotocols import XMPPHandler | 52 from wokkel.subprotocols import XMPPHandler |
53 | 53 |
54 AVATAR_PATH = "avatars" | 54 AVATAR_PATH = "avatars" |
55 AVATAR_DIM = (64, 64) # FIXME: dim are not adapted to modern resolutions ! | 55 # AVATAR_DIM = (64, 64) # FIXME: dim are not adapted to modern resolutions ! |
56 AVATAR_DIM = (128, 128) | |
56 | 57 |
57 IQ_GET = '/iq[@type="get"]' | 58 IQ_GET = '/iq[@type="get"]' |
58 NS_VCARD = "vcard-temp" | 59 NS_VCARD = "vcard-temp" |
59 VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' # TODO: manage requests | 60 VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' # TODO: manage requests |
60 | 61 |
316 # TODO: handle EXTVAL | 317 # TODO: handle EXTVAL |
317 try: | 318 try: |
318 avatar_hash = yield threads.deferToThread( | 319 avatar_hash = yield threads.deferToThread( |
319 self.savePhoto, client, elem, entity_jid | 320 self.savePhoto, client, elem, entity_jid |
320 ) | 321 ) |
321 except (exceptions.DataError, exceptions.NotFound) as e: | 322 except (exceptions.DataError, exceptions.NotFound): |
322 avatar_hash = "" | 323 avatar_hash = "" |
323 vcard_dict["avatar"] = avatar_hash | 324 vcard_dict["avatar"] = avatar_hash |
324 except Exception as e: | 325 except Exception as e: |
325 log.error("avatar saving error: {}".format(e)) | 326 log.error("avatar saving error: {}".format(e)) |
326 avatar_hash = None | 327 avatar_hash = None |
513 lower -= offset | 514 lower -= offset |
514 else: | 515 else: |
515 left += offset | 516 left += offset |
516 right -= offset | 517 right -= offset |
517 img = img.crop((left, upper, right, lower)) | 518 img = img.crop((left, upper, right, lower)) |
518 img_buf = StringIO() | 519 img_buf = io.BytesIO() |
519 img.save(img_buf, "PNG") | 520 img.save(img_buf, "PNG") |
520 | 521 |
521 photo_elt = vcard_elt.addElement("PHOTO") | 522 photo_elt = vcard_elt.addElement("PHOTO") |
522 photo_elt.addElement("TYPE", content="image/png") | 523 photo_elt.addElement("TYPE", content="image/png") |
523 photo_elt.addElement("BINVAL", content=b64encode(img_buf.getvalue())) | 524 image_b64 = b64encode(img_buf.getvalue()).decode('utf-8') |
525 photo_elt.addElement("BINVAL", content=image_b64) | |
524 image_hash = sha1(img_buf.getvalue()).hexdigest() | 526 image_hash = sha1(img_buf.getvalue()).hexdigest() |
525 with client.cache.cacheData( | 527 with client.cache.cacheData( |
526 PLUGIN_INFO["import_name"], image_hash, "image/png", MAX_AGE | 528 PLUGIN_INFO["import_name"], image_hash, "image/png", MAX_AGE |
527 ) as f: | 529 ) as f: |
528 f.write(img_buf.getvalue()) | 530 f.write(img_buf.getvalue()) |