guilds: fix guild update icon call

- images: add support for splash and banner
 - add jpeg check when converting extension
This commit is contained in:
Luna 2019-03-15 04:17:57 -03:00
parent 7cd95d3f94
commit 44b81ea2f6
2 changed files with 38 additions and 1 deletions

View File

@ -224,7 +224,7 @@ async def _guild_update_icon(scope: str, guild_id: int,
icon: Optional[str], **kwargs): icon: Optional[str], **kwargs):
"""Update icon.""" """Update icon."""
new_icon = await app.icons.update( new_icon = await app.icons.update(
'guild', guild_id, icon, always_icon=True, **kwargs scope, guild_id, icon, always_icon=True, **kwargs
) )
table = { table = {

View File

@ -46,6 +46,7 @@ EXTENSIONS = {
MIMES = { MIMES = {
'jpg': 'image/jpeg', 'jpg': 'image/jpeg',
'jpe': 'image/jpeg', 'jpe': 'image/jpeg',
'jpeg': 'image/jpeg',
'webp': 'image/webp', 'webp': 'image/webp',
} }
@ -171,15 +172,23 @@ def parse_data_uri(string) -> tuple:
def _gen_update_sql(scope: str) -> str: def _gen_update_sql(scope: str) -> str:
# match a scope to (table, field)
field = { field = {
'user': 'avatar', 'user': 'avatar',
'guild': 'icon', 'guild': 'icon',
'splash': 'splash',
'banner': 'banner',
'channel-icons': 'icon', 'channel-icons': 'icon',
}[scope] }[scope]
table = { table = {
'user': 'users', 'user': 'users',
'guild': 'guilds', 'guild': 'guilds',
'splash': 'guilds',
'banner': 'guilds',
'channel-icons': 'group_dm_channels' 'channel-icons': 'group_dm_channels'
}[scope] }[scope]
@ -269,6 +278,8 @@ class IconManager:
self.storage = app.storage self.storage = app.storage
async def _convert_ext(self, icon: Icon, target: str): async def _convert_ext(self, icon: Icon, target: str):
target = 'jpeg' if target == 'jpg' else target
target_mime = get_mime(target) target_mime = get_mime(target)
log.info('converting from {} to {}', icon.mime, target_mime) log.info('converting from {} to {}', icon.mime, target_mime)
@ -279,6 +290,10 @@ class IconManager:
image = Image.open(icon.as_path) image = Image.open(icon.as_path)
target_fd = target_path.open('wb') target_fd = target_path.open('wb')
if target == 'jpeg':
image = image.convert('RGB')
image.save(target_fd, format=target) image.save(target_fd, format=target)
target_fd.close() target_fd.close()
@ -386,6 +401,9 @@ class IconManager:
if scope == 'user' and mime == 'image/gif': if scope == 'user' and mime == 'image/gif':
icon_hash = f'a_{icon_hash}' icon_hash = f'a_{icon_hash}'
log.debug('PUT icon {!r} {!r} {!r} {!r}',
scope, key, icon_hash, mime)
await self.storage.db.execute(""" await self.storage.db.execute("""
INSERT INTO icons (scope, key, hash, mime) INSERT INTO icons (scope, key, hash, mime)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4)
@ -406,6 +424,9 @@ class IconManager:
if not icon: if not icon:
return return
log.debug('DEL {}',
icon)
# dereference # dereference
await self.storage.db.execute(""" await self.storage.db.execute("""
UPDATE users UPDATE users
@ -430,6 +451,18 @@ class IconManager:
WHERE icon = $1 WHERE icon = $1
""", icon.icon_hash) """, icon.icon_hash)
await self.storage.db.execute("""
UPDATE guilds
SET splash = NULL
WHERE splash = $1
""", icon.icon_hash)
await self.storage.db.execute("""
UPDATE guilds
SET banner = NULL
WHERE banner = $1
""", icon.icon_hash)
await self.storage.db.execute(""" await self.storage.db.execute("""
UPDATE group_dm_channels UPDATE group_dm_channels
SET icon = NULL SET icon = NULL
@ -455,7 +488,11 @@ class IconManager:
old_icon_hash = await self.storage.db.fetchval( old_icon_hash = await self.storage.db.fetchval(
_gen_update_sql(scope), key) _gen_update_sql(scope), key)
# converting key to str only here since from here onwards
# its operations on the icons table (or a dereference with
# the delete() method but that will work regardless)
key = str(key) key = str(key)
old_icon = await self.generic_get(scope, key, old_icon_hash) old_icon = await self.generic_get(scope, key, old_icon_hash)
await self.delete(old_icon) await self.delete(old_icon)