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.

markdown: Insert `![alt text](url)` in messagebox for uploaded images.

Rohan Gudimetlaccabbc376ce6
diff --git a/web/src/upload.ts b/web/src/upload.ts
index 1c668634e3c..8d179744cb7 100644
--- a/web/src/upload.ts
+++ b/web/src/upload.ts
@@ -29,6 +29,24 @@ let drag_drop_img: HTMLElement | null = null;
let compose_upload_object: Uppy<ZulipMeta, TusBody>;
const upload_objects_by_message_edit_row = new Map<number, Uppy<ZulipMeta, TusBody>>();
+// This list should be kept identical to the one defined as
+// THUMBNAIL_ACCEPT_IMAGE_TYPES in zerver/lib/thumbnail.py
+// "Supported" in this context means _by the server_ -- uploaded
+// images are transcoded into more widely supported formats by the server.
+export const SUPPORTED_IMAGE_TYPES = new Set([
+ "image/avif",
+ "image/gif",
+ "image/heic",
+ "image/jpeg",
+ "image/png",
+ "image/tiff",
+ "image/webp",
+]);
+
+export function is_supported_image_type(file_type: string): boolean {
+ return SUPPORTED_IMAGE_TYPES.has(file_type);
+}
+
export function compose_upload_cancel(): void {
compose_upload_object.cancelAll();
}
@@ -587,7 +605,11 @@ export function setup_upload(config: Config): Uppy<ZulipMeta, TusBody> {
}
const filtered_filename = file.name.replaceAll("[", "").replaceAll("]", "");
- const syntax_to_insert = "[" + filtered_filename + "](" + file.meta.zulip_url + ")";
+ let syntax_to_insert = "[" + filtered_filename + "](" + file.meta.zulip_url + ")";
+ if (is_supported_image_type(file.type)) {
+ syntax_to_insert = "!" + syntax_to_insert;
+ }
+
const $text_area = config.textarea();
const replacement_successful = compose_ui.replace_syntax(
// We need to replace the original file name, and not the
diff --git a/web/src/upload_widget.ts b/web/src/upload_widget.ts
index bb8da2bdd1e..384f9636039 100644
--- a/web/src/upload_widget.ts
+++ b/web/src/upload_widget.ts
@@ -9,6 +9,7 @@ import render_image_editor_modal from "../templates/image_editor_modal.hbs";
import * as blueslip from "./blueslip.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t, $t_html} from "./i18n.ts";
+import {SUPPORTED_IMAGE_TYPES, is_supported_image_type} from "./upload.ts";
import * as util from "./util.ts";
export type UploadWidget = {
@@ -22,27 +23,6 @@ const default_max_file_size = 5;
let uppy_widget: Uppy<Meta, Body> | undefined;
-// These formats do not need to be universally understood by clients; they are all
-// converted, server-side, currently to PNGs. This list should be kept in sync with
-// the THUMBNAIL_ACCEPT_IMAGE_TYPES in zerver/lib/thumbnail.py
-const supported_types = [
- "image/avif",
- "image/gif",
- "image/heic",
- "image/jpeg",
- "image/png",
- "image/tiff",
- "image/webp",
-];
-
-function is_image_format(file: File): boolean {
- const type = file.type;
- if (!type) {
- return false;
- }
- return supported_types.includes(type);
-}
-
export function build_widget(
// function returns a jQuery file input object
get_file_input: () => JQuery<HTMLInputElement>,
@@ -97,7 +77,7 @@ export function build_widget(
return false;
});
- get_file_input().attr("accept", supported_types.toString());
+ get_file_input().attr("accept", [...SUPPORTED_IMAGE_TYPES].toString());
get_file_input().on("change", (e) => {
if (e.target.files?.[0] === undefined) {
$input_error.hide();
@@ -112,7 +92,7 @@ export function build_widget(
);
$input_error.show();
clear();
- } else if (!is_image_format(file)) {
+ } else if (!is_supported_image_type(file.type)) {
$input_error.text($t({defaultMessage: "File type is not supported."}));
$input_error.show();
clear();
@@ -162,7 +142,7 @@ function ensure_file(resized_img: File | Blob, original_file: File): File {
function set_up_uppy_widget(property_name: "realm_icon" | "realm_logo" | "user_avatar"): void {
uppy_widget = new Uppy<Meta, Body>({
restrictions: {
- allowedFileTypes: supported_types,
+ allowedFileTypes: [...SUPPORTED_IMAGE_TYPES],
maxNumberOfFiles: 1,
},
}).use(ImageEditor, {
@@ -320,7 +300,7 @@ export function build_direct_upload_widget(
return false;
});
- get_file_input().attr("accept", supported_types.toString());
+ get_file_input().attr("accept", [...SUPPORTED_IMAGE_TYPES].toString());
get_file_input().on("change", (e) => {
if (e.target.files?.[0] === undefined) {
$input_error.hide();
@@ -335,7 +315,7 @@ export function build_direct_upload_widget(
);
$input_error.show();
clear();
- } else if (!is_image_format(file)) {
+ } else if (!is_supported_image_type(file.type)) {
$input_error.text($t({defaultMessage: "File type is not supported."}));
$input_error.show();
clear();
diff --git a/web/tests/upload.test.cjs b/web/tests/upload.test.cjs
index e66ed0e4182..1c9746a064b 100644
--- a/web/tests/upload.test.cjs
+++ b/web/tests/upload.test.cjs
@@ -458,6 +458,7 @@ test("uppy_events", ({override_rewire, mock_template}) => {
let state = {};
const file = {
name: "copenhagen.png",
+ type: "image/png",
meta: {
name: "copenhagen.png",
},
@@ -485,6 +486,7 @@ test("uppy_events", ({override_rewire, mock_template}) => {
return {
...file,
name: "modified-name-copenhagen.png",
+ type: "image/png",
meta: {
...file.meta,
zulip_url: "/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png",
@@ -535,7 +537,7 @@ test("uppy_events", ({override_rewire, mock_template}) => {
assert.equal(old_syntax, "[translated: Uploading copenhagen.png…]()");
assert.equal(
new_syntax,
- "[modified-name-copenhagen.png](/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png)",
+ "![modified-name-copenhagen.png](/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png)",
);
assert.equal($textarea, $("textarea#compose-textarea"));
});
diff --git a/zerver/lib/thumbnail.py b/zerver/lib/thumbnail.py
index 2994d3e090e..3861de8344a 100644
--- a/zerver/lib/thumbnail.py
+++ b/zerver/lib/thumbnail.py
@@ -104,8 +104,8 @@ TRANSCODED_IMAGE_FORMAT = ThumbnailFormat("webp", 4032, 3024, animated=False)
# this does not provide any *security*, since the content-type is
# provided by the browser, and may not match the bytes they uploaded.
#
-# This should be kept synced with the client-side image-picker in
-# web/upload_widget.ts. Any additions below must be accompanied by
+# This should be kept synced with the client-side list in
+# web/src/upload.ts. Any additions below must be accompanied by
# changes to the pyvips block below as well.
THUMBNAIL_ACCEPT_IMAGE_TYPES = frozenset(
[