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

Do not raise an exception when previewing empty media. #8883

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8883.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a 500 error when attempting to preview an empty HTML file.
6 changes: 5 additions & 1 deletion synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,11 @@ async def _expire_url_cache_data(self):
logger.debug("No media removed from url cache")


def decode_and_calc_og(body, media_uri, request_encoding=None):
def decode_and_calc_og(body, media_uri, request_encoding=None) -> Dict[str, str]:
# If there's no body, nothing useful is going to be found.
if not body:
return {}
Comment on lines +681 to +682
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative might be to check if tree (below) is None before passing it into _calc_og.


from lxml import etree

try:
Expand Down
27 changes: 16 additions & 11 deletions tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_long_summarize(self):

desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)

self.assertEquals(
self.assertEqual(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals is the deprecated alias.

desc,
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
Expand All @@ -69,7 +69,7 @@ def test_long_summarize(self):

desc = summarize_paragraphs(example_paras[1:], min_size=200, max_size=500)

self.assertEquals(
self.assertEqual(
desc,
"Tromsø lies in Northern Norway. The municipality has a population of"
" (2015) 72,066, but with an annual influx of students it has over 75,000"
Expand All @@ -96,7 +96,7 @@ def test_short_summarize(self):

desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)

self.assertEquals(
self.assertEqual(
desc,
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
Expand All @@ -122,7 +122,7 @@ def test_small_then_large_summarize(self):
]

desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
self.assertEquals(
self.assertEqual(
desc,
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
Expand All @@ -149,7 +149,7 @@ def test_simple(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(og, {"og:title": "Foo", "og:description": "Some text."})
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})

def test_comment(self):
html = """
Expand All @@ -164,7 +164,7 @@ def test_comment(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(og, {"og:title": "Foo", "og:description": "Some text."})
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})

def test_comment2(self):
html = """
Expand All @@ -182,7 +182,7 @@ def test_comment2(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(
self.assertEqual(
og,
{
"og:title": "Foo",
Expand All @@ -203,7 +203,7 @@ def test_script(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(og, {"og:title": "Foo", "og:description": "Some text."})
self.assertEqual(og, {"og:title": "Foo", "og:description": "Some text."})

def test_missing_title(self):
html = """
Expand All @@ -216,7 +216,7 @@ def test_missing_title(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(og, {"og:title": None, "og:description": "Some text."})
self.assertEqual(og, {"og:title": None, "og:description": "Some text."})

def test_h1_as_title(self):
html = """
Expand All @@ -230,7 +230,7 @@ def test_h1_as_title(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(og, {"og:title": "Title", "og:description": "Some text."})
self.assertEqual(og, {"og:title": "Title", "og:description": "Some text."})

def test_missing_title_and_broken_h1(self):
html = """
Expand All @@ -244,4 +244,9 @@ def test_missing_title_and_broken_h1(self):

og = decode_and_calc_og(html, "http://example.com/test.html")

self.assertEquals(og, {"og:title": None, "og:description": "Some text."})
self.assertEqual(og, {"og:title": None, "og:description": "Some text."})

def test_empty(self):
html = ""
og = decode_and_calc_og(html, "http://example.com/test.html")
self.assertEqual(og, {})