FIX: Show image uploads in composer preview

FIX: Show image uploads in composer preview

Encrypted image uploads were displayed only in post body. This commit fixes the bug which enlarged small uploaded images.

diff --git a/assets/javascripts/discourse/initializers/encrypt-uploads.js b/assets/javascripts/discourse/initializers/encrypt-uploads.js
index 004f5b8..b5d95c7 100644
--- a/assets/javascripts/discourse/initializers/encrypt-uploads.js
+++ b/assets/javascripts/discourse/initializers/encrypt-uploads.js
@@ -95,8 +95,8 @@ export default {
           return;
         }
 
-        const uploadData = Object.assign({}, upload, encryptedUpload.metadata);
-        const markdown = getUploadMarkdown(uploadData).replace(
+        Object.assign(upload, encryptedUpload.metadata);
+        const markdown = getUploadMarkdown(upload).replace(
           "](",
           `|type=${encryptedUpload.type}|key=${encryptedUpload.key}](`
         );
diff --git a/assets/javascripts/lib/uploads.js b/assets/javascripts/lib/uploads.js
index 341aee2..a725928 100644
--- a/assets/javascripts/lib/uploads.js
+++ b/assets/javascripts/lib/uploads.js
@@ -12,10 +12,17 @@ export function getMetadata(file, siteSettings) {
     img.onerror = (err) => reject(err);
     img.src = window.URL.createObjectURL(file);
   }).then((img) => {
-    const ratio = Math.min(
-      siteSettings.max_image_width / img.width,
-      siteSettings.max_image_height / img.height
-    );
+    let ratio = 1;
+
+    if (
+      img.width > siteSettings.max_image_width ||
+      img.height > siteSettings.max_image_height
+    ) {
+      ratio = Math.min(
+        siteSettings.max_image_width / img.width,
+        siteSettings.max_image_height / img.height
+      );
+    }
 
     return {
       original_filename: file.name,
diff --git a/test/javascripts/lib/uploads-test.js b/test/javascripts/lib/uploads-test.js
index c037086..89b8582 100644
--- a/test/javascripts/lib/uploads-test.js
+++ b/test/javascripts/lib/uploads-test.js
@@ -15,6 +15,10 @@ test("getMetadata - image file", async (assert) => {
   });
   const data = await getMetadata(file, SITE_SETTINGS);
   assert.equal(data.original_filename, "test.png");
+  assert.equal(data.width, 1);
+  assert.equal(data.height, 1);
+  assert.equal(data.thumbnail_width, 1);
+  assert.equal(data.thumbnail_height, 1);
   assert.ok(data.url);
 });
 

GitHub sha: 26c6b0030abc33c7ed4b11dd79064c18cbbcf91c

This commit appears in #116 which was approved by eviltrout. It was merged by udan11.