def _process(self): f = request.files['picture'] try: pic = Image.open(f) except IOError: raise UserValueError( _('You cannot upload this file as profile picture.')) if pic.format.lower() not in {'jpeg', 'png', 'gif'}: raise UserValueError( _('The file has an invalid format ({format}).').format( format=pic.format)) if pic.mode not in {'RGB', 'RGBA'}: pic = pic.convert('RGB') image_bytes = BytesIO() pic = square(pic) if pic.height > 256: pic = pic.resize((256, 256), resample=Image.BICUBIC) pic.save(image_bytes, 'PNG') image_bytes.seek(0) content = image_bytes.read() self.user.picture = content self.user.picture_metadata = { 'hash': crc32(content), 'size': len(content), 'filename': os.path.splitext(secure_filename(f.filename, 'user'))[0] + '.png', 'content_type': 'image/png' } flash(_('Profile picture uploaded'), 'success') logger.info('Profile picture of user %s uploaded by %s', self.user, session.user) return jsonify_data(content=get_picture_data(self.user))
def _process(self, source): self.user.picture_source = source if source == ProfilePictureSource.standard: self.user.picture = None self.user.picture_metadata = None logger.info('Profile picture of user %s removed by %s', self.user, session.user) return '', 204 if source == ProfilePictureSource.custom: f = request.files['picture'] try: pic = Image.open(f) except OSError: raise UserValueError(_('You cannot upload this file as profile picture.')) if pic.format.lower() not in {'jpeg', 'png', 'gif', 'webp'}: raise UserValueError(_('The file has an invalid format ({format}).').format(format=pic.format)) if pic.mode not in ('RGB', 'RGBA'): pic = pic.convert('RGB') pic = square(pic) if pic.height > 256: pic = pic.resize((256, 256), resample=Image.BICUBIC) image_bytes = BytesIO() pic.save(image_bytes, 'PNG') image_bytes.seek(0) set_user_avatar(self.user, image_bytes.read(), f.filename) else: content, lastmod = get_gravatar_for_user(self.user, source == ProfilePictureSource.identicon, 256) set_user_avatar(self.user, content, source.name, lastmod) logger.info('Profile picture of user %s updated by %s', self.user, session.user) return '', 204