1 # Mattermost Bot module.
2 # Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
10 # pylint: disable=wrong-import-position
11 from AbstractWSHandler import AbstractWSHandler
12 class WSConvertWebp(AbstractWSHandler):
15 def on_WS_EVENT(self, data):
16 post = json.loads(data["data"]["post"])
18 if not "metadata" in post or not "files" in post["metadata"]:
21 with tempfile.TemporaryDirectory() as tmpdirname:
23 for mmfile in post["metadata"]["files"]:
24 if mmfile["mime_type"] in ["image/webp", "invalid-image/webp"]:
25 res = self.bot.api.get_file(mmfile["id"])
26 with open(tmpdirname+"/"+mmfile["id"]+".webp", 'wb') as tmpfile:
27 tmpfile.write(res.content)
28 image = Image.open(tmpdirname+"/"+mmfile["id"]+".webp").convert("RGB")
29 image.save(tmpdirname+"/"+mmfile["id"]+".png", "png")
30 tempfilenames.append(tmpdirname+"/"+mmfile["id"]+".png")
34 if post["root_id"] != "":
35 root_id = post["root_id"]
36 self.bot.api.create_post(post['channel_id'], "Let me convert that webp for you", props={"from_webhook":"true"}, filepaths=tempfilenames, root_id=root_id)