plugin XEP-0054(XEP-0153): ignore image formats other than gif, jpeg and png, and empty BINVAL
This commit is contained in:
parent
6a007eb69a
commit
d4d4f6534b
1 changed files with 34 additions and 17 deletions
|
|
@ -204,17 +204,33 @@ class XEP_0054(object):
|
|||
else:
|
||||
log.debug(_(u"file [%s] already in cache") % hash_)
|
||||
|
||||
def savePhoto(self, photo_xml):
|
||||
"""Parse a <PHOTO> elem and save the picture"""
|
||||
for elem in photo_xml.elements():
|
||||
if elem.name == 'TYPE':
|
||||
log.debug(_(u'Photo of type [%s] found') % str(elem))
|
||||
if elem.name == 'BINVAL':
|
||||
log.debug(_('Decoding binary'))
|
||||
decoded = b64decode(str(elem))
|
||||
image_hash = sha1(decoded).hexdigest()
|
||||
self.saveAvatarFile(decoded, image_hash)
|
||||
return image_hash
|
||||
def savePhoto(self, photo_elt, target):
|
||||
"""Parse a <PHOTO> photo_elt and save the picture"""
|
||||
try:
|
||||
type_ = unicode(photo_elt.elements(NS_VCARD, 'TYPE').next())
|
||||
except StopIteration:
|
||||
type_ = None
|
||||
else:
|
||||
if type_ and type_ not in ("image/gif", "image/jpeg", "image/png"):
|
||||
# TODO: handle other image formats (svg?)
|
||||
log.warning(u"following avatar image format is not handled: {type} [{jid}]".format(
|
||||
type=type_, jid=target.full()))
|
||||
return
|
||||
log.debug(u'Photo of type {type} found [{jid}]'.format(
|
||||
type=type_, jid=target.full()))
|
||||
try:
|
||||
bin_elt = photo_elt.elements(NS_VCARD, 'BINVAL').next()
|
||||
except StopIteration:
|
||||
return
|
||||
buf = str(bin_elt)
|
||||
if not buf:
|
||||
log.warning(u"empty avatar for {jid}".format(jid=target.full()))
|
||||
return
|
||||
log.debug(_(u'Decoding binary'))
|
||||
decoded = b64decode(buf)
|
||||
image_hash = sha1(decoded).hexdigest()
|
||||
self.saveAvatarFile(decoded, image_hash)
|
||||
return image_hash
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def vCard2Dict(self, client, vcard, target):
|
||||
|
|
@ -235,13 +251,14 @@ class XEP_0054(object):
|
|||
elif elem.name == 'BDAY':
|
||||
dictionary['birthday'] = unicode(elem)
|
||||
elif elem.name == 'PHOTO':
|
||||
dictionary["avatar"] = yield threads.deferToThread(self.savePhoto, elem)
|
||||
if not dictionary["avatar"]: # can happen in case of e.g. empty photo elem
|
||||
del dictionary['avatar']
|
||||
else:
|
||||
self.updateCache(client, target, 'avatar', dictionary['avatar'])
|
||||
# TODO: handle EXTVAL
|
||||
avatar_hash = yield threads.deferToThread(self.savePhoto, elem, target)
|
||||
if avatar_hash is None: # can happen e.g. in case of empty photo elem
|
||||
continue
|
||||
dictionary['avatar'] = avatar_hash
|
||||
self.updateCache(client, target, 'avatar', dictionary['avatar'])
|
||||
else:
|
||||
log.info(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
|
||||
log.debug(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
|
||||
|
||||
# if a data in cache doesn't exist anymore, we need to reset it
|
||||
# so we check CACHED_DATA no gotten (i.e. not in dictionary keys)
|
||||
|
|
|
|||
Loading…
Reference in a new issue