FIX: Use file.id instead of file.name for media-optimization resolvers (#14110)

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.

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 1cac178..f2e8258 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 3ed3c52..189a88b 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;

GitHub sha: 8989c9e6c9a82ae3a0b1f7aa4cf910995d32712a

This commit appears in #14110 which was approved by lis2. It was merged by martin.