FIX: Better support for encrypted images

FIX: Better support for encrypted images

Generate the correct Markdown for encrypted images.

diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index a799ac9..7cc21f6 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -147,14 +147,13 @@ function resolveShortUrlElement($el) {
       $el.attr("href", url);
       const content = $el.text().split("|");
 
-      const topicId = $el.closest("[data-topic-id]").data("topic-id");
-      const keyPromise = getTopicKey(topicId);
-
       if (content[1] === ATTACHMENT_CSS_CLASS) {
         $el.addClass(ATTACHMENT_CSS_CLASS);
         $el.text(content[0].replace(/\.encrypted$/, ""));
         if (content[0].match(/\.encrypted$/)) {
           $el.on("click", () => {
+            const topicId = $el.closest("[data-topic-id]").data("topic-id");
+            const keyPromise = getTopicKey(topicId);
             downloadEncryptedFile(url, keyPromise).then(file => {
               const a = document.createElement("a");
               a.href = window.URL.createObjectURL(file.blob);
@@ -183,10 +182,9 @@ function resolveShortUrlElement($el) {
     $el.removeAttr("data-orig-src");
 
     if (url !== MISSING) {
-      const topicId = $el.closest("[data-topic-id]").data("topic-id");
-      const keyPromise = getTopicKey(topicId);
-
       if ($el.attr("alt").match(/\.encrypted$/)) {
+        const topicId = $el.closest("[data-topic-id]").data("topic-id");
+        const keyPromise = getTopicKey(topicId);
         return downloadEncryptedFile(url, keyPromise).then(file => {
           $el.attr("alt", $el.attr("alt").replace(/\.encrypted$/, ""));
           $el.attr("src", window.URL.createObjectURL(file.blob));
diff --git a/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6 b/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
index e87de29..549081a 100644
--- a/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
@@ -5,6 +5,7 @@ import {
   getTopicKey,
   hasTopicKey
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
+import { getUploadMarkdown, isAnImage } from "discourse/lib/uploads";
 
 export default {
   name: "hook-encrypt-upload",
@@ -16,6 +17,8 @@ export default {
     }
 
     withPluginApi("0.8.31", api => {
+      const localData = {};
+
       api.addComposerUploadHandler([".*"], (file, editor) => {
         const controller = container.lookup("controller:composer");
         const topicId = controller.get("model.topic.id");
@@ -29,6 +32,29 @@ export default {
           return true;
         }
 
+        if (isAnImage(file.name)) {
+          const img = new Image();
+          img.onload = function() {
+            const ratio = Math.min(
+              Discourse.SiteSettings.max_image_width / img.width,
+              Discourse.SiteSettings.max_image_height / img.height
+            );
+
+            localData[file.name] = {
+              original_filename: file.name,
+              width: img.width,
+              height: img.height,
+              thumbnail_width: Math.floor(img.width * ratio),
+              thumbnail_height: Math.floor(img.height * ratio)
+            };
+
+            // TODO: Save object URL to be used in composer
+          };
+          img.src = window.URL.createObjectURL(file);
+        } else {
+          localData[file.name] = { original_filename: file.name };
+        }
+
         let reader = new FileReader();
         reader.onloadend = () => {
           const iv = window.crypto.getRandomValues(new Uint8Array(12));
@@ -55,6 +81,21 @@ export default {
         reader.readAsArrayBuffer(file);
         return false;
       });
+
+      api.addComposerUploadMarkdownResolver(upload => {
+        const realUpload = {};
+        Object.assign(realUpload, upload);
+
+        const filename = upload.original_filename.replace(/\.encrypted$/, "");
+        if (!localData[filename]) {
+          return;
+        }
+
+        Object.assign(realUpload, localData[filename]);
+        delete localData[filename];
+
+        return getUploadMarkdown(realUpload).replace("|", ".encrypted|");
+      });
     });
   }
 };

GitHub sha: 1fcdc7f3