Verify that target sizes are sane

This commit is contained in:
Zeva Rose 2022-03-25 12:48:33 -06:00
parent 0729bce295
commit 02d50180f1

View file

@ -58,6 +58,7 @@ class ImageCog(Scale):
async def _resize(
self, ctx: InteractionContext, target: str, attachment: Attachment = None, url: str = None
) -> None:
await ctx.defer()
if not attachment and not url:
await ctx.send("A URL or attachment is required", ephemeral=True)
return
@ -73,10 +74,18 @@ class ImageCog(Scale):
)
return
tgt_size = unconvert_bytesize(float(tgt.groups()[0]), tgt.groups()[1])
try:
tgt_size = unconvert_bytesize(float(tgt.groups()[0]), tgt.groups()[1])
except ValueError:
await ctx.send("Failed to read your target size. Try a more sane one", ephemeral=True)
return
if tgt_size > unconvert_bytesize(8, "MB"):
await ctx.send("Target too large to send. Please make target < 8MB", ephemeral=True)
return
elif tgt_size < 1024:
await ctx.send("Sizes < 1KB are extremely unreliable and are disabled", ephemeral=True)
return
if attachment:
url = attachment.url
@ -91,7 +100,7 @@ class ImageCog(Scale):
size = len(data)
if size <= tgt_size:
await ctx.send("Image already meets target.")
await ctx.send("Image already meets target.", ephemeral=True)
return
ratio = max(tgt_size / size - 0.02, 0.50)