2022-10-07 00:56:00 +02:00
|
|
|
import {Uppy} from "@uppy/core";
|
2021-02-28 00:52:20 +01:00
|
|
|
import XHRUpload from "@uppy/xhr-upload";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2023-01-09 07:43:39 +01:00
|
|
|
import render_upload_banner from "../templates/compose_banner/upload_banner.hbs";
|
|
|
|
|
2021-02-28 21:32:22 +01:00
|
|
|
import * as compose_actions from "./compose_actions";
|
2021-02-28 00:52:20 +01:00
|
|
|
import * as compose_state from "./compose_state";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-03-25 23:20:18 +01:00
|
|
|
import {csrf_token} from "./csrf";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2018-02-13 23:26:19 +01:00
|
|
|
// Show the upload button only if the browser supports it.
|
2022-01-25 11:36:19 +01:00
|
|
|
export function feature_check($upload_button) {
|
2021-03-05 17:27:53 +01:00
|
|
|
if (window.XMLHttpRequest && new window.XMLHttpRequest().upload) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.removeClass("notdisplayed");
|
2018-02-13 23:26:19 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function get_translated_status(file) {
|
2021-04-13 06:51:54 +02:00
|
|
|
const status = $t({defaultMessage: "Uploading {filename}…"}, {filename: file.name});
|
2020-04-17 17:37:06 +02:00
|
|
|
return "[" + status + "]()";
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2018-02-13 23:26:19 +01:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function get_item(key, config) {
|
2019-11-21 05:24:55 +01:00
|
|
|
if (!config) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Missing config");
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
if (config.mode === "compose") {
|
|
|
|
switch (key) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "textarea":
|
|
|
|
return $("#compose-textarea");
|
|
|
|
case "send_button":
|
|
|
|
return $("#compose-send-button");
|
2023-01-09 07:43:39 +01:00
|
|
|
case "banner_container":
|
|
|
|
return $("#compose_banners");
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_identifier":
|
2023-01-09 07:43:39 +01:00
|
|
|
return "#compose_banners .upload_banner";
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $("#compose_banners .upload_banner");
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_close_button":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $("#compose_banners .upload_banner .compose_banner_close_button");
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_message":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $("#compose_banners .upload_banner .upload_msg");
|
2020-07-15 02:14:03 +02:00
|
|
|
case "file_input_identifier":
|
2021-04-20 16:14:55 +02:00
|
|
|
return "#compose .file_input";
|
2020-07-15 02:14:03 +02:00
|
|
|
case "source":
|
|
|
|
return "compose-file-input";
|
|
|
|
case "drag_drop_container":
|
|
|
|
return $("#compose");
|
2020-09-04 21:10:48 +02:00
|
|
|
case "markdown_preview_hide_button":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $("#compose .undo_markdown_preview");
|
2020-07-15 02:14:03 +02:00
|
|
|
default:
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(`Invalid key name for mode "${config.mode}"`);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
} else if (config.mode === "edit") {
|
|
|
|
if (!config.row) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Missing row in config");
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
switch (key) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "textarea":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .message_edit_content`);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_button":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .message_edit_content`)
|
2021-04-21 16:20:16 +02:00
|
|
|
.closest(".message_edit_form")
|
2020-07-15 00:34:28 +02:00
|
|
|
.find(".message_edit_save");
|
2023-01-09 07:43:39 +01:00
|
|
|
case "banner_container":
|
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .banners`);
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_identifier":
|
2023-01-09 07:43:39 +01:00
|
|
|
return `#edit_form_${CSS.escape(config.row)} .upload_banner`;
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .upload_banner`);
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_close_button":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $(
|
|
|
|
`#edit_form_${CSS.escape(
|
|
|
|
config.row,
|
|
|
|
)} .upload_banner .compose_banner_close_button`,
|
2021-02-03 23:23:32 +01:00
|
|
|
);
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_message":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .upload_banner .upload_msg`);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "file_input_identifier":
|
2021-04-20 16:14:55 +02:00
|
|
|
return `#edit_form_${CSS.escape(config.row)} .file_input`;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "source":
|
|
|
|
return "message-edit-file-input";
|
|
|
|
case "drag_drop_container":
|
2021-10-29 13:59:36 +02:00
|
|
|
return $(`#zfilt${CSS.escape(config.row)} .message_edit_form`);
|
2020-09-04 21:10:48 +02:00
|
|
|
case "markdown_preview_hide_button":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .undo_markdown_preview`);
|
2020-07-15 02:14:03 +02:00
|
|
|
default:
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(`Invalid key name for mode "${config.mode}"`);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Invalid upload mode!");
|
2018-02-13 22:48:48 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2018-02-13 22:48:48 +01:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function hide_upload_status(config) {
|
|
|
|
get_item("send_button", config).prop("disabled", false);
|
2023-01-09 08:09:35 +01:00
|
|
|
get_item("upload_banner", config).remove();
|
2023-01-09 07:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function show_upload_banner(config, banner_type, banner_text) {
|
|
|
|
// We only show one upload banner at a time per compose box,
|
|
|
|
// and all uploads are combined into the same progress bar.
|
|
|
|
// TODO: It would be nice to separate the error banner into
|
|
|
|
// a different element, so that we can show it at the same
|
|
|
|
// time as the upload bar and other uploads can still continue
|
|
|
|
// when an error occurs.
|
2023-01-09 08:09:35 +01:00
|
|
|
const $upload_banner = get_item("upload_banner", config);
|
2023-01-09 07:43:39 +01:00
|
|
|
if ($upload_banner.length) {
|
|
|
|
if (banner_type === "error") {
|
|
|
|
// Hide moving bar so that it doesn't do the 1s transition to 0
|
2023-01-09 08:09:35 +01:00
|
|
|
const $moving_bar = $(`${get_item("upload_banner_identifier", config)} .moving_bar`);
|
2023-01-09 07:43:39 +01:00
|
|
|
$moving_bar.hide();
|
|
|
|
$upload_banner.removeClass("info").addClass("error");
|
|
|
|
// Show it again once the animation is complete.
|
|
|
|
setTimeout(() => $moving_bar.show(), 1000);
|
|
|
|
} else {
|
|
|
|
$upload_banner.removeClass("error").addClass("info");
|
|
|
|
}
|
2023-01-09 08:09:35 +01:00
|
|
|
get_item("upload_banner_message", config).text(banner_text);
|
2023-01-09 07:43:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-03-17 18:53:24 +01:00
|
|
|
const new_banner = render_upload_banner({
|
2023-01-09 07:43:39 +01:00
|
|
|
banner_type,
|
|
|
|
banner_text,
|
|
|
|
});
|
2023-03-17 18:53:24 +01:00
|
|
|
get_item("banner_container", config).append(new_banner);
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
export function show_error_message(
|
|
|
|
config,
|
|
|
|
message = $t({defaultMessage: "An unknown error occurred."}),
|
|
|
|
) {
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("send_button", config).prop("disabled", false);
|
2023-01-09 07:43:39 +01:00
|
|
|
show_upload_banner(config, "error", message);
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2023-01-09 07:43:39 +01:00
|
|
|
export async function upload_files(uppy, config, files) {
|
2019-11-21 05:24:55 +01:00
|
|
|
if (files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-08 06:57:19 +02:00
|
|
|
if (page_params.max_file_upload_size_mib === 0) {
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(
|
2020-07-15 00:34:28 +02:00
|
|
|
config,
|
2021-04-13 06:51:54 +02:00
|
|
|
$t({
|
|
|
|
defaultMessage: "File and image uploads have been disabled for this organization.",
|
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-09-04 21:10:48 +02:00
|
|
|
|
|
|
|
// If we're looking at a markdown preview, switch back to the edit
|
|
|
|
// UI. This is important for all the later logic around focus
|
|
|
|
// (etc.) to work correctly.
|
|
|
|
//
|
|
|
|
// We implement this transition through triggering a click on the
|
|
|
|
// toggle button to take advantage of the existing plumbing for
|
|
|
|
// handling the compose and edit UIs.
|
2021-02-28 00:52:20 +01:00
|
|
|
if (get_item("markdown_preview_hide_button", config).is(":visible")) {
|
|
|
|
get_item("markdown_preview_hide_button", config).trigger("click");
|
2020-09-04 21:10:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("send_button", config).prop("disabled", true);
|
2023-01-09 07:43:39 +01:00
|
|
|
show_upload_banner(config, "info", $t({defaultMessage: "Uploading…"}));
|
2023-01-09 08:09:35 +01:00
|
|
|
get_item("upload_banner_close_button", config).one("click", () => {
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const file of uppy.getFiles()) {
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.replace_syntax(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
2020-07-15 00:34:28 +02:00
|
|
|
"",
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("textarea", config),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy.cancelAll();
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("textarea", config).trigger("focus");
|
2023-01-09 07:06:16 +01:00
|
|
|
hide_upload_status(config);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
try {
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.insert_syntax_and_focus(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
|
|
|
get_item("textarea", config),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy.addFile({
|
2021-02-28 00:52:20 +01:00
|
|
|
source: get_item("source", config),
|
2019-11-21 05:24:55 +01:00
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
data: file,
|
|
|
|
});
|
2020-10-07 10:18:48 +02:00
|
|
|
} catch {
|
2019-11-21 05:24:55 +01:00
|
|
|
// Errors are handled by info-visible and upload-error event callbacks.
|
2020-04-16 17:11:06 +02:00
|
|
|
break;
|
2018-04-14 11:48:22 +02:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2018-04-14 11:48:22 +02:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function setup_upload(config) {
|
2020-12-11 04:26:23 +01:00
|
|
|
const uppy = new Uppy({
|
2019-11-21 05:24:55 +01:00
|
|
|
debug: false,
|
|
|
|
autoProceed: true,
|
|
|
|
restrictions: {
|
2020-05-08 06:57:19 +02:00
|
|
|
maxFileSize: page_params.max_file_upload_size_mib * 1024 * 1024,
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
locale: {
|
|
|
|
strings: {
|
2022-06-12 21:43:55 +02:00
|
|
|
exceedsSize: $t(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"%'{file}' exceeds the maximum file size for attachments ({variable} MB).",
|
|
|
|
},
|
|
|
|
{variable: `${page_params.max_file_upload_size_mib}`},
|
|
|
|
),
|
2021-04-13 06:51:54 +02:00
|
|
|
failedToUpload: $t({defaultMessage: "Failed to upload %'{file}'"}),
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
uppy.setMeta({
|
|
|
|
csrfmiddlewaretoken: csrf_token,
|
|
|
|
});
|
2020-07-15 00:34:28 +02:00
|
|
|
uppy.use(XHRUpload, {
|
|
|
|
endpoint: "/json/user_uploads",
|
|
|
|
formData: true,
|
|
|
|
fieldName: "file",
|
|
|
|
// Number of concurrent uploads
|
|
|
|
limit: 5,
|
|
|
|
locale: {
|
|
|
|
strings: {
|
2021-04-13 06:51:54 +02:00
|
|
|
timedOut: $t({
|
|
|
|
defaultMessage: "Upload stalled for %'{seconds}' seconds, aborting.",
|
|
|
|
}),
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2020-07-15 00:34:28 +02:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2023-01-09 07:06:16 +01:00
|
|
|
uppy.on("progress", (progress) => {
|
|
|
|
// When upload is complete, it resets to 0, but we want to see it at 100%.
|
|
|
|
if (progress === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-09 08:09:35 +01:00
|
|
|
$(`${get_item("upload_banner_identifier", config)} .moving_bar`).css({
|
2023-01-09 07:06:16 +01:00
|
|
|
width: `${progress}%`,
|
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
$("body").on("change", get_item("file_input_identifier", config), (event) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const files = event.target.files;
|
2021-02-28 00:52:20 +01:00
|
|
|
upload_files(uppy, config, files);
|
2022-11-22 11:16:58 +01:00
|
|
|
get_item("textarea", config).trigger("focus");
|
2020-04-09 20:19:37 +02:00
|
|
|
event.target.value = "";
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $drag_drop_container = get_item("drag_drop_container", config);
|
|
|
|
$drag_drop_container.on("dragover", (event) => event.preventDefault());
|
|
|
|
$drag_drop_container.on("dragenter", (event) => event.preventDefault());
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$drag_drop_container.on("drop", (event) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
event.preventDefault();
|
|
|
|
const files = event.originalEvent.dataTransfer.files;
|
2021-02-28 00:52:20 +01:00
|
|
|
upload_files(uppy, config, files);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$drag_drop_container.on("paste", (event) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const clipboard_data = event.clipboardData || event.originalEvent.clipboardData;
|
|
|
|
if (!clipboard_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const items = clipboard_data.items;
|
|
|
|
const files = [];
|
|
|
|
for (const item of items) {
|
|
|
|
if (item.kind !== "file") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const file = item.getAsFile();
|
|
|
|
files.push(file);
|
2018-02-13 22:39:51 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
upload_files(uppy, config, files);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
2017-11-23 15:00:05 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("upload-success", (file, response) => {
|
2023-04-09 05:22:23 +02:00
|
|
|
const url = response.body.uri;
|
|
|
|
if (url === undefined) {
|
2018-02-13 22:39:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-04-09 05:22:23 +02:00
|
|
|
const split_url = url.split("/");
|
|
|
|
const filename = split_url.at(-1);
|
2020-07-23 10:20:10 +02:00
|
|
|
if (config.mode === "compose" && !compose_state.composing()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_actions.start("stream");
|
2018-02-13 22:39:51 +01:00
|
|
|
}
|
2023-04-09 05:22:23 +02:00
|
|
|
const filename_url = "[" + filename + "](" + url + ")";
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.replace_syntax(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
2023-04-09 05:22:23 +02:00
|
|
|
filename_url,
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("textarea", config),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
2018-04-05 04:29:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("complete", () => {
|
2020-04-14 16:16:49 +02:00
|
|
|
let uploads_in_progress = false;
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const file of uppy.getFiles()) {
|
2020-04-14 16:16:49 +02:00
|
|
|
if (file.progress.uploadComplete) {
|
|
|
|
// The uploaded files should be removed since uppy don't allow files in the store
|
|
|
|
// to be re-uploaded again.
|
|
|
|
uppy.removeFile(file.id);
|
|
|
|
} else {
|
|
|
|
// Happens when user tries to upload files when there is already an existing batch
|
|
|
|
// being uploaded. So when the first batch of files complete, the second batch would
|
|
|
|
// still be in progress.
|
|
|
|
uploads_in_progress = true;
|
|
|
|
}
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2020-04-14 16:16:49 +02:00
|
|
|
|
2023-01-09 08:09:35 +01:00
|
|
|
const has_errors = get_item("upload_banner", config).hasClass("error");
|
2020-04-16 18:22:43 +02:00
|
|
|
if (!uploads_in_progress && !has_errors) {
|
2023-01-09 07:06:16 +01:00
|
|
|
// Hide upload status for 100ms after the 1s transition to 100%
|
|
|
|
// so that the user can see the progress bar at 100%.
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2021-02-28 00:52:20 +01:00
|
|
|
hide_upload_status(config);
|
2023-01-09 07:06:16 +01:00
|
|
|
}, 1100);
|
2020-04-14 16:16:49 +02:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
2017-11-23 15:00:05 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("info-visible", () => {
|
2022-06-12 21:43:55 +02:00
|
|
|
// Uppy's `info-visible` event is issued after prepending the
|
|
|
|
// notice details into the list of event events accessed via
|
|
|
|
// uppy.getState().info. Extract the notice details so that we
|
|
|
|
// can potentially act on the error.
|
|
|
|
//
|
|
|
|
// TODO: Ideally, we'd be using the `.error()` hook or
|
|
|
|
// something, not parsing error message strings.
|
|
|
|
const info = uppy.getState().info[0];
|
2019-11-21 05:24:55 +01:00
|
|
|
if (info.type === "error" && info.message === "No Internet connection") {
|
|
|
|
// server_events already handles the case of no internet.
|
|
|
|
return;
|
2018-02-13 23:29:17 +01:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
if (info.type === "error" && info.details === "Upload Error") {
|
|
|
|
// The server errors come under 'Upload Error'. But we can't handle them
|
|
|
|
// here because info object don't contain response.body.msg received from
|
|
|
|
// the server. Server errors are hence handled by on('upload-error').
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.type === "error") {
|
|
|
|
// The remaining errors are mostly frontend errors like file being too large
|
|
|
|
// for upload.
|
2023-01-09 07:43:39 +01:00
|
|
|
// TODO: It would be nice to keep the other uploads going if one fails,
|
|
|
|
// and show both an error message and the upload bar.
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy.cancelAll();
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(config, info.message);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("upload-error", (file, error, response) => {
|
2021-03-24 21:44:43 +01:00
|
|
|
const message = response ? response.body.msg : undefined;
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy.cancelAll();
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(config, message);
|
|
|
|
compose_ui.replace_syntax(get_translated_status(file), "", get_item("textarea", config));
|
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("restriction-failed", (file) => {
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.replace_syntax(get_translated_status(file), "", get_item("textarea", config));
|
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2020-03-07 12:44:17 +01:00
|
|
|
});
|
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
return uppy;
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|