FIX: Cache decrypted images

FIX: Cache decrypted images

diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index 9f3c8f7..db50aca 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -212,7 +212,8 @@ function resolveShortUrlElement($el) {
         ? imageNameFromFileName(file.name)
         : $el.attr("alt").replace(/\.encrypted$/, "");
       $el.attr("alt", imageName);
-      $el.attr("src", window.URL.createObjectURL(file.blob));
+      data.url = window.URL.createObjectURL(file.blob);
+      $el.attr("src", data.url);
     });
   }
 }
diff --git a/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6 b/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
index c718339..fcf57db 100644
--- a/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
@@ -27,6 +27,7 @@ export default {
       const uploadsKeys = {};
       const uploadsType = {};
       const uploadsData = {};
+      const uploadsUrl = {};
 
       api.addComposerUploadHandler([".*"], (file, editor) => {
         const controller = container.lookup("controller:composer");
@@ -41,6 +42,7 @@ export default {
               img.onload = () => resolve(img);
               img.onerror = err => reject(err);
               img.src = window.URL.createObjectURL(file);
+              uploadsUrl[file.name] = img.src;
             }).then(img => {
               const ratio = Math.min(
                 Discourse.SiteSettings.max_image_width / img.width,
@@ -114,7 +116,7 @@ export default {
 
       api.addComposerUploadMarkdownResolver(upload => {
         const filename = upload.original_filename.replace(/\.encrypted$/, "");
-        if (!uploadsKeys[filename] && !uploadsData[filename]) {
+        if (!uploadsKeys[filename]) {
           return;
         }
 
@@ -123,10 +125,12 @@ export default {
         Object.assign(realUpload, uploadsData[filename]);
         const key = uploadsKeys[filename];
         const type = uploadsType[filename];
+        upload.url = uploadsUrl[filename];
 
         delete uploadsData[filename];
         delete uploadsKeys[filename];
         delete uploadsType[filename];
+        delete uploadsUrl[filename];
 
         return getUploadMarkdown(realUpload).replace(
           "](",

GitHub sha: c9058381

1 Like