Use slixmpp’s new cache module for avatars too.

This commit is contained in:
Emmanuel Gil Peyrot 2018-03-30 22:29:30 +02:00
parent b03a92e7ea
commit a2ad4af79a
2 changed files with 15 additions and 31 deletions

View file

@ -18,6 +18,7 @@ import sys
import time
from slixmpp.xmlstream.handler import Callback
from slixmpp.util import FileSystemPerJidCache
from poezio import connection
from poezio import decorators
@ -30,7 +31,7 @@ from poezio import windows
from poezio.bookmarks import BookmarkList
from poezio.common import safeJID
from poezio.config import config, firstrun
from poezio.config import config, firstrun, CACHE_DIR
from poezio.contact import Contact, Resource
from poezio.daemon import Executor
from poezio.fifo import Fifo
@ -76,6 +77,7 @@ class Core(object):
self.bookmarks = BookmarkList()
self.debug = False
self.remote_fifo = None
self.avatar_cache = FileSystemPerJidCache(CACHE_DIR, 'avatars', binary=True)
# a unique buffer used to store global information
# that are displayed in almost all tabs, in an
# information window.

View file

@ -395,19 +395,15 @@ class HandlerCore:
except Exception:
log.debug('Failed getting metadata from 0084:', exc_info=True)
return
cache_dir = path.join(CACHE_DIR, 'avatars', jid)
for info in metadata:
avatar_hash = info['id']
# First check whether we have it in cache.
cached_path = path.join(cache_dir, avatar_hash)
try:
with open(cached_path, 'rb') as avatar_file:
contact.avatar = avatar_file.read()
log.debug('Using cached avatar')
cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash)
if cached_avatar:
contact.avatar = cached_avatar
log.debug('Using cached avatar for %s', jid)
return
except OSError:
pass
# If we didnt have any, query the data instead.
if not info['url']:
@ -429,16 +425,11 @@ class HandlerCore:
log.debug('Received %s avatar: %s', jid, info['type'])
# Now we save the data on the file system to not have to request it again.
try:
makedirs(cache_dir, exist_ok=True)
with open(cached_path, 'wb') as avatar_file:
avatar_file.write(contact.avatar)
except OSError:
if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar):
log.debug(
'Failed writing %s avatar to cache:',
'Failed writing %ss avatar to cache:',
jid,
exc_info=True)
pass
return
@asyncio.coroutine
@ -451,15 +442,11 @@ class HandlerCore:
log.debug('Received vCard avatar update from %s: %s', jid, avatar_hash)
# First check whether we have it in cache.
cache_dir = path.join(CACHE_DIR, 'avatars', jid)
cached_path = path.join(cache_dir, avatar_hash)
try:
with open(cached_path, 'rb') as avatar_file:
contact.avatar = avatar_file.read()
log.debug('Using cached avatar')
cached_avatar = self.core.avatar_cache.retrieve_by_jid(jid, avatar_hash)
if cached_avatar:
contact.avatar = cached_avatar
log.debug('Using cached avatar for %s', jid)
return
except OSError:
pass
# If we didnt have any, query the vCard instead.
try:
@ -476,13 +463,8 @@ class HandlerCore:
log.debug('Received %s avatar: %s', jid, avatar['TYPE'])
# Now we save the data on the file system to not have to request it again.
try:
makedirs(cache_dir, exist_ok=True)
with open(cached_path, 'wb') as avatar_file:
avatar_file.write(contact.avatar)
except OSError:
log.debug('Failed writing %s avatar to cache:', jid, exc_info=True)
pass
if not self.core.avatar_cache.store_by_jid(jid, avatar_hash, contact.avatar):
log.debug('Failed writing %ss avatar to cache:', jid, exc_info=True)
def on_nick_received(self, message):
"""