Fix crash when fetching emoji without mimetype and extension (#524)
This commit is contained in:
parent
2b56b33e38
commit
9aff13118a
|
@ -11,6 +11,7 @@ from django.core.exceptions import ValidationError
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
from core.files import get_remote_file
|
from core.files import get_remote_file
|
||||||
from core.html import FediverseHtmlParser
|
from core.html import FediverseHtmlParser
|
||||||
|
@ -47,7 +48,11 @@ class EmojiStates(StateGraph):
|
||||||
)
|
)
|
||||||
except httpx.RequestError:
|
except httpx.RequestError:
|
||||||
return
|
return
|
||||||
|
|
||||||
if file:
|
if file:
|
||||||
|
if mimetype == "application/octet-stream":
|
||||||
|
mimetype = Image.open(file).get_format_mimetype()
|
||||||
|
|
||||||
instance.file = file
|
instance.file = file
|
||||||
instance.mimetype = mimetype
|
instance.mimetype = mimetype
|
||||||
await sync_to_async(instance.save)()
|
await sync_to_async(instance.save)()
|
||||||
|
@ -288,8 +293,6 @@ class Emoji(StatorModel):
|
||||||
mimetype = icon.get("mediaType")
|
mimetype = icon.get("mediaType")
|
||||||
if not mimetype:
|
if not mimetype:
|
||||||
mimetype, _ = mimetypes.guess_type(icon["url"])
|
mimetype, _ = mimetypes.guess_type(icon["url"])
|
||||||
if mimetype is None:
|
|
||||||
raise ValueError("No mimetype on emoji JSON")
|
|
||||||
|
|
||||||
# create
|
# create
|
||||||
shortcode = name.strip(":")
|
shortcode = name.strip(":")
|
||||||
|
@ -301,6 +304,11 @@ class Emoji(StatorModel):
|
||||||
except cls.DoesNotExist:
|
except cls.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
# default to previously discovered mimetype if not provided
|
||||||
|
# by the instance to avoid infinite outdated state
|
||||||
|
if mimetype is None:
|
||||||
|
mimetype = emoji.mimetype
|
||||||
|
|
||||||
# Domain previously provided this shortcode. Trample in the new emoji
|
# Domain previously provided this shortcode. Trample in the new emoji
|
||||||
if emoji.remote_url != icon["url"] or emoji.mimetype != mimetype:
|
if emoji.remote_url != icon["url"] or emoji.mimetype != mimetype:
|
||||||
emoji.object_uri = data["id"]
|
emoji.object_uri = data["id"]
|
||||||
|
@ -319,7 +327,7 @@ class Emoji(StatorModel):
|
||||||
domain=None if domain.local else domain,
|
domain=None if domain.local else domain,
|
||||||
local=domain.local,
|
local=domain.local,
|
||||||
object_uri=data["id"],
|
object_uri=data["id"],
|
||||||
mimetype=mimetype,
|
mimetype=mimetype or "application/octet-stream",
|
||||||
category=category,
|
category=category,
|
||||||
remote_url=icon["url"],
|
remote_url=icon["url"],
|
||||||
)
|
)
|
||||||
|
|
|
@ -59,3 +59,23 @@ def test_emoji_ingestion(identity):
|
||||||
create=True,
|
create=True,
|
||||||
)
|
)
|
||||||
assert cased_emoji.shortcode == "CasedEmoji"
|
assert cased_emoji.shortcode == "CasedEmoji"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_emoji_without_mimetype(identity):
|
||||||
|
"""
|
||||||
|
Tests that emoji ingest properly from JSON-LD
|
||||||
|
"""
|
||||||
|
|
||||||
|
emoji = Emoji.by_ap_tag(
|
||||||
|
identity.domain,
|
||||||
|
{
|
||||||
|
"icon": {"type": "Image", "url": "https://example.com/emoji/custom/emoji"},
|
||||||
|
"id": "https://example.com/emoji/custom/emoji",
|
||||||
|
"nameMap": {"und": ":emoji:"},
|
||||||
|
"type": "Emoji",
|
||||||
|
"updated": "1970-01-01T00:00:00Z",
|
||||||
|
},
|
||||||
|
create=True,
|
||||||
|
)
|
||||||
|
assert emoji.shortcode == "emoji"
|
||||||
|
|
Loading…
Reference in New Issue