Fix broken uploads(?)
This commit is contained in:
parent
661a477fb0
commit
ba6a439929
1 changed files with 8 additions and 3 deletions
11
server.py
11
server.py
|
@ -1,3 +1,4 @@
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import fastapi
|
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(
|
logging.info(
|
||||||
"Creating media at %r called %r with the content type %r and %d bytes",
|
"Creating media at %r called %r with the content type %r and %d bytes",
|
||||||
domain,
|
domain,
|
||||||
filename,
|
filename,
|
||||||
content_type,
|
content_type,
|
||||||
len(file)
|
len(file.getvalue())
|
||||||
)
|
)
|
||||||
|
|
||||||
response = httpx.post(
|
response = httpx.post(
|
||||||
|
@ -196,11 +198,14 @@ def preview_url(
|
||||||
if response_media.status_code not in range(200, 300):
|
if response_media.status_code not in range(200, 300):
|
||||||
logging.warning("Failed to fetch media: %r - HTTP %s", _url, response_media.status_code)
|
logging.warning("Failed to fetch media: %r - HTTP %s", _url, response_media.status_code)
|
||||||
og_tags.pop(tag_name, None)
|
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:
|
else:
|
||||||
upload_response = upload_media(
|
upload_response = upload_media(
|
||||||
domain,
|
domain,
|
||||||
access_token,
|
access_token,
|
||||||
response_media.content,
|
io.BytesIO(response_media.content),
|
||||||
Path(httpx.URL(_url).path).name,
|
Path(httpx.URL(_url).path).name,
|
||||||
response_media.headers.get("content-type", "")
|
response_media.headers.get("content-type", "")
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue