mirror of
https://github.com/element-hq/synapse.git
synced 2024-11-26 03:25:53 +03:00
Do not raise a 500 exception when previewing empty media. (#8883)
This commit is contained in:
parent
92d87c6882
commit
1f3748f033
3 changed files with 22 additions and 12 deletions
1
changelog.d/8883.bugfix
Normal file
1
changelog.d/8883.bugfix
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Fix a 500 error when attempting to preview an empty HTML file.
|
|
@ -676,7 +676,11 @@ class PreviewUrlResource(DirectServeJsonResource):
|
||||||
logger.debug("No media removed from url cache")
|
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
|
from lxml import etree
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -56,7 +56,7 @@ class PreviewTestCase(unittest.TestCase):
|
||||||
|
|
||||||
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
||||||
|
|
||||||
self.assertEquals(
|
self.assertEqual(
|
||||||
desc,
|
desc,
|
||||||
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
||||||
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
||||||
|
@ -69,7 +69,7 @@ class PreviewTestCase(unittest.TestCase):
|
||||||
|
|
||||||
desc = summarize_paragraphs(example_paras[1:], min_size=200, max_size=500)
|
desc = summarize_paragraphs(example_paras[1:], min_size=200, max_size=500)
|
||||||
|
|
||||||
self.assertEquals(
|
self.assertEqual(
|
||||||
desc,
|
desc,
|
||||||
"Tromsø lies in Northern Norway. The municipality has a population of"
|
"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"
|
" (2015) 72,066, but with an annual influx of students it has over 75,000"
|
||||||
|
@ -96,7 +96,7 @@ class PreviewTestCase(unittest.TestCase):
|
||||||
|
|
||||||
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
||||||
|
|
||||||
self.assertEquals(
|
self.assertEqual(
|
||||||
desc,
|
desc,
|
||||||
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
||||||
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
||||||
|
@ -122,7 +122,7 @@ class PreviewTestCase(unittest.TestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
|
||||||
self.assertEquals(
|
self.assertEqual(
|
||||||
desc,
|
desc,
|
||||||
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
|
||||||
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
|
||||||
|
@ -149,7 +149,7 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
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):
|
def test_comment(self):
|
||||||
html = """
|
html = """
|
||||||
|
@ -164,7 +164,7 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
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):
|
def test_comment2(self):
|
||||||
html = """
|
html = """
|
||||||
|
@ -182,7 +182,7 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
og = decode_and_calc_og(html, "http://example.com/test.html")
|
||||||
|
|
||||||
self.assertEquals(
|
self.assertEqual(
|
||||||
og,
|
og,
|
||||||
{
|
{
|
||||||
"og:title": "Foo",
|
"og:title": "Foo",
|
||||||
|
@ -203,7 +203,7 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
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):
|
def test_missing_title(self):
|
||||||
html = """
|
html = """
|
||||||
|
@ -216,7 +216,7 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
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):
|
def test_h1_as_title(self):
|
||||||
html = """
|
html = """
|
||||||
|
@ -230,7 +230,7 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
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):
|
def test_missing_title_and_broken_h1(self):
|
||||||
html = """
|
html = """
|
||||||
|
@ -244,4 +244,9 @@ class PreviewUrlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
og = decode_and_calc_og(html, "http://example.com/test.html")
|
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, {})
|
||||||
|
|
Loading…
Reference in a new issue