diff --git a/web/src/compose_setup.js b/web/src/compose_setup.js index 7e1a283120..61d6c4ff49 100644 --- a/web/src/compose_setup.js +++ b/web/src/compose_setup.js @@ -34,7 +34,7 @@ import * as user_topics from "./user_topics"; export function abort_xhr() { $("#compose-send-button").prop("disabled", false); - upload.compose_upload_object.cancelAll(); + upload.compose_upload_cancel(); } function setup_compose_actions_hooks() { diff --git a/web/src/message_edit.js b/web/src/message_edit.js index c10554f8af..a6414e4271 100644 --- a/web/src/message_edit.js +++ b/web/src/message_edit.js @@ -577,11 +577,10 @@ function start_edit_with_content($row, content, edit_box_open_callback) { edit_box_open_callback(); } const row_id = rows.id($row); - const upload_object = upload.setup_upload({ + upload.setup_upload({ mode: "edit", row: row_id, }); - upload.upload_objects_by_message_edit_row.set(row_id, upload_object); } export function start($row, edit_box_open_callback) { @@ -791,25 +790,11 @@ export function end_inline_topic_edit($row) { message_lists.current.hide_edit_topic_on_recipient_row($row); } -function remove_uploads_from_row(row_id) { - const uploads_for_row = upload.upload_objects_by_message_edit_row.get(row_id); - if (!uploads_for_row) { - return; - } - // We need to cancel all uploads, reset their progress, - // and clear the files upon ending the edit. - upload.deactivate_upload(uploads_for_row, { - mode: "edit", - row: row_id, - }); - // Since we removed all the uploads from the row, we should - // now remove the corresponding upload object from the store. - upload.upload_objects_by_message_edit_row.delete(row_id); -} - export function end_message_row_edit($row) { const row_id = rows.id($row); - remove_uploads_from_row(row_id); + + // Clean up the upload handler + upload.deactivate_upload({mode: "edit", row: row_id}); const message = message_lists.current.get(row_id); if (message !== undefined && currently_editing_messages.has(message.id)) { diff --git a/web/src/upload.js b/web/src/upload.js index 6db89fb669..8afe34af37 100644 --- a/web/src/upload.js +++ b/web/src/upload.js @@ -4,6 +4,7 @@ import $ from "jquery"; import render_upload_banner from "../templates/compose_banner/upload_banner.hbs"; +import * as blueslip from "./blueslip"; import * as compose_actions from "./compose_actions"; import * as compose_banner from "./compose_banner"; import * as compose_reply from "./compose_reply"; @@ -17,8 +18,12 @@ import * as rows from "./rows"; import {realm} from "./state_data"; let drag_drop_img = null; -export let compose_upload_object; -export const upload_objects_by_message_edit_row = new Map(); +let compose_upload_object; +const upload_objects_by_message_edit_row = new Map(); + +export function compose_upload_cancel() { + compose_upload_object.cancelAll(); +} // Show the upload button only if the browser supports it. export function feature_check($upload_button) { @@ -292,6 +297,10 @@ export function setup_upload(config) { }, }); + if (config.mode === "edit") { + upload_objects_by_message_edit_row.set(config.row, uppy); + } + uppy.on("upload-progress", (file, progress) => { const percent_complete = (100 * progress.bytesUploaded) / progress.bytesTotal; $(`${get_item("upload_banner_identifier", config, file.id)} .moving_bar`).css({ @@ -437,16 +446,38 @@ export function setup_upload(config) { return uppy; } -export function deactivate_upload(uppy, config) { +export function deactivate_upload(config) { // Remove event listeners added for handling uploads. $(get_item("file_input_identifier", config)).off("change"); get_item("banner_container", config).off("click"); get_item("drag_drop_container", config).off("dragover dragenter drop paste"); - // Uninstall all plugins and close down the Uppy instance. - // Also runs uppy.cancelAll() before uninstalling - which - // cancels all uploads, resets progress and removes all files. - uppy.close(); + let uppy; + + if (config.mode === "edit") { + uppy = upload_objects_by_message_edit_row.get(config.row); + } else if (config.mode === "compose") { + uppy = compose_upload_object; + } + + if (!uppy) { + return; + } + + try { + // Uninstall all plugins and close down the Uppy instance. + // Also runs uppy.cancelAll() before uninstalling - which + // cancels all uploads, resets progress and removes all files. + uppy.close(); + } catch (error) { + blueslip.error("Failed to close upload object.", {config}, error); + } + + if (config.mode === "edit") { + // Since we removed all the uploads from the row, we should + // now remove the corresponding upload object from the store. + upload_objects_by_message_edit_row.delete(config.row); + } } export function initialize() { diff --git a/web/tests/compose.test.js b/web/tests/compose.test.js index 7b87ca1460..a75735711e 100644 --- a/web/tests/compose.test.js +++ b/web/tests/compose.test.js @@ -508,10 +508,8 @@ test_ui("initialize", ({override}) => { realm.max_file_upload_size_mib = 512; let uppy_cancel_all_called = false; - override(upload, "compose_upload_object", { - cancelAll() { - uppy_cancel_all_called = true; - }, + override(upload, "compose_upload_cancel", () => { + uppy_cancel_all_called = true; }); override(upload, "feature_check", noop);