handle missing dimensions without crashing

This commit is contained in:
Matthew Hodgson 2016-04-02 02:29:57 +01:00
parent b26e8604f1
commit 5037ee0d37

View File

@ -78,10 +78,14 @@ class PreviewUrlResource(BaseMediaResource):
"og:description" : media_info['download_name'], "og:description" : media_info['download_name'],
"og:image" : "mxc://%s/%s" % (self.server_name, media_info['filesystem_id']), "og:image" : "mxc://%s/%s" % (self.server_name, media_info['filesystem_id']),
"og:image:type" : media_info['media_type'], "og:image:type" : media_info['media_type'],
"og:image:width" : dims['width'],
"og:image:height" : dims['height'],
} }
if dims:
og["og:image:width"] = dims['width']
og["og:image:height"] = dims['height']
else:
logger.warn("Couldn't get dims for %s" % url)
# define our OG response for this media # define our OG response for this media
elif self._is_html(media_info['media_type']): elif self._is_html(media_info['media_type']):
# TODO: somehow stop a big HTML tree from exploding synapse's RAM # TODO: somehow stop a big HTML tree from exploding synapse's RAM
@ -174,6 +178,7 @@ class PreviewUrlResource(BaseMediaResource):
if meta_image: if meta_image:
og['og:image'] = self._rebase_url(meta_image[0], media_info['uri']) og['og:image'] = self._rebase_url(meta_image[0], media_info['uri'])
else: else:
# TODO: consider inlined CSS styles as well as width & height attribs
images = tree.xpath("//img[@src][number(@width)>10][number(@height)>10]") 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']))) images = sorted(images, key=lambda i: (-1 * int(i.attrib['width']) * int(i.attrib['height'])))
if not images: if not images:
@ -190,10 +195,14 @@ class PreviewUrlResource(BaseMediaResource):
dims = yield self._generate_local_thumbnails( dims = yield self._generate_local_thumbnails(
image_info['filesystem_id'], image_info image_info['filesystem_id'], image_info
) )
if dims:
og["og:image:width"] = dims['width']
og["og:image:height"] = dims['height']
else:
logger.warn("Couldn't get dims for %s" % og["og:image"])
og["og:image"] = "mxc://%s/%s" % (self.server_name, image_info['filesystem_id']) og["og:image"] = "mxc://%s/%s" % (self.server_name, image_info['filesystem_id'])
og["og:image:type"] = image_info['media_type'] og["og:image:type"] = image_info['media_type']
og["og:image:width"] = dims['width']
og["og:image:height"] = dims['height']
else: else:
del og["og:image"] del og["og:image"]