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"] == "image/webp":
25 res = self.bot.api.get_file(mmfile["id"])
26 open(tmpdirname+"/"+mmfile["id"]+".webp", 'wb').write(res.content)
27 image = Image.open(tmpdirname+"/"+mmfile["id"]+".webp").convert("RGB")
28 image.save(tmpdirname+"/"+mmfile["id"]+".png", "png")
29 tempfilenames.append(tmpdirname+"/"+mmfile["id"]+".png")
33 if post["root_id"] != "":
34 root_id = post["root_id"]
35 self.bot.api.create_post(post['channel_id'], "Let me convert that for you", props={"from_webhook":"true"}, filepaths=tempfilenames, root_id=root_id)