Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
fix type annotations in preview_url_resource.py
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Sep 24, 2020
1 parent cba1fc2 commit ce0e924
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ async def _get_oembed_content(self, endpoint: str, url: str) -> OEmbedResult:
logger.warning("Error downloading oEmbed metadata from %s: %r", url, e)
raise OEmbedError() from e

async def _download_url(self, url, user):
async def _download_url(self, url: str, user):
# TODO: we should probably honour robots.txt... except in practice
# we're most likely being explicitly triggered by a human rather than a
# bot, so are we really a robot?
Expand All @@ -460,7 +460,7 @@ async def _download_url(self, url, user):
file_info = FileInfo(server_name=None, file_id=file_id, url_cache=True)

# If this URL can be accessed via oEmbed, use that instead.
url_to_download = url
url_to_download = url # type: Optional[str]
oembed_url = self._get_oembed_url(url)
if oembed_url:
# The result might be a new URL to download, or it might be HTML content.
Expand Down Expand Up @@ -520,9 +520,15 @@ async def _download_url(self, url, user):
# FIXME: we should calculate a proper expiration based on the
# Cache-Control and Expire headers. But for now, assume 1 hour.
expires = ONE_HOUR
etag = headers["ETag"][0] if "ETag" in headers else None
etag = (
headers[b"ETag"][0].decode("ascii") if b"ETag" in headers else None
)
else:
html_bytes = oembed_result.html.encode("utf-8") # type: ignore
# we can only get here if we did an oembed request and have an oembed_result.html
assert oembed_result.html is not None
assert oembed_url is not None

html_bytes = oembed_result.html.encode("utf-8")
with self.media_storage.store_into_file(file_info) as (f, fname, finish):
f.write(html_bytes)
await finish()
Expand Down

0 comments on commit ce0e924

Please sign in to comment.