FEATURE: Save MIME type for encrypted uploads

FEATURE: Save MIME type for encrypted uploads

diff --git a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6 b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
index b5d10a3..9f3c8f7 100644
--- a/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
@@ -103,7 +103,9 @@ function checkMetadata(attrs, expected) {
   return diff;
 }
 
-function downloadEncryptedFile(url, keyPromise) {
+function downloadEncryptedFile(url, keyPromise, opts) {
+  opts = opts || {};
+
   const downloadPromise = new Promise((resolve, reject) => {
     var req = new XMLHttpRequest();
     req.open("GET", url, true);
@@ -129,7 +131,7 @@ function downloadEncryptedFile(url, keyPromise) {
         .decrypt({ name: "AES-GCM", iv, tagLength: 128 }, key, content)
         .then(resolve, reject);
     }).then(buffer => ({
-      blob: new Blob([buffer], { type: "octet/stream" }),
+      blob: new Blob([buffer], { type: opts.type || "application/x-binary" }),
       name: download.filename
     }));
   });
@@ -173,19 +175,21 @@ function resolveShortUrlElement($el) {
 
     $el.text($el.text().replace(/\.encrypted$/, ""));
     $el.on("click", () => {
-      downloadEncryptedFile(url, keyPromise).then(file => {
-        const a = document.createElement("a");
-        a.href = window.URL.createObjectURL(file.blob);
-        a.download = file.name || $el.text();
-        a.download = a.download.replace(/\.encrypted$/, "");
-        a.style.display = "none";
-
-        document.body.appendChild(a);
-        a.click();
-        document.body.removeChild(a);
-
-        window.URL.revokeObjectURL(a.href);
-      });
+      downloadEncryptedFile(url, keyPromise, { type: $el.data("type") }).then(
+        file => {
+          const a = document.createElement("a");
+          a.href = window.URL.createObjectURL(file.blob);
+          a.download = file.name || $el.text();
+          a.download = a.download.replace(/\.encrypted$/, "");
+          a.style.display = "none";
+
+          document.body.appendChild(a);
+          a.click();
+          document.body.removeChild(a);
+
+          window.URL.revokeObjectURL(a.href);
+        }
+      );
       return false;
     });
   } else if ($el.prop("tagName") === "IMG") {
@@ -201,7 +205,9 @@ function resolveShortUrlElement($el) {
       return;
     }
 
-    return downloadEncryptedFile(url, keyPromise).then(file => {
+    return downloadEncryptedFile(url, keyPromise, {
+      type: $el.data("type")
+    }).then(file => {
       const imageName = file.name
         ? imageNameFromFileName(file.name)
         : $el.attr("alt").replace(/\.encrypted$/, "");
diff --git a/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6 b/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
index 897b870..c718339 100644
--- a/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-encrypt-upload.js.es6
@@ -20,9 +20,12 @@ export default {
 
     withPluginApi("0.8.31", api => {
       DEFAULT_LIST.push("a[data-key]");
+      DEFAULT_LIST.push("a[data-type]");
       DEFAULT_LIST.push("img[data-key]");
+      DEFAULT_LIST.push("img[data-type]");
 
       const uploadsKeys = {};
+      const uploadsType = {};
       const uploadsData = {};
 
       api.addComposerUploadHandler([".*"], (file, editor) => {
@@ -92,6 +95,7 @@ export default {
         Promise.all([encryptedPromise, exportedKeyPromise, dataPromise]).then(
           ([encrypted, exportedKey, data]) => {
             uploadsKeys[file.name] = exportedKey;
+            uploadsType[file.name] = file.type;
             uploadsData[file.name] = data;
 
             const blob = new Blob([iv, encrypted], {
@@ -118,11 +122,16 @@ export default {
         Object.assign(realUpload, upload);
         Object.assign(realUpload, uploadsData[filename]);
         const key = uploadsKeys[filename];
+        const type = uploadsType[filename];
 
         delete uploadsData[filename];
         delete uploadsKeys[filename];
+        delete uploadsType[filename];
 
-        return getUploadMarkdown(realUpload).replace("](", `|key=${key}](`);
+        return getUploadMarkdown(realUpload).replace(
+          "](",
+          `|type=${type}|key=${key}](`
+        );
       });
     });
   }

GitHub sha: 1ce15bc3

1 Like