mirror of https://gitlab.com/litecord/litecord.git
guilds: decouple icon put logic into a func
This commit is contained in:
parent
060e91907d
commit
1987db11e4
|
|
@ -108,17 +108,39 @@ async def guild_create_channels_prep(guild_id: int, channels: list):
|
||||||
await create_guild_channel(guild_id, channel_id, ctype)
|
await create_guild_channel(guild_id, channel_id, ctype)
|
||||||
|
|
||||||
|
|
||||||
async def put_guild_icon(guild_id: int, icon: str):
|
def sanitize_icon(icon: Optional[str]) -> Optional[str]:
|
||||||
"""Insert a guild icon on the icon database."""
|
"""Return sanitized version of the given icon.
|
||||||
|
|
||||||
|
Defaults to a jpeg icon when the header isn't given.
|
||||||
|
"""
|
||||||
if icon and icon.startswith('data'):
|
if icon and icon.startswith('data'):
|
||||||
encoded = icon
|
return icon
|
||||||
else:
|
|
||||||
encoded = (f'data:image/jpeg;base64,{icon}'
|
return (f'data:image/jpeg;base64,{icon}'
|
||||||
if icon
|
if icon
|
||||||
else None)
|
else None)
|
||||||
|
|
||||||
|
|
||||||
|
async def _general_guild_icon(scope: str, guild_id: int,
|
||||||
|
icon: str, **kwargs):
|
||||||
|
encoded = sanitize_icon(icon)
|
||||||
|
|
||||||
|
icon_kwargs = {
|
||||||
|
'always_icon': True
|
||||||
|
}
|
||||||
|
|
||||||
|
if 'size' in kwargs:
|
||||||
|
icon_kwargs['size'] = kwargs['size']
|
||||||
|
|
||||||
return await app.icons.put(
|
return await app.icons.put(
|
||||||
'guild', guild_id, encoded, size=(128, 128), always_icon=True)
|
scope, guild_id, encoded,
|
||||||
|
**icon_kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def put_guild_icon(guild_id: int, icon: Optional[str]):
|
||||||
|
"""Insert a guild icon on the icon database."""
|
||||||
|
return await _general_guild_icon('guild', guild_id, icon, size=(128, 128))
|
||||||
|
|
||||||
|
|
||||||
@bp.route('', methods=['POST'])
|
@bp.route('', methods=['POST'])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue