Fix crash when fetching emoji without mimetype and extension (#524)

pull/526/head
Humberto Rocha 2023-02-25 16:47:43 -05:00 zatwierdzone przez GitHub
rodzic 2b56b33e38
commit 9aff13118a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 31 dodań i 3 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
from django.db import models
from django.utils.safestring import mark_safe
from PIL import Image
from core.files import get_remote_file
from core.html import FediverseHtmlParser
@ -47,7 +48,11 @@ class EmojiStates(StateGraph):
)
except httpx.RequestError:
return
if file:
if mimetype == "application/octet-stream":
mimetype = Image.open(file).get_format_mimetype()
instance.file = file
instance.mimetype = mimetype
await sync_to_async(instance.save)()
@ -288,8 +293,6 @@ class Emoji(StatorModel):
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.strip(":")
@ -301,6 +304,11 @@ class Emoji(StatorModel):
except cls.DoesNotExist:
pass
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
if emoji.remote_url != icon["url"] or emoji.mimetype != mimetype:
emoji.object_uri = data["id"]
@ -319,7 +327,7 @@ class Emoji(StatorModel):
domain=None if domain.local else domain,
local=domain.local,
object_uri=data["id"],
mimetype=mimetype,
mimetype=mimetype or "application/octet-stream",
category=category,
remote_url=icon["url"],
)

Wyświetl plik

@ -59,3 +59,23 @@ def test_emoji_ingestion(identity):
create=True,
)
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"