diff --git a/server.py b/server.py index 979fa87..353196a 100644 --- a/server.py +++ b/server.py @@ -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", "") )