comparison sat/plugins/plugin_xep_0054.py @ 3185:554b3b632378

memory (cache): purge + pathlib: - cache is now purged on backend startup (every file which has passed EOL will be deleted) - use of pathlib, getPath now returns a Path
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 15:54:43 +0100
parents 559a625a236b
children 5afd7416ca2d
comparison
equal deleted inserted replaced
3184:e8ce30798d15 3185:554b3b632378
16 # GNU Affero General Public License for more details. 16 # GNU Affero General Public License for more details.
17 17
18 # You should have received a copy of the GNU Affero General Public License 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/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 from sat.core.i18n import _ 21 import mimetypes
22 from sat.core.constants import Const as C 22 from base64 import b64decode, b64encode
23 from sat.core.log import getLogger 23 from hashlib import sha1
24 24 from pathlib import Path
25 log = getLogger(__name__) 25 from zope.interface import implementer
26 from twisted.internet import threads, defer 26 from twisted.internet import threads, defer
27 from twisted.words.protocols.jabber import jid, error 27 from twisted.words.protocols.jabber import jid, error
28 from twisted.words.xish import domish 28 from twisted.words.xish import domish
29 from twisted.python.failure import Failure 29 from twisted.python.failure import Failure
30
31 from zope.interface import implementer
32
33 from wokkel import disco, iwokkel 30 from wokkel import disco, iwokkel
34
35 from base64 import b64decode, b64encode
36 from hashlib import sha1
37 from sat.core import exceptions 31 from sat.core import exceptions
32 from sat.core.i18n import _
33 from sat.core.constants import Const as C
34 from sat.core.log import getLogger
38 from sat.memory import persistent 35 from sat.memory import persistent
39 import mimetypes 36
37 log = getLogger(__name__)
40 38
41 try: 39 try:
42 from PIL import Image 40 from PIL import Image
43 except: 41 except:
44 raise exceptions.MissingModule( 42 raise exceptions.MissingModule(
404 raise Failure(exceptions.NotFound()) 402 raise Failure(exceptions.NotFound())
405 403
406 def _getAvatar(self, entity, cache_only, hash_only, profile): 404 def _getAvatar(self, entity, cache_only, hash_only, profile):
407 client = self.host.getClient(profile) 405 client = self.host.getClient(profile)
408 d = self.getAvatar(client, jid.JID(entity), cache_only, hash_only) 406 d = self.getAvatar(client, jid.JID(entity), cache_only, hash_only)
407 # we need to convert the Path to string
408 d.addCallback(str)
409 d.addErrback(lambda __: "") 409 d.addErrback(lambda __: "")
410
411 return d 410 return d
412 411
413 def getAvatar(self, client, entity, cache_only=True, hash_only=False): 412 def getAvatar(self, client, entity, cache_only=True, hash_only=False):
414 """get avatar full path or hash 413 """get avatar full path or hash
415 414
434 if full_path is None: 433 if full_path is None:
435 # cache file is not available (probably expired) 434 # cache file is not available (probably expired)
436 raise KeyError 435 raise KeyError
437 else: 436 else:
438 # avatar has already been checked but it is not set 437 # avatar has already been checked but it is not set
439 full_path = "" 438 full_path = Path("")
440 except KeyError: 439 except KeyError:
441 # avatar is not in cache 440 # avatar is not in cache
442 if cache_only: 441 if cache_only:
443 return defer.fail(Failure(exceptions.NotFound())) 442 return defer.fail(Failure(exceptions.NotFound()))
444 # we request vCard to get avatar 443 # we request vCard to get avatar