Fix broken uploads(?)

This commit is contained in:
Nexus 2024-02-09 23:11:07 +00:00
parent 661a477fb0
commit ba6a439929
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -1,3 +1,4 @@
import io
import os
import fastapi
@ -105,13 +106,14 @@ URL_OG_TAGS = [
]
def upload_media(domain: str, access_token: str, file: bytes, filename: str, content_type: str):
def upload_media(domain: str, access_token: str, file: io.BytesIO, filename: str, content_type: str):
file.seek(0)
logging.info(
"Creating media at %r called %r with the content type %r and %d bytes",
domain,
filename,
content_type,
len(file)
len(file.getvalue())
)
response = httpx.post(
@ -196,11 +198,14 @@ def preview_url(
if response_media.status_code not in range(200, 300):
logging.warning("Failed to fetch media: %r - HTTP %s", _url, response_media.status_code)
og_tags.pop(tag_name, None)
elif not response_media.headers.get("content-type", "").startswith(("image/", "video/", "audio/")):
logging.warning("Failed to fetch media: %r - not a media type", _url)
og_tags.pop(tag_name, None)
else:
upload_response = upload_media(
domain,
access_token,
response_media.content,
io.BytesIO(response_media.content),
Path(httpx.URL(_url).path).name,
response_media.headers.get("content-type", "")
)