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

Commit

Permalink
Do not raise an exception when previewing empty media.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Dec 4, 2020
1 parent 96358cb commit 5223a13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
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 {}

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(
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, {})

0 comments on commit 5223a13

Please sign in to comment.