Fixing the image conversion to use temp files and time.time for filename on upload

This commit is contained in:
Luke Robles 2022-06-23 14:02:20 -07:00
parent 75aabd6ff0
commit b07000f789

9
app/bot.py Executable file → Normal file
View File

@ -53,15 +53,18 @@ async def fix_cdn_url(ctx):
@bot.listen("on_message") @bot.listen("on_message")
async def convert_heic_to_jpg(ctx): async def convert_heic_to_jpg(ctx):
from cmagick import cmagick from cmagick import cmagick
import time
import tempfile
if ctx.attachments: if ctx.attachments:
for attachment in ctx.attachments: for attachment in ctx.attachments:
if attachment.filename.lower().endswith(("heic", "tiff")): if attachment.filename.lower().endswith(("heic", "tiff")):
source_file = "/tmp/source" source_file = "/tmp/source"
jpg_file = "/tmp/converted.jpg" source_file, file_path = tempfile.mkstemp()
await attachment.save(fp=source_file) jpg_file = "/tmp/%s.jpg" % time.time()
await attachment.save(fp=file_path)
img = cmagick.convert(source_file, jpg_file) img = cmagick.convert(file_path, jpg_file)
try: try:
await ctx.delete() await ctx.delete()