Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow cancelling a composer upload while it is processing (#45069)

Renato Atilio02792d22d9ab
diff --git a/frontend/discourse/app/components/docked-composer.gjs b/frontend/discourse/app/components/docked-composer.gjs
index 1e03e6a95cb..5c8bb8af0e1 100644
--- a/frontend/discourse/app/components/docked-composer.gjs
+++ b/frontend/discourse/app/components/docked-composer.gjs
@@ -247,6 +247,7 @@ export default class DockedComposer extends Component {
this.uppyUpload.uppyWrapper.useUploadPlugin(UppyMediaOptimization, {
optimizeFn: (data, opts) =>
this.mediaOptimizationWorker.optimizeImage(data, opts),
+ cancelFn: (fileId) => this.mediaOptimizationWorker.cancel(fileId),
runParallel: !this.capabilities.isMobileDevice,
});
}
diff --git a/frontend/discourse/app/instance-initializers/register-media-optimization-upload-processor.js b/frontend/discourse/app/instance-initializers/register-media-optimization-upload-processor.js
index 392643acab0..a566a16c648 100644
--- a/frontend/discourse/app/instance-initializers/register-media-optimization-upload-processor.js
+++ b/frontend/discourse/app/instance-initializers/register-media-optimization-upload-processor.js
@@ -36,6 +36,13 @@ export default {
.lookup("service:media-optimization-worker")
.optimizeImage(data, opts);
},
+ cancelFn: (fileId) => {
+ if (!owner.isDestroying) {
+ owner
+ .lookup("service:media-optimization-worker")
+ .cancel(fileId);
+ }
+ },
runParallel: !isMobileDevice,
};
}
diff --git a/frontend/discourse/app/lib/textarea-text-manipulation.ts b/frontend/discourse/app/lib/textarea-text-manipulation.ts
index c7e829efc6f..daa68bfc503 100644
--- a/frontend/discourse/app/lib/textarea-text-manipulation.ts
+++ b/frontend/discourse/app/lib/textarea-text-manipulation.ts
@@ -1349,30 +1349,46 @@ class TextareaPlaceholderHandler implements PlaceholderHandler {
placeholderData.processingPlaceholder,
placeholderData.uploadPlaceholder
);
+ placeholderData.processingPlaceholder = undefined;
}
cancelAll(): void {
- Object.values(this.#placeholders).forEach((data) => {
- this.textManipulation.replaceText(data.uploadPlaceholder, "");
- });
+ Object.values(this.#placeholders).forEach((data) =>
+ this.#removePlaceholder(data)
+ );
+ this.#placeholders = {};
}
cancel(file: UppyFile): void {
- if (this.#placeholders[file.id]) {
- this.textManipulation.replaceText(
- this.#placeholders[file.id].uploadPlaceholder,
- ""
- );
+ const placeholderData = this.#placeholders[file.id];
+ if (placeholderData) {
+ this.#removePlaceholder(placeholderData);
+ delete this.#placeholders[file.id];
}
}
success(file: UppyFile, markdown: string): void {
+ const placeholderData = this.#placeholders[file.id];
+ if (!placeholderData) {
+ return;
+ }
+
this.textManipulation.replaceText(
- this.#placeholders[file.id].uploadPlaceholder.trim(),
+ placeholderData.uploadPlaceholder.trim(),
markdown
);
}
+ #removePlaceholder(placeholderData: PlaceholderData): void {
+ if (placeholderData.processingPlaceholder !== undefined) {
+ this.textManipulation.replaceText(
+ placeholderData.processingPlaceholder,
+ ""
+ );
+ }
+ this.textManipulation.replaceText(placeholderData.uploadPlaceholder, "");
+ }
+
#uploadPlaceholder(file: UppyFile, currentMarkdown: string): string {
const clipboard = i18n("clipboard");
const uploadFilenamePlaceholder = this.#uploadFilenamePlaceholder(
diff --git a/frontend/discourse/app/lib/uppy-media-optimization-plugin.js b/frontend/discourse/app/lib/uppy-media-optimization-plugin.js
index d9ecfa1eed7..596475e11b3 100644
--- a/frontend/discourse/app/lib/uppy-media-optimization-plugin.js
+++ b/frontend/discourse/app/lib/uppy-media-optimization-plugin.js
@@ -5,9 +5,12 @@ import { UploadPreProcessorPlugin } from "discourse/lib/uppy-plugin-base";
export default class UppyMediaOptimization extends UploadPreProcessorPlugin {
static pluginId = "uppy-media-optimization";
+ #settleRemoved = new Map();
+
constructor(uppy, opts) {
super(uppy, opts);
this.optimizeFn = opts.optimizeFn;
+ this.cancelFn = opts.cancelFn;
// mobile devices have limited processing power, so we only enable
// running media optimization in parallel when we are sure the user
@@ -22,6 +25,7 @@ export default class UppyMediaOptimization extends UploadPreProcessorPlugin {
} else {
this._install(this._optimizeSerial);
}
+ this.uppy.on("file-removed", this._onFileRemoved);
}
uninstall() {
@@ -30,16 +34,38 @@ export default class UppyMediaOptimization extends UploadPreProcessorPlugin {
} else {
this._uninstall(this._optimizeSerial);
}
+ this.uppy.off("file-removed", this._onFileRemoved);
+ }
+
+ // Don't keep the rest of the batch waiting on a file that was cancelled.
+ @bind
+ _onFileRemoved(file) {
+ this.#settleRemoved.get(file.id)?.();
+ this.cancelFn?.(file.id);
}
@bind
_optimizeFile(fileId) {
let file = this._getFile(fileId);
+ if (!file) {
+ return Promise.resolve();
+ }
this._emitProgress(file);
- return this.optimizeFn(file, { stopWorkerOnError: !this.runParallel })
+ const removed = new Promise((resolve) =>
+ this.#settleRemoved.set(fileId, resolve)
+ );
+
+ return Promise.race([
+ this.optimizeFn(file, { stopWorkerOnError: !this.runParallel }),
+ removed,
+ ])
.then((optimizedFile) => {
+ if (!this._getFile(fileId)) {
+ return;
+ }
+
let skipped = false;
if (!optimizedFile) {
this._consoleWarn(
@@ -57,7 +83,8 @@ export default class UppyMediaOptimization extends UploadPreProcessorPlugin {
.catch((err) => {
this._consoleWarn(err);
this._emitComplete(file);
- });
+ })
+ .finally(() => this.#settleRemoved.delete(fileId));
}
@bind
diff --git a/frontend/discourse/app/lib/uppy/composer-upload.js b/frontend/discourse/app/lib/uppy/composer-upload.js
index 21fe8294e35..27e24af8519 100644
--- a/frontend/discourse/app/lib/uppy/composer-upload.js
+++ b/frontend/discourse/app/lib/uppy/composer-upload.js
@@ -305,11 +305,9 @@ export default class UppyComposerUpload {
this.uppyWrapper.uppyInstance.on("upload", (uploadId, files) => {
run(() => {
- this.uppyWrapper.addNeedProcessing(files.length);
-
this.composer.setProperties({
isProcessingUpload: true,
- isCancellable: false,
+ isCancellable: true,
});
files.forEach((file) => {
diff --git a/frontend/discourse/app/lib/uppy/uppy-upload.js b/frontend/discourse/app/lib/uppy/uppy-upload.js
index 24ad85e8b46..b8df6e15718 100644
--- a/frontend/discourse/app/lib/uppy/uppy-upload.js
+++ b/frontend/discourse/app/lib/uppy/uppy-upload.js
@@ -270,7 +270,6 @@ export default class UppyUpload {
});
this.uppyWrapper.uppyInstance.on("upload", (uploadId, files) => {
- this.uppyWrapper.addNeedProcessing(files.length);
this.processing = true;
this.cancellable = false;
files.forEach((file) => {
diff --git a/frontend/discourse/app/lib/uppy/wrapper.js b/frontend/discourse/app/lib/uppy/wrapper.js
index e43a69eb7f8..ac02cb76325 100644
--- a/frontend/discourse/app/lib/uppy/wrapper.js
+++ b/frontend/discourse/app/lib/uppy/wrapper.js
@@ -1,4 +1,5 @@
import { setOwner } from "@ember/owner";
+import deprecated from "discourse/lib/deprecated";
import UppyUploadDebugging from "./upload-debugging.js";
/**
@@ -10,20 +11,10 @@ import UppyUploadDebugging from "./upload-debugging.js";
* - UppyMediaOptimization
* - UppyChecksum
*
- * Once installed with useUploadPlugin(PluginClass, opts), we track the following
- * status for every preprocessor plugin:
- *
- * - needProcessing - The total number of files that have been added to uppy that
- * will need to be run through the preprocessor plugins.
- * - activeProcessing - The number of files that are currently being processed,
- * which is determined by the preprocess-progress event.
- * - completeProcessing - The number of files that have completed being processed,
- * which is determined by the preprocess-complete event.
- * - allComplete - Whether all files have completed the preprocessing for the plugin.
- *
- * There is a caveat - you must call addNeedProcessing(data.fileIDs.length) when
- * handling the "upload" event with uppy, otherwise this mixin does not know how
- * many files need to be processed.
+ * Once installed with useUploadPlugin(PluginClass, opts), we track, for every
+ * preprocessor plugin, the files that still need to be run through it. Files
+ * are picked up from uppy's "upload" event; files removed from uppy
+ * mid-preprocessing stop being waited on, and late events for them are ignored.
*
* If you need to do something else on progress or completion of preprocessors,
* hook into the onPreProcessProgress(callback) or onPreProcessComplete(callback, allCompleteCallback)
@@ -31,11 +22,6 @@ import UppyUploadDebugging from "./upload-debugging.js";
* that is fired only when _all_ of the files have been preprocessed for all
* preprocessor plugins.
*
- * A preprocessor is considered complete if the completeProcessing count is
- * equal to needProcessing, at which point the allComplete prop is set to true.
- * If all preprocessor plugins have allComplete set to true, then the allCompleteCallback
- * is called for onPreProcessComplete.
- *
* To completely reset the preprocessor state for all plugins, call resetPreProcessors.
*
* See ComposerUploadUppy for an example of a component using this class.
@@ -43,7 +29,11 @@ import UppyUploadDebugging from "./upload-debugging.js";
export default class UppyWrapper {
debug;
uppyInstance;
+
+ /** @type {Record<string, Set<string>>} pending file ids per preprocessor */
#preProcessorStatus = {};
+ #allCompleteCallbacks = [];
+ #trackedInstance = null;
constructor(owner) {
setOwner(this, owner);
@@ -55,6 +45,8 @@ export default class UppyWrapper {
return;
}
+ this.#trackUploads();
+
if (!pluginClass.pluginId) {
throw new Error(
"The uppy plugin should have a static pluginId that is used to uniquely identify it."
@@ -79,28 +71,40 @@ export default class UppyWrapper {
);
if (pluginClass.pluginType === "preprocessor") {
- this.#trackPreProcessorStatus(pluginClass.pluginId);
+ this.#preProcessorStatus[pluginClass.pluginId] = new Set();
}
}
- // NOTE: This and _onPreProcessComplete will need to be tweaked
+ // NOTE: This and onPreProcessComplete will need to be tweaked
// if we ever add support for "determinate" preprocessors for uppy, which
// means the progress will have a value rather than a started/complete
// state ("indeterminate").
//
// See: https://uppy.io/docs/writing-plugins/#Progress-events
onPreProcessProgress(callback) {
+ this.#trackUploads();
this.uppyInstance.on("preprocess-progress", (file, progress, pluginId) => {
- this.debug.log(`[${pluginId}] processing file ${file.name} (${file.id})`);
+ if (!this.#isPending(pluginId, file)) {
+ return;
+ }
- this.#preProcessorStatus[pluginId].activeProcessing++;
+ this.debug.log(`[${pluginId}] processing file ${file.name} (${file.id})`);
callback(file);
});
}
onPreProcessComplete(callback, allCompleteCallback = null) {
+ this.#trackUploads();
+ if (allCompleteCallback) {
+ this.#allCompleteCallbacks.push(allCompleteCallback);
+ }
+
this.uppyInstance.on("preprocess-complete", (file, skipped, pluginId) => {
+ if (!this.#isPending(pluginId, file)) {
+ return;
+ }
+
this.debug.log(
`[${pluginId}] ${skipped ? "skipped" : "completed"} processing file ${
file.name
@@ -109,76 +113,73 @@ export default class UppyWrapper {
callback(file);
- this.#completePreProcessing(pluginId, (allComplete) => {
- if (allComplete) {
- this.debug.log("[uppy] All upload preprocessors complete!");
- if (allCompleteCallback) {
- allCompleteCallback();
- }
- }
- });
+ this.#preProcessorStatus[pluginId].delete(file.id);
+ this.#notifyIfAllComplete();
});
}
resetPreProcessors() {
this.#eachPreProcessor((pluginId) => {
- this.#preProcessorStatus[pluginId] = {
- needProcessing: 0,
- activeProcessing: 0,
- completeProcessing: 0,
- allComplete: false,
- };
+ this.#preProcessorStatus[pluginId] = new Set();
});
}
- addNeedProcessing(fileCount) {
- this.#eachPreProcessor((pluginName, status) => {
- status.needProcessing += fileCount;
- status.allComplete = false;
- });
+ addNeedProcessing() {
+ deprecated(
+ "`UppyWrapper#addNeedProcessing` is no longer needed; files are tracked from uppy's `upload` event.",
+ { id: "discourse.uppy-wrapper-add-need-processing", since: "2026.10.0" }
+ );
}
- #trackPreProcessorStatus(pluginId) {
- this.#preProcessorStatus[pluginId] = {
- needProcessing: 0,
- activeProcessing: 0,
- completeProcessing: 0,
- allComplete: false,
- };
+ // The wrapper outlives its Uppy instance, which is rebuilt whenever the
+ // editor is set up again, so tracking starts over for each instance.
+ #trackUploads() {
+ if (this.#trackedInstance === this.uppyInstance) {
+ return;
+ }
+ this.#trackedInstance = this.uppyInstance;
+ this.#preProcessorStatus = {};
+ this.#allCompleteCallbacks = [];
+
+ this.uppyInstance.on("upload", (uploadId, files) => {
+ this.#eachPreProcessor((pluginId, pendingFileIds) => {
+ files.forEach((file) => pendingFileIds.add(file.id));
+ });
+ });
+
+ this.uppyInstance.on("file-removed", (file) => {
+ let wasPending = false;
+ this.#eachPreProcessor((pluginId, pendingFileIds) => {
+ wasPending = pendingFileIds.delete(file.id) || wasPending;
+ });
+
+ // Removing the last file is a cancel-all, which consumers reset on themselves
+ if (wasPending && this.uppyInstance.getFiles().length > 0) {
+ this.#notifyIfAllComplete();
+ }
+ });
}
#eachPreProcessor(cb) {
- for (const [pluginId, status] of Object.entries(this.#preProcessorStatus)) {
- cb(pluginId, status);
+ for (const [pluginId, pendingFileIds] of Object.entries(
+ this.#preProcessorStatus
+ )) {
+ cb(pluginId, pendingFileIds);
}
}
- #allPreprocessorsComplete() {
- let completed = [];
- this.#eachPreProcessor((pluginId, status) => {
- completed.push(status.allComplete);
- });
- return completed.every(Boolean);
+ #isPending(pluginId, file) {
+ return !!file && !!this.#preProcessorStatus[pluginId]?.has(file.id);
}
- #completePreProcessing(pluginId, callback) {
- const preProcessorStatus = this.#preProcessorStatus[pluginId];
- preProcessorStatus.activeProcessing--;
- preProcessorStatus.completeProcessing++;
+ #notifyIfAllComplete() {
+ const allComplete = Object.values(this.#preProcessorStatus).every(
+ (pendingFileIds) => pendingFileIds.size === 0
+ );
- if (
- preProcessorStatus.completeProcessing ===
- preProcessorStatus.needProcessing
- ) {
- preProcessorStatus.allComplete = true;
- preProcessorStatus.needProcessing = 0;
- preProcessorStatus.completeProcessing = 0;
-
- if (this.#allPreprocessorsComplete()) {
- callback(true);
- } else {
- callback(false);
- }
+ if (allComplete) {
+ this.debug.log("[uppy] All upload preprocessors complete!");
+ this.#allCompleteCallbacks.forEach((callback) => callback());
}
}
}
diff --git a/frontend/discourse/app/services/media-optimization-worker.js b/frontend/discourse/app/services/media-optimization-worker.js
index 879372959bd..217080aa1c7 100644
--- a/frontend/discourse/app/services/media-optimization-worker.js
+++ b/frontend/discourse/app/services/media-optimization-worker.js
@@ -42,6 +42,13 @@ export default class MediaOptimizationWorkerService extends Service {
workerDoneCount = 0;
workerPendingCount = 0;
+ #stopOnComposerClose = false;
+
+ willDestroy() {
+ super.willDestroy(...arguments);
+ this.stopWorker();
+ }
+
async optimizeImage(data, opts = {}) {
this.promiseResolvers = this.promiseResolvers || {};
this.stopWorkerOnError = opts.hasOwnProperty("stopWorkerOnError")
@@ -102,6 +109,7 @@ export default class MediaOptimizationWorkerService extends Service {
this.currentComposerUploadData = data;
this.promiseResolvers[file.id] = resolve;
+ this.workerPendingCount++;
const settings = {
resize_threshold:
@@ -127,8 +135,18 @@ export default class MediaOptimizationWorkerService extends Service {
try {
arrayBuffer = await file.data.arrayBuffer();
} catch (error) {
+ if (this.promiseResolvers[file.id] !== resolve) {
+ return;
+ }
+
this.logIfDebug(error);
- return resolve();
+ this.workerPendingCount--;
+ return this.#settle(file.id);
+ }
+
+ // The worker may have been stopped, or this file cancelled, while reading it
+ if (this.promiseResolvers[file.id] !== resolve) {
+ return;
}
if (isConvertFormat || isOptimizable) {
@@ -157,8 +175,6 @@ export default class MediaOptimizationWorkerService extends Service {
[arrayBuffer]
);
}
-
- this.workerPendingCount++;
});
}
@@ -189,18 +205,37 @@ export default class MediaOptimizationWorkerService extends Service {
}
this.registerMessageHandler();
this.worker.onerror = (err) => this.handleWorkerError(err);
- this.appEvents.on("composer:closed", this, "stopWorker");
+
+ if (!this.#stopOnComposerClose) {
+ this.#stopOnComposerClose = true;
+ this.appEvents.on("composer:closed", this, "stopWorker");
+ }
}
handleWorkerError(err) {
this.logIfDebug("media-optimization-worker error", err);
this.stopWorker();
- // Resolve any in-flight optimizations so their uploads continue unoptimized.
- Object.values(this.promiseResolvers ?? {}).forEach((resolve) =>
- resolve?.()
- );
}
+ /**
+ * Stops waiting on a file, e.g. because its upload was cancelled. With nothing
+ * else pending the worker is replaced, so a retry doesn't queue behind a job
+ * that may be stuck.
+ */
+ cancel(fileId) {
+ if (!this.promiseResolvers?.[fileId]) {
+ return;
+ }
+
+ this.#settle(fileId);
+ this.workerPendingCount = Math.max(this.workerPendingCount - 1, 0);
+
+ if (this.workerPendingCount === 0) {
+ this.stopWorker();
+ }
+ }
+
+ // Also settles any in-flight optimizations, so their uploads continue unoptimized.
stopWorker() {
if (this.worker) {
this.logIfDebug("Stopping media-optimization-worker...");
@@ -209,10 +244,18 @@ export default class MediaOptimizationWorkerService extends Service {
this.workerDoneCount = 0;
}
this.workerPendingCount = 0;
+
+ Object.keys(this.promiseResolvers ?? {}).forEach((fileId) =>
+ this.#settle(fileId)
+ );
}
registerMessageHandler() {
this.worker.onmessage = (workerMessage) => {
+ if (!this.promiseResolvers?.[workerMessage.data.fileId]) {
+ return;
+ }
+
switch (workerMessage.data.type) {
case "file":
const outputType = workerMessage.data.outputType;
@@ -233,13 +276,13 @@ export default class MediaOptimizationWorkerService extends Service {
`Finished optimization of ${optimizedFile.name}, new size is ${optimizedFile.size} bytes`
);
- this.promiseResolvers[workerMessage.data.fileId](optimizedFile);
+ this.#settle(workerMessage.data.fileId, optimizedFile);
} else {
this.logIfDebug(
`Optimizing ${workerMessage.data.fileName} would produce ${outputFileName}, which is not an authorized extension here; keeping the original`
);
- this.promiseResolvers[workerMessage.data.fileId]();
+ this.#settle(workerMessage.data.fileId);
}
this.workerDoneCount++;
@@ -255,19 +298,18 @@ export default class MediaOptimizationWorkerService extends Service {
`Handling error message from image optimization for ${workerMessage.data.fileName}`
);
+ this.workerPendingCount--;
+ this.#settle(workerMessage.data.fileId);
if (this.stopWorkerOnError) {
this.stopWorker();
}
-
- this.promiseResolvers[workerMessage.data.fileId]();
- this.workerPendingCount--;
break;
case "skipped":
this.logIfDebug(
`Conversion skipped for ${workerMessage.data.fileName}, keeping the original`
);
- this.promiseResolvers[workerMessage.data.fileId]();
+ this.#settle(workerMessage.data.fileId);
this.workerDoneCount++;
this.workerPendingCount--;
break;
@@ -284,6 +326,11 @@ export default class MediaOptimizationWorkerService extends Service {
}
}
+ #settle(fileId, optimizedFile) {
+ this.promiseResolvers?.[fileId]?.(optimizedFile);
+ delete this.promiseResolvers?.[fileId];
+ }
+
_renameForOutputType(fileName, outputType) {
const baseName = fileName.replace(/\.[^.]+$/, "");
switch (outputType) {
diff --git a/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js b/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js
index f11542d2511..4a78941c4e2 100644
--- a/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js
+++ b/frontend/discourse/tests/acceptance/composer-uploads-uppy-test.js
@@ -10,12 +10,14 @@ import {
triggerKeyEvent,
visit,
waitFor,
+ waitUntil,
} from "@ember/test-helpers";
import { test } from "qunit";
import { Promise } from "rsvp";
import sinon from "sinon";
import { withPluginApi } from "discourse/lib/plugin-api";
import { authorizedExtensions, dialog } from "discourse/lib/uploads";
+import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
import {
acceptance,
chromeTest,
@@ -656,6 +658,253 @@ acceptance("Uppy Composer Attachment - Rich Editor", function (needs) {
});
});
+let optimizeFn;
+let runParallel = true;
+
+class TestMediaOptimization extends UppyMediaOptimization {
+ static pluginId = "test-media-optimization";
+}
+
+function registerTestOptimizer() {
+ withPluginApi((api) => {
+ api.addComposerUploadPreProcessor(TestMediaOptimization, () => ({
+ optimizeFn: (file) => optimizeFn(file),
+ runParallel,
+ }));
+ });
+}
+
+acceptance(
+ "Uppy Composer Attachment - Cancelling during processing",
+ function (needs) {
+ async function cancelOneOfTwoFiles(owner, assert) {
+ let stuckFileId;
+ optimizeFn = (file) => {
+ if (file.name === "stuck.png") {
+ stuckFileId = file.id;
+ return new Promise(() => {});
+ }
+ return Promise.resolve();
+ };
+
+ await visit("/");
+ await click("#create-topic");
+ await fillIn(".d-editor-input", "A long post\n");
+
+ const appEvents = owner.lookup("service:app-events");
+ const composer = owner.lookup("service:composer");
+ const uploadsComplete = new Promise((resolve) =>
+ appEvents.one("composer:all-uploads-complete", resolve)
+ );
+
+ appEvents.trigger("composer:add-files", [
+ createFile("stuck.png"),
+ createFile("avatar.png"),
+ ]);
+
+ await waitUntil(() => stuckFileId);
+ appEvents.trigger("composer:cancel-upload", { fileId: stuckFileId });
+ await uploadsComplete;
+ await settled();
+
+ assert
+ .dom(".d-editor-input")
+ .hasValue(
+ "A long post\n![avatar.PNG|690x320](upload://yoj8pf9DdIeHRRULyw7i57GAYdz.jpeg)\n",
+ "the cancelled file is removed and the other one is uploaded"
+ );
+ assert.false(
+ composer.isProcessingUpload,
+ "the composer is no longer processing"
+ );
+ }
+
+ needs.user();
+ needs.pretender(pretender);
+ needs.settings({ allow_uncategorized_topics: true });
+ needs.hooks.beforeEach(() => {
+ runParallel = true;
+ registerTestOptimizer();
+ });
+ needs.hooks.afterEach(() => {
+ uploadNumber = 1;
+ });
+
+ test("cancels a stuck upload without touching the rest of the post", async function (assert) {
+ optimizeFn = () => new Promise(() => {});
+
+ await visit("/");
+ await click("#create-topic");
+ await fillIn(".d-editor-input", "A long post\n");
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ const composer = getOwner(this).lookup("service:composer");
+ appEvents.trigger("composer:add-files", createFile("avatar.png"));
+
+ await waitUntil(() =>
+ find(".d-editor-input").value.includes("[Processing: avatar.png…]()")
+ );
+ assert
+ .dom("#cancel-file-upload")
+ .exists("the upload can be cancelled while it is processing");
+
+ await click("#cancel-file-upload");
+
+ assert
+ .dom(".d-editor-input")
+ .hasValue("A long post\n", "only the upload placeholder is removed");
+ assert
+ .dom("#file-uploading")
+ .doesNotExist("the upload indicator is cleared");
+ assert.false(composer.isUploading, "the composer is no longer uploading");
+ assert.false(
+ composer.isProcessingUpload,
+ "the composer is no longer processing"
+ );
+ assert.dom("#reply-control.open").exists("the composer stays open");
+ });
+
+ test("cancelling one file mid-processing lets the others finish", async function (assert) {
+ await cancelOneOfTwoFiles(getOwner(this), assert);
+ });
+
+ test("cancelling one file mid-processing lets the others finish when processing serially", async function (assert) {
+ runParallel = false;
+ await cancelOneOfTwoFiles(getOwner(this), assert);
+ });
+
+ test("cancelling while the uploaded markdown is resolved keeps the post intact", async function (assert) {
+ optimizeFn = () => Promise.resolve();
+ let markdownRequested = false;
+ let resolveMarkdown;
+ const markdown = new Promise((resolve) => (resolveMarkdown = resolve));
+ withPluginApi((api) => {
+ api.addComposerUploadMarkdownResolver(() => {
+ markdownRequested = true;
+ return markdown;
+ });
+ });
+
+ await visit("/");
+ await click("#create-topic");
+ await fillIn(".d-editor-input", "A long post\n");
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ let uploadFinished = false;
+ appEvents.one("composer:upload-success", () => (uploadFinished = true));
+ appEvents.trigger("composer:add-files", createFile("avatar.png"));
+
+ await waitUntil(() => markdownRequested);
+ await click("#cancel-file-upload");
+ resolveMarkdown("![avatar](upload://avatar.png)");
+ await waitUntil(() => uploadFinished);
+
+ assert
+ .dom(".d-editor-input")
+ .hasValue(
+ "A long post\n",
+ "the cancelled upload isn't inserted into the post"
+ );
+ });
+
+ test("cancelling a later batch does not wait on files already uploading", async function (assert) {
+ let stuckFileId;
+ optimizeFn = (file) => {
+ if (file.name === "stuck.png") {
+ stuckFileId = file.id;
+ return new Promise(() => {});
+ }
+ return Promise.resolve();
+ };
+
+ await visit("/");
+ await click("#create-topic");
+ await fillIn(".d-editor-input", "A long post\n");
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ const composer = getOwner(this).lookup("service:composer");
+ const uploadsComplete = new Promise((resolve) =>
+ appEvents.one("composer:all-uploads-complete", resolve)
+ );
+ let preprocessedBatches = 0;
+ appEvents.on("composer:uploads-preprocessing-complete", () => {
+ preprocessedBatches++;
+ });
+
+ appEvents.trigger("composer:add-files", createFile("avatar.png"));
+ await waitUntil(() => preprocessedBatches === 1);
+ appEvents.trigger("composer:add-files", createFile("stuck.png"));
+ await waitUntil(() => stuckFileId);
+ assert.true(
+ composer.isProcessingUpload,
+ "the second batch is processing"
+ );
+
+ appEvents.trigger("composer:cancel-upload", { fileId: stuckFileId });
+
+ assert.false(
+ composer.isProcessingUpload,
+ "cancelling the stuck file ends processing while the first batch uploads"
+ );
+ assert.true(composer.isUploading, "the first batch is still uploading");
+
+ await uploadsComplete;
+ await settled();
+
+ assert
+ .dom(".d-editor-input")
+ .hasValue(
+ "A long post\n![avatar.PNG|690x320](upload://yoj8pf9DdIeHRRULyw7i57GAYdz.jpeg)\n",
+ "the first batch is inserted and the cancelled file is not"
+ );
+ });
+ }
+);
+
+acceptance(
+ "Uppy Composer Attachment - Cancelling during processing - Rich Editor",
+ function (needs) {
+ needs.user({ "user_option.composition_mode": 1 });
+ needs.pretender(pretender);
+ needs.settings({ allow_uncategorized_topics: true });
+ needs.hooks.beforeEach(() => {
+ runParallel = true;
+ registerTestOptimizer();
+ });
+ needs.hooks.afterEach(() => {
+ uploadNumber = 1;
+ });
+
+ test("cancels a stuck upload and keeps the composer open", async function (assert) {
+ optimizeFn = () => new Promise(() => {});
+
+ await visit("/new-topic");
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ const composer = getOwner(this).lookup("service:composer");
+ appEvents.trigger("composer:add-files", createFile("avatar.png"));
+
+ await waitFor(".upload-placeholder.--image img");
+ await waitUntil(() => composer.isProcessingUpload);
+ assert
+ .dom("#cancel-file-upload")
+ .exists("the upload can be cancelled while it is processing");
+
+ await click("#cancel-file-upload");
+
+ assert
+ .dom(".upload-placeholder")
+ .doesNotExist("the placeholder is removed");
+ assert.false(composer.isUploading, "the composer is no longer uploading");
+ assert.false(
+ composer.isProcessingUpload,
+ "the composer is no longer processing"
+ );
+ assert.dom("#reply-control.open").exists("the composer stays open");
+ });
+ }
+);
+
acceptance("Uppy Composer Attachment - Upload Error", function (needs) {
needs.user();
needs.pretender((server, helper) => {
diff --git a/frontend/discourse/tests/unit/lib/uppy-media-optimization-plugin-test.js b/frontend/discourse/tests/unit/lib/uppy-media-optimization-plugin-test.js
index 5b6fb9cf02f..032c53be5b7 100644
--- a/frontend/discourse/tests/unit/lib/uppy-media-optimization-plugin-test.js
+++ b/frontend/discourse/tests/unit/lib/uppy-media-optimization-plugin-test.js
@@ -9,10 +9,12 @@ class FakeUppy {
this.files = {
"uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764":
{
+ id: "uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764",
data: "old file state",
},
"uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764":
{
+ id: "uppy-test/file/blah1/ads37x2/blah1/jpg-1d-1d-2v-1d-1e-image/jpeg-99999-1837921727764",
data: "old file state 1",
},
};
@@ -22,6 +24,19 @@ class FakeUppy {
this.preprocessors.push(fn);
}
+ on(event, fn) {
+ this.handlers ??= {};
+ (this.handlers[event] ??= []).push(fn);
+ }
+
+ off() {}
+
+ removeFile(id) {
+ const file = this.files[id];
+ delete this.files[id];
+ this.handlers["file-removed"]?.forEach((fn) => fn(file));
+ }
+
getFile(id) {
return this.files[id];
}
@@ -172,4 +187,54 @@ module("Unit | Utility | UppyMediaOptimization Plugin", function (hooks) {
done();
});
});
+
+ test("a file removed mid-optimization settles the batch without completing it", async function (assert) {
+ const fakeUppy = new FakeUppy();
+ const cancelled = [];
+ const plugin = new UppyMediaOptimization(fakeUppy, {
+ runParallel: true,
+ optimizeFn: () => new Promise(() => {}),
+ cancelFn: (fileId) => cancelled.push(fileId),
+ });
+ plugin.install();
+ const fileId =
+ "uppy-test/file/vv2/xvejg5w/blah/jpg-1d-1d-2v-1d-1e-image/jpeg-9043429-1624921727764";
+
+ const batch = plugin.uppy.preprocessors[0]([fileId]);
+ fakeUppy.removeFile(fileId);
+ await batch;
+
+ assert.deepEqual(
+ plugin.uppy.emitted.map((e) => e.event),
+ ["preprocess-progress"],
+ "no completion is emitted for the removed file"
+ );
+ assert.deepEqual(cancelled, [fileId], "the optimizer is told to cancel");
+ });
+
+ test("serial optimization skips files removed before their turn", async function (assert) {
+ const fakeUppy = new FakeUppy();
+ const resolvers = {};
+ const plugin = new UppyMediaOptimization(fakeUppy, {
+ runParallel: false,
+ optimizeFn: (file) =>
+ new Promise((resolve) => (resolvers[file.id] = resolve)),
+ });
+ plugin.install();
+ const [first, second] = Object.keys(fakeUppy.files);
+
+ const batch = plugin.uppy.preprocessors[0]([first, second]);
+ fakeUppy.removeFile(second);
+ resolvers[first]("new file state");
+ await batch;
+
+ assert.deepEqual(
+ plugin.uppy.emitted.map((e) => [e.event, e.file.id]),
+ [
+ ["preprocess-progress", first],
+ ["preprocess-complete", first],
+ ],
+ "only the remaining file is processed"
+ );
+ });
});
diff --git a/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js b/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js
index 10509b5f8db..a6779c4c4f4 100644
--- a/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js
+++ b/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js
@@ -1,28 +1,56 @@
import { destroy } from "@ember/destroyable";
import { getOwner, setOwner } from "@ember/owner";
import { run } from "@ember/runloop";
+import { waitUntil } from "@ember/test-helpers";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import UppyComposerUpload from "discourse/lib/uppy/composer-upload";
+import { UploadPreProcessorPlugin } from "discourse/lib/uppy-plugin-base";
+import pretender from "discourse/tests/helpers/create-pretender";
import { createFile } from "discourse/tests/helpers/qunit-helpers";
+class PassThroughPreProcessor extends UploadPreProcessorPlugin {
+ static pluginId = "pass-through-preprocessor";
+
+ _process = (fileIds) => this._emitAllComplete(fileIds);
+
+ install() {
+ this._install(this._process);
+ }
+}
+
function fakeComposerModel() {
return { privateMessage: false };
}
-function buildUploader(parent) {
+function buildUploader(parent, uploadPreProcessors = []) {
return new UppyComposerUpload(parent, {
composerEventPrefix: "composer",
composerModel: fakeComposerModel(),
uploadMarkdownResolvers: [],
- uploadPreProcessors: [],
+ uploadPreProcessors,
uploadHandlers: [],
fileUploadElementId: "file-uploader",
});
}
+function fakeTextManipulation() {
+ const noop = () => {};
+ return {
+ autoGridImages: noop,
+ placeholder: {
+ insert: noop,
+ progress: noop,
+ progressComplete: noop,
+ success: noop,
+ cancel: noop,
+ cancelAll: noop,
+ },
+ };
+}
+
function buildPasteEvent(target, files) {
const event = new Event("paste", { cancelable: true, bubbles: true });
Object.defineProperty(event, "target", { value: target });
@@ -142,6 +170,45 @@ module("Unit | Lib | uppy/composer-upload", function (hooks) {
);
});
+ test("preprocessing completes once per batch after setup() is re-run", async function (assert) {
+ pretender.post("/uploads.json", (request) => [
+ 200,
+ { "Content-Type": "application/json" },
+ JSON.stringify({
+ id: 1,
+ url: "/uploads/avatar.png",
+ short_url: "upload://avatar.png",
+ original_filename: request.requestBody.get("file").name,
+ }),
+ ]);
+
+ const uploader = buildUploader(this.parent, [
+ { pluginClass: PassThroughPreProcessor, optionsResolverFn: () => ({}) },
+ ]);
+ uploader.textManipulation = fakeTextManipulation();
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ let preprocessedBatches = 0;
+ let uploadsComplete = false;
+ appEvents.on("composer:uploads-preprocessing-complete", () => {
+ preprocessedBatches++;
+ });
+ appEvents.one("composer:all-uploads-complete", () => {
+ uploadsComplete = true;
+ });
+
+ uploader.setup(this.formEl);
+ uploader.setup(this.formEl);
+ await uploader._addFiles([createFile("avatar.png")]);
+ await waitUntil(() => uploadsComplete, { timeout: 5000 });
+
+ assert.strictEqual(
+ preprocessedBatches,
+ 1,
+ "the rebuilt uploader reports preprocessing complete exactly once"
+ );
+ });
+
test("destroying the parent tears down", function (assert) {
const uploader = buildUploader(this.parent);
uploader.setup(this.formEl);
diff --git a/frontend/discourse/tests/unit/lib/uppy/wrapper-test.js b/frontend/discourse/tests/unit/lib/uppy/wrapper-test.js
new file mode 100644
index 00000000000..4c737771bc7
--- /dev/null
+++ b/frontend/discourse/tests/unit/lib/uppy/wrapper-test.js
@@ -0,0 +1,141 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { withSilencedDeprecations } from "discourse/lib/deprecated";
+import UppyWrapper from "discourse/lib/uppy/wrapper";
+
+class FakePreprocessor {
+ static pluginId = "fake-preprocessor";
+ static pluginType = "preprocessor";
+}
+
+class FakeUppy {
+ files = {};
+ #handlers = {};
+
+ use() {}
+
+ on(event, fn) {
+ (this.#handlers[event] ??= []).push(fn);
+ }
+
+ emit(event, ...args) {
+ this.#handlers[event]?.forEach((fn) => fn(...args));
+ }
+
+ getFiles() {
+ return Object.values(this.files);
+ }
+
+ upload(...files) {
+ files.forEach((file) => (this.files[file.id] = file));
+ this.emit("upload", "upload-id", files);
+ }
+
+ removeFile(id) {
+ const file = this.files[id];
+ delete this.files[id];
+ this.emit("file-removed", file);
+ }
+
+ complete(file) {
+ this.emit("preprocess-complete", file, false, FakePreprocessor.pluginId);
+ }
+}
+
+module("Unit | Lib | uppy/wrapper", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.uppy = new FakeUppy();
+ this.wrapper = new UppyWrapper(getOwner(this));
+ this.wrapper.uppyInstance = this.uppy;
+ this.wrapper.useUploadPlugin(FakePreprocessor);
+
+ this.completed = [];
+ this.allComplete = 0;
+ this.wrapper.onPreProcessComplete(
+ (file) => this.completed.push(file.id),
+ () => this.allComplete++
+ );
+
+ this.a = { id: "a", name: "a.png" };
+ this.b = { id: "b", name: "b.png" };
+ });
+
+ test("completes once every uploaded file has been preprocessed", function (assert) {
+ this.uppy.upload(this.a, this.b);
+ this.uppy.complete(this.a);
+ assert.strictEqual(this.allComplete, 0, "waits for the second file");
+
+ this.uppy.complete(this.b);
+ assert.strictEqual(this.allComplete, 1, "completes after the last file");
+ assert.deepEqual(this.completed, ["a", "b"]);
+ });
+
+ test("removing the last pending file completes preprocessing", function (assert) {
+ this.uppy.upload(this.a, this.b);
+ this.uppy.complete(this.a);
+ this.uppy.removeFile("b");
+ assert.strictEqual(
+ this.allComplete,
+ 1,
+ "does not wait on the removed file"
+ );
+
+ this.uppy.complete(this.b);
+ assert.deepEqual(this.completed, ["a"], "a late event for it is ignored");
+ assert.strictEqual(this.allComplete, 1, "and does not complete again");
+ });
+
+ test("removing an already preprocessed file does not complete again", function (assert) {
+ this.uppy.upload(this.a, this.b);
+ this.uppy.complete(this.a);
+ this.uppy.complete(this.b);
+ this.uppy.removeFile("a");
+ assert.strictEqual(this.allComplete, 1);
+ });
+
+ test("removing the only file is a cancel-all, not a completion", function (assert) {
+ this.uppy.upload(this.a);
+ this.uppy.removeFile("a");
+ assert.strictEqual(this.allComplete, 0);
+ });
+
+ test("ignores events for files that were never uploaded", function (assert) {
+ this.uppy.complete(this.a);
+ assert.deepEqual(this.completed, []);
+ assert.strictEqual(this.allComplete, 0);
+ });
+
+ test("starts over when the uppy instance is replaced", function (assert) {
+ this.uppy.upload(this.a);
+
+ this.uppy = new FakeUppy();
+ this.wrapper.uppyInstance = this.uppy;
+ this.wrapper.useUploadPlugin(FakePreprocessor);
+ this.wrapper.onPreProcessComplete(
+ (file) => this.completed.push(file.id),
+ () => this.allComplete++
+ );
+
+ this.uppy.upload(this.b);
+ this.uppy.complete(this.b);
+ assert.deepEqual(this.completed, ["b"], "tracks the new instance's files");
+ assert.strictEqual(
+ this.allComplete,
+ 1,
+ "completes once, not once per setup"
+ );
+ });
+
+ test("addNeedProcessing is a deprecated no-op", function (assert) {
+ withSilencedDeprecations("discourse.uppy-wrapper-add-need-processing", () =>
+ this.wrapper.addNeedProcessing(2)
+ );
+
+ this.uppy.upload(this.a);
+ this.uppy.complete(this.a);
+ assert.strictEqual(this.allComplete, 1, "tracking is unaffected");
+ });
+});
diff --git a/frontend/discourse/tests/unit/services/media-optimization-worker-test.js b/frontend/discourse/tests/unit/services/media-optimization-worker-test.js
new file mode 100644
index 00000000000..5787ff54b5d
--- /dev/null
+++ b/frontend/discourse/tests/unit/services/media-optimization-worker-test.js
@@ -0,0 +1,178 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import sinon from "sinon";
+
+function imageFile(id) {
+ return {
+ id,
+ name: `${id}.jpg`,
+ type: "image/jpeg",
+ size: 1024,
+ // Blob#arrayBuffer settles in real time, outside the fake clock
+ data: { type: "image/jpeg", arrayBuffer: async () => new ArrayBuffer(8) },
+ };
+}
+
+module("Unit | Service | media-optimization-worker", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.clock = sinon.useFakeTimers({
+ toFake: ["setTimeout", "clearTimeout"],
+ });
+
+ this.owner.lookup(
+ "service:site-settings"
+ ).composer_media_optimization_image_bytes_optimization_threshold = 0;
+
+ this.service = this.owner.lookup("service:media-optimization-worker");
+ this.workers = [];
+ sinon.stub(window, "Worker").callsFake(() => {
+ const worker = { postMessage: sinon.stub(), terminate: sinon.stub() };
+ this.workers.push(worker);
+ return worker;
+ });
+
+ this.optimize = (id) => {
+ const state = { settled: false, result: undefined };
+ this.service.optimizeImage(imageFile(id)).then((file) => {
+ state.settled = true;
+ state.result = file;
+ });
+ return state;
+ };
+ });
+
+ hooks.afterEach(function () {
+ this.service.stopWorker();
+ this.clock.restore();
+ sinon.restore();
+ });
+
+ test("cancelling the only pending file replaces the worker", async function (assert) {
+ const stuck = this.optimize("stuck");
+ await this.clock.tickAsync(0);
+
+ this.service.cancel("stuck");
+ await this.clock.tickAsync(0);
+ assert.true(stuck.settled, "the cancelled file settles");
+ assert.true(
+ this.workers[0].terminate.calledOnce,
+ "the worker it may be stuck in is terminated"
+ );
+
+ this.optimize("retry");
+ await this.clock.tickAsync(0);
+ assert.strictEqual(this.workers.length, 2, "a retry gets a fresh worker");
+ });
+
+ test("cancelling one of several pending files keeps the worker", async function (assert) {
+ const first = this.optimize("first");
+ const second = this.optimize("second");
+ await this.clock.tickAsync(0);
+
+ this.service.cancel("first");
+ await this.clock.tickAsync(0);
+ assert.true(first.settled, "the cancelled file settles");
+ assert.false(second.settled, "the other file is still being optimized");
+ assert.false(this.workers[0].terminate.called, "the worker is kept");
+ });
+
+ test("cancelling the remaining file after a late reply allows a fresh retry", async function (assert) {
+ this.optimize("cancelled");
+ const remaining = this.optimize("remaining");
+ await this.clock.tickAsync(0);
+
+ this.service.cancel("cancelled");
+ this.workers[0].onmessage({
+ data: { type: "skipped", fileId: "cancelled", fileName: "cancelled.jpg" },
+ });
+ await this.clock.tickAsync(0);
+
+ assert.false(remaining.settled, "the other file is still pending");
+ assert.strictEqual(
+ this.service.workerPendingCount,
+ 1,
+ "the cancelled file's reply does not decrement the pending count again"
+ );
+ assert.false(this.workers[0].terminate.called, "the shared worker is kept");
+
+ this.service.cancel("remaining");
+ await this.clock.tickAsync(0);
+ assert.true(remaining.settled, "the remaining file can be cancelled");
+ assert.true(
+ this.workers[0].terminate.calledOnce,
+ "cancelling the last file terminates the worker"
+ );
+
+ const retry = this.optimize("remaining");
+ await this.clock.tickAsync(0);
+ assert.strictEqual(this.workers.length, 2, "the retry gets a fresh worker");
+ this.workers[1].onmessage({
+ data: { type: "skipped", fileId: "remaining", fileName: "remaining.jpg" },
+ });
+ await this.clock.tickAsync(0);
+ assert.true(retry.settled, "the retry completes normally");
+ });
+
+ test("a read failure after cancellation does not prevent cancelling the next upload", async function (assert) {
+ let rejectRead;
+ const file = imageFile("cancelled");
+ file.data.arrayBuffer = () =>
+ new Promise((resolve, reject) => (rejectRead = reject));
+ this.service.optimizeImage(file);
+
+ this.service.cancel(file.id);
+ await this.clock.tickAsync(0);
+ rejectRead(new Error("Read failed"));
+ await this.clock.tickAsync(0);
+
+ const next = this.optimize("next");
+ await this.clock.tickAsync(0);
+ assert.strictEqual(
+ this.service.workerPendingCount,
+ 1,
+ "the late read failure does not affect the next upload's pending count"
+ );
+ this.service.cancel("next");
+ await this.clock.tickAsync(0);
+
+ assert.true(next.settled, "the next upload can be cancelled");
+ assert.true(
+ this.workers[1].terminate.calledOnce,
+ "the unresponsive replacement worker is terminated"
+ );
+ });
+
+ test("a read failure from a stopped worker does not settle a retry of the same file", async function (assert) {
+ let rejectRead;
+ const file = imageFile("retry");
+ file.data.arrayBuffer = () =>
+ new Promise((resolve, reject) => (rejectRead = reject));
+ this.service.optimizeImage(file);
+
+ this.service.stopWorker();
+ await this.clock.tickAsync(0);
+ const retry = this.optimize(file.id);
+ await this.clock.tickAsync(0);
+ rejectRead(new Error("Read failed"));
+ await this.clock.tickAsync(0);
+
+ assert.false(retry.settled, "the retry keeps waiting for its own result");
+
+ this.workers[1].onmessage({
+ data: { type: "skipped", fileId: file.id, fileName: file.name },
+ });
+ await this.clock.tickAsync(0);
+ assert.true(retry.settled, "the retry completes with its own result");
+ });
+
+ test("closing the composer settles in-flight optimizations", async function (assert) {
+ const pending = this.optimize("pending");
+ await this.clock.tickAsync(0);
+
+ this.owner.lookup("service:app-events").trigger("composer:closed");
+ await this.clock.tickAsync(0);
+ assert.true(pending.settled, "the upload is not left hanging");
+ });
+});
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs
index 41f5e4d4c1e..475f531c313 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs
@@ -31,6 +31,7 @@ export default class ChatComposerUploads extends Component {
this.uppyUpload.uppyWrapper.useUploadPlugin(UppyMediaOptimization, {
optimizeFn: (data, opts) =>
this.mediaOptimizationWorker.optimizeImage(data, opts),
+ cancelFn: (fileId) => this.mediaOptimizationWorker.cancel(fileId),
runParallel: !this.capabilities.isMobileDevice,
});
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs
index 7516dedff7c..e44f662bddf 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-bot-conversations.gjs
@@ -109,6 +109,7 @@ export default class AiBotConversations extends Component {
this.uppyUpload.uppyWrapper.useUploadPlugin(UppyMediaOptimization, {
optimizeFn: (data, opts) =>
this.mediaOptimizationWorker.optimizeImage(data, opts),
+ cancelFn: (fileId) => this.mediaOptimizationWorker.cancel(fileId),
runParallel: !this.capabilities.isMobileDevice,
});
}