def test_upload_avatar_image(self) -> None: conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket = conn.create_bucket(settings.S3_AVATAR_BUCKET) user_profile = self.example_user('hamlet') path_id = user_avatar_path(user_profile) original_image_path_id = path_id + ".original" medium_path_id = path_id + "-medium.png" with get_test_image_file('img.png') as image_file: zerver.lib.upload.upload_backend.upload_avatar_image( image_file, user_profile, user_profile) test_image_data = open(get_test_image_file('img.png').name, 'rb').read() test_medium_image_data = resize_avatar(test_image_data, MEDIUM_AVATAR_SIZE) original_image_key = bucket.get_key(original_image_path_id) self.assertEqual(original_image_key.key, original_image_path_id) image_data = original_image_key.get_contents_as_string() self.assertEqual(image_data, test_image_data) medium_image_key = bucket.get_key(medium_path_id) self.assertEqual(medium_image_key.key, medium_path_id) medium_image_data = medium_image_key.get_contents_as_string() self.assertEqual(medium_image_data, test_medium_image_data) bucket.delete_key(medium_image_key) zerver.lib.upload.upload_backend.ensure_medium_avatar_image( user_profile) medium_image_key = bucket.get_key(medium_path_id) self.assertEqual(medium_image_key.key, medium_path_id)
def test_upload_avatar_image(self) -> None: conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY) bucket = conn.create_bucket(settings.S3_AVATAR_BUCKET) user_profile = self.example_user('hamlet') path_id = user_avatar_path(user_profile) original_image_path_id = path_id + ".original" medium_path_id = path_id + "-medium.png" with get_test_image_file('img.png') as image_file: zerver.lib.upload.upload_backend.upload_avatar_image(image_file, user_profile, user_profile) test_image_data = open(get_test_image_file('img.png').name, 'rb').read() test_medium_image_data = resize_avatar(test_image_data, MEDIUM_AVATAR_SIZE) original_image_key = bucket.get_key(original_image_path_id) self.assertEqual(original_image_key.key, original_image_path_id) image_data = original_image_key.get_contents_as_string() self.assertEqual(image_data, test_image_data) medium_image_key = bucket.get_key(medium_path_id) self.assertEqual(medium_image_key.key, medium_path_id) medium_image_data = medium_image_key.get_contents_as_string() self.assertEqual(medium_image_data, test_medium_image_data) bucket.delete_key(medium_image_key) zerver.lib.upload.upload_backend.ensure_medium_avatar_image(user_profile) medium_image_key = bucket.get_key(medium_path_id) self.assertEqual(medium_image_key.key, medium_path_id)
def create_integration_bot_avatar(logo_path: str, bot_avatar_path: str) -> None: if logo_path.endswith('.svg'): avatar = cairosvg.svg2png( url=logo_path, output_width=DEFAULT_AVATAR_SIZE, output_height=DEFAULT_AVATAR_SIZE) else: with open(logo_path, 'rb') as f: image = f.read() square_image = create_square_image(image) avatar = resize_avatar(square_image) os.makedirs(os.path.dirname(bot_avatar_path), exist_ok=True) with open(bot_avatar_path, 'wb') as f: f.write(avatar)
def create_integration_bot_avatar(logo_path: str) -> None: if logo_path.endswith('.svg'): avatar = cairosvg.svg2png( url=logo_path, output_width=DEFAULT_AVATAR_SIZE, output_height=DEFAULT_AVATAR_SIZE) else: with open(logo_path, 'rb') as f: image = f.read() square_image = create_square_image(image) avatar = resize_avatar(square_image) name = os.path.splitext(os.path.basename(logo_path))[0] bot_avatar_path = os.path.join( ZULIP_PATH, 'static', Integration.DEFAULT_BOT_AVATAR_PATH.format(name=name)) os.makedirs(os.path.dirname(bot_avatar_path), exist_ok=True) with open(bot_avatar_path, 'wb') as f: f.write(avatar)