From 8989c9e6c9a82ae3a0b1f7aa4cf910995d32712a Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 23 Aug 2021 12:10:33 +1000 Subject: [PATCH] FIX: Use file.id instead of file.name for media-optimization resolvers (#14110) This change only applies when uppy is calling the media-optimization-worker. Since the old way of calling the worker via jQuery file uploader will be removed soon, there is no point coming up with some random string to use in place of the file name for the promise resolvers there, we can live with this for now. --- .../discourse/app/services/media-optimization-worker.js | 9 +++++---- public/javascripts/media-optimization-worker.js | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/app/services/media-optimization-worker.js b/app/assets/javascripts/discourse/app/services/media-optimization-worker.js index 1cac1783573..f2e82584761 100644 --- a/app/assets/javascripts/discourse/app/services/media-optimization-worker.js +++ b/app/assets/javascripts/discourse/app/services/media-optimization-worker.js @@ -60,7 +60,7 @@ export default class MediaOptimizationWorkerService extends Service { this.logIfDebug(`Transforming ${file.name}`); this.currentComposerUploadData = data; - this.promiseResolvers[file.name] = resolve; + this.promiseResolvers[this.usingUppy ? file.id : file.name] = resolve; let imageData; try { @@ -77,6 +77,7 @@ export default class MediaOptimizationWorkerService extends Service { this.worker.postMessage( { type: "compress", + fileId: file.id, file: imageData.data.buffer, fileName: file.name, width: imageData.width, @@ -117,7 +118,7 @@ export default class MediaOptimizationWorkerService extends Service { this.worker.onmessage = (e) => { switch (e.data.type) { case "file": - let optimizedFile = new File([e.data.file], `${e.data.fileName}`, { + let optimizedFile = new File([e.data.file], e.data.fileName, { type: "image/jpeg", }); this.logIfDebug( @@ -125,7 +126,7 @@ export default class MediaOptimizationWorkerService extends Service { ); if (this.usingUppy) { - this.promiseResolvers[optimizedFile.name](optimizedFile); + this.promiseResolvers[e.data.fileId](optimizedFile); } else { let data = this.currentComposerUploadData; data.files[data.index] = optimizedFile; @@ -143,7 +144,7 @@ export default class MediaOptimizationWorkerService extends Service { } if (this.usingUppy) { - this.promiseResolvers[e.data.fileName](); + this.promiseResolvers[e.data.fileId](); } else { this.promiseResolvers[e.data.fileName]( this.currentComposerUploadData diff --git a/public/javascripts/media-optimization-worker.js b/public/javascripts/media-optimization-worker.js index 3ed3c52a61a..189a88b761f 100644 --- a/public/javascripts/media-optimization-worker.js +++ b/public/javascripts/media-optimization-worker.js @@ -131,7 +131,8 @@ onmessage = async function (e) { { type: "file", file: optimized, - fileName: e.data.fileName + fileName: e.data.fileName, + fileId: e.data.fileId }, [optimized] ); @@ -140,7 +141,8 @@ onmessage = async function (e) { postMessage({ type: "error", file: e.data.file, - fileName: e.data.fileName + fileName: e.data.fileName, + fileId: e.data.fileId }); } break;