<commit_before>import base64
from cStringIO import StringIO

from twisted.words.xish import domish
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from txmongo import gridfs
import Image

from base import send_raw
import bnw_core.base
import bnw_core.bnw_mongo
from bnw_core import bnw_objects as objs


def get_and_resize_avatar(iq):
    mimetype = str(iq.vCard.PHOTO.TYPE)
    if mimetype not in ('image/png', 'image/jpeg', 'image/gif'):
        return
    filedata = iq.vCard.PHOTO.BINVAL
    if not filedata:
        return
    filedata = str(filedata)
    if len(filedata) > 32768:
        # XEP-0153 says what image SHOULD be 8KB max,
        # let's approve up to 32KB.
        return
    try:
        avatar = base64.b64decode(filedata)
    except TypeError:
        return
    try:
        im = Image.open(StringIO(avatar))
        im.thumbnail((48, 48), Image.ANTIALIAS)
        thumb_f = StringIO()
        im.save(thumb_f, 'png')
    except IOError:
        return
    return avatar, mimetype, thumb_f.getvalue()

@defer.inlineCallbacks
def vcard(iq, iq_user):
    if not iq.vCard:
        defer.returnValue(False)
    if not iq_user:
        # User which have been sent IQ not registered.
        defer.returnValue(True)
    v = iq.vCard
    # Update avatar info.
    av_info = iq_user.get('avatar')
    if v.PHOTO:
        res = yield deferToThread(get_and_resize_avatar, iq)
        if res:
            avatar, mimetype, thumb = res
            fs = yield bnw_core.bnw_mongo.get_fs('avatars')
            update_needed = True
            if av_info:
                # TODO: Fix fs.get/GridOut.__init__ in txmongo
                doc = yield fs._GridFS__files.find_one({'_id': av_info[0]})
                f = yield fs.get(doc)
                old_avatar = yield f.read()
                if old_avatar == avatar:
                    update_needed = False
                else:
                    fs.delete(av_info[0])
                    fs.delete(av_info[2])
            if update_needed:
                extension = mimetype.split('/')[1]<commit_msg>Fix avatars being reset on vcard request.<commit_after>import base64
from cStringIO import StringIO

from twisted.words.xish import domish
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from txmongo import gridfs
import Image

from base import send_raw
import bnw_core.base
import bnw_core.bnw_mongo
from bnw_core import bnw_objects as objs


def get_and_resize_avatar(iq):
    mimetype = str(iq.vCard.PHOTO.TYPE)
    if mimetype not in ('image/png', 'image/jpeg', 'image/gif'):
        return
    filedata = iq.vCard.PHOTO.BINVAL
    if not filedata:
        return
    filedata = str(filedata)
    if len(filedata) > 32768:
        # XEP-0153 says what image SHOULD be 8KB max,
        # let's approve up to 32KB.
        return
    try:
        avatar = base64.b64decode(filedata)
    except TypeError:
        return
    try:
        im = Image.open(StringIO(avatar))
        im.thumbnail((48, 48), Image.ANTIALIAS)
        thumb_f = StringIO()
        im.save(thumb_f, 'png')
    except IOError:
        return
    return avatar, mimetype, thumb_f.getvalue()

@defer.inlineCallbacks
def vcard(iq, iq_user):
    if (not iq.vCard) or iq['type']!='result':
        defer.returnValue(False)
    if not iq_user:
        # User which have been sent IQ not registered.
        defer.returnValue(True)
    v = iq.vCard
    # Update avatar info.
    av_info = iq_user.get('avatar')
    if v.PHOTO:
        res = yield deferToThread(get_and_resize_avatar, iq)
        if res:
            avatar, mimetype, thumb = res
            fs = yield bnw_core.bnw_mongo.get_fs('avatars')
            update_needed = True
            if av_info:
                # TODO: Fix fs.get/GridOut.__init__ in txmongo
                doc = yield fs._GridFS__files.find_one({'_id': av_info[0]})
                f = yield fs.get(doc)
                old_avatar = yield f.read()
                if old_avatar == avatar:
                    update_needed = False
                else:
                    fs.delete(av_info[0])
                    fs.delete(av_info[2])
            if update_needed:
                extension = mimetype.split('/')[1]
{'fraction_non_alphanumeric': 0.08841463414634146, 'fraction_numerical': 0.012195121951219513, 'mean_word_length': 2.108600583090379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7022170', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1353, 'n_words': 349}