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

Commit

Permalink
Merge pull request #872 from matrix-org/erikj/preview_url_fixes
Browse files Browse the repository at this point in the history
Fix some `/preview_url` explosions
  • Loading branch information
erikjohnston authored Jun 16, 2016
2 parents 78a16d3 + 09a17f9 commit 1f31381
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ def _calc_og(self, tree, media_info, requester):

og = {}
for tag in tree.xpath("//*/meta[starts-with(@property, 'og:')]"):
og[tag.attrib['property']] = tag.attrib['content']
if 'content' in tag.attrib:
og[tag.attrib['property']] = tag.attrib['content']

# TODO: grab article: meta tags too, e.g.:

Expand All @@ -279,17 +280,17 @@ def _calc_og(self, tree, media_info, requester):
# TODO: consider inlined CSS styles as well as width & height attribs
images = tree.xpath("//img[@src][number(@width)>10][number(@height)>10]")
images = sorted(images, key=lambda i: (
-1 * int(i.attrib['width']) * int(i.attrib['height'])
-1 * float(i.attrib['width']) * float(i.attrib['height'])
))
if not images:
images = tree.xpath("//img[@src]")
if images:
og['og:image'] = images[0].attrib['src']

# pre-cache the image for posterity
# FIXME: it might be cleaner to use the same flow as the main /preview_url request
# itself and benefit from the same caching etc. But for now we just rely on the
# caching on the master request to speed things up.
# FIXME: it might be cleaner to use the same flow as the main /preview_url
# request itself and benefit from the same caching etc. But for now we
# just rely on the caching on the master request to speed things up.
if 'og:image' in og and og['og:image']:
image_info = yield self._download_url(
self._rebase_url(og['og:image'], media_info['uri']), requester.user
Expand Down

0 comments on commit 1f31381

Please sign in to comment.