Skip to content

Commit

Permalink
Guess Post attachment mimetype when 'mediaType' field not provided (j…
Browse files Browse the repository at this point in the history
  • Loading branch information
manfre authored Jan 6, 2023
1 parent f1fa4aa commit af47e9d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion activities/models/post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hashlib
import mimetypes
import re
from collections.abc import Iterable
from typing import Optional
Expand Down Expand Up @@ -795,9 +796,14 @@ def by_ap(cls, data, create=False, update=False, fetch_author=False) -> "Post":
focal_x, focal_y = attachment["focalPoint"]
else:
focal_x, focal_y = None, None
mimetype = attachment.get("mediaType")
if not mimetype:
mimetype, _ = mimetypes.guess_type(attachment["url"])
if not mimetype:
mimetype = "application/octet-stream"
post.attachments.create(
remote_url=attachment["url"],
mimetype=attachment["mediaType"],
mimetype=mimetype,
name=attachment.get("name"),
width=attachment.get("width"),
height=attachment.get("height"),
Expand Down

0 comments on commit af47e9d

Please sign in to comment.