From 26f64bfc06b4c682da8adf6238e339a7c9937820 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Tue, 27 Dec 2022 21:41:16 -0500 Subject: [PATCH] Guess emoji mimetype if not provided in tag (#298) --- activities/models/emoji.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/activities/models/emoji.py b/activities/models/emoji.py index 8c74c27..32d586b 100644 --- a/activities/models/emoji.py +++ b/activities/models/emoji.py @@ -1,3 +1,4 @@ +import mimetypes import re from functools import partial from typing import ClassVar @@ -225,16 +226,23 @@ class Emoji(StatorModel): else: raise ValueError("No name on emoji JSON") + icon = data["icon"] + + mimetype = icon.get("mediaType") + if not mimetype: + mimetype, _ = mimetypes.guess_type(icon["url"]) + if mimetype is None: + raise ValueError("No mimetype on emoji JSON") + # create shortcode = name.lower().strip(":") - icon = data["icon"] category = (icon.get("category") or "")[:100] emoji = cls.objects.create( shortcode=shortcode, domain=None if domain.local else domain, local=domain.local, object_uri=data["id"], - mimetype=icon["mediaType"], + mimetype=mimetype, category=category, remote_url=icon["url"], )