Skip to content

Commit

Permalink
FEATURE: Save MIME type for encrypted uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
udan11 committed Dec 9, 2019
1 parent 0739d88 commit 1ce15bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
38 changes: 22 additions & 16 deletions assets/javascripts/discourse/initializers/hook-decrypt-post.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
}));
});
Expand Down Expand Up @@ -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") {
Expand All @@ -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$/, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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], {
Expand All @@ -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}](`
);
});
});
}
Expand Down

0 comments on commit 1ce15bc

Please sign in to comment.