Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

thumbnail: Only support Markdown image syntax for uploaded images.

Alex Vandiverbbe3c59d5c15
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index e030c0b2f2b..82fafc97d06 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -2141,41 +2141,40 @@ class ImageInlineProcessor(markdown.inlinepatterns.ImageInlineProcessor):
db_data: DbData | None = self.zmd.zulip_db_data
src = rewrite_local_links_to_relative(db_data, src)
maybe_add_attachment_path_id(src, self.zmd)
- img.set("data-original-src", src)
- # We try to use image thumbnails for previews of the user
- # upload images rather than original images when available.
- if src.startswith("/user_uploads/") and db_data:
- path_id = src[len("/user_uploads/") :]
+ # We only support Markdown image syntax for uploaded files
+ if not src.startswith("/user_uploads/"):
+ return None
- # We should have pulled the preview data for this image
- # (even if that's "no preview yet") from the database
- # before rendering; Else, its header didn't parse as
- # a valid image type which libvips handles.
- if path_id not in db_data.user_upload_previews.image_metadata:
- return None
- else:
- assert path_id in db_data.user_upload_previews.image_metadata
- metadata = db_data.user_upload_previews.image_metadata[path_id]
+ path_id = src[len("/user_uploads/") :]
- # Insert a placeholder image spinner. We post-process
- # this content (see rewrite_thumbnailed_images in
- # zerver.lib.thumbnail), looking specifically for this
- # tag, and may re-write it into the thumbnail URL if it
- # already exists when the message is sent.
- img.set("class", "inline-image image-loading-placeholder")
- img.set("src", "/static/images/loading/loader-black.svg")
+ # We should have pulled the preview data for this image
+ # (even if that's "no preview yet") from the database
+ # before rendering; Else, its header didn't parse as
+ # a valid image type which libvips handles.
+ if not db_data or path_id not in db_data.user_upload_previews.image_metadata:
+ return None
+ else:
+ assert path_id in db_data.user_upload_previews.image_metadata
+ metadata = db_data.user_upload_previews.image_metadata[path_id]
+
+ # Insert a placeholder image spinner. We post-process
+ # this content (see rewrite_thumbnailed_images in
+ # zerver.lib.thumbnail), looking specifically for this
+ # tag, and may re-write it into the thumbnail URL if it
+ # already exists when the message is sent.
+ img.set("data-original-src", src)
+ img.set("class", "inline-image image-loading-placeholder")
+ img.set("src", "/static/images/loading/loader-black.svg")
+ img.set(
+ "data-original-dimensions",
+ f"{metadata.original_width_px}x{metadata.original_height_px}",
+ )
+ if metadata.original_content_type:
img.set(
- "data-original-dimensions",
- f"{metadata.original_width_px}x{metadata.original_height_px}",
+ "data-original-content-type",
+ metadata.original_content_type,
)
- if metadata.original_content_type:
- img.set(
- "data-original-content-type",
- metadata.original_content_type,
- )
- else:
- img.set("src", src)
return img
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index 34949aa1c13..43f93762a54 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -830,29 +830,6 @@ class MarkdownEmbedsTest(ZulipTestCase):
f"""<p><a href="http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw">http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw</a></p>\n<div class="youtube-video message_inline_image"><a data-id="nOJgD4fcZhI" href="http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw"><img src="{get_camo_url("https://i.ytimg.com/vi/nOJgD4fcZhI/mqdefault.jpg")}"></a></div>""",
)
- def test_inline_image(self) -> None:
- image_url = "https://www.google.com/images/srpr/logo4w.png"
- msg = f"The Google logo looks like this: ![Google logo]({image_url}) and..."
- converted = markdown_convert_wrapper(msg)
- self.assertEqual(
- converted,
- f"""<p>The Google logo looks like this: <img alt="Google logo" data-original-src="{image_url}" src="{get_camo_url(image_url)}"> and...</p>""",
- )
-
- msg = '![foo](/url "the title")'
- converted = markdown_convert_wrapper(msg)
- self.assertEqual(
- converted,
- '<p><img alt="foo" data-original-src="/url" src="/url" title="the title"></p>',
- )
-
- msg = "![](/url)"
- converted = markdown_convert_wrapper(msg)
- self.assertEqual(
- converted,
- '<p><img alt="" data-original-src="/url" src="/url"></p>',
- )
-
def test_inline_image_preview(self) -> None:
url = "http://cdn.wallpapersafari.com/13/6/16eVjx.jpeg"
camo_url = get_camo_url(url)