Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: show progress when encrypting files

thecodrrbed1cf956452
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.js
index 8e7c5b36d..6b8988d85 100644
--- a/apps/web/src/app-effects.js
+++ b/apps/web/src/app-effects.js
@@ -65,10 +65,7 @@ function AppEffects({ isMobile, isTablet, setShow, slideToIndex }) {
const attachmentsLoadingEvent = EV.subscribe(
EVENTS.attachmentsLoading,
({ type, total, current }) => {
- const [key, status] =
- type === "download"
- ? ["downloadingAttachments", `Downloading`]
- : ["uploadingAttachments", `Uploading`];
+ const [key, status] = getProcessingStatusFromType(type);
if (current === total) setProcessingStatus(key);
else
@@ -83,12 +80,11 @@ function AppEffects({ isMobile, isTablet, setShow, slideToIndex }) {
const progressEvent = AppEventManager.subscribe(
AppEvents.UPDATE_ATTACHMENT_PROGRESS,
({ type, total, loaded }) => {
- const key =
- type === "download"
- ? "downloadingAttachments"
- : "uploadingAttachments";
+ console.log(type, total, loaded);
+ const [key] = getProcessingStatusFromType(type);
+ if (!key) return;
+
const processingStatus = store.get().processingStatuses[key];
- if (!processingStatus) return;
const { status } = processingStatus;
const percent = Math.round((loaded / total) * 100);
@@ -138,3 +134,16 @@ function AppEffects({ isMobile, isTablet, setShow, slideToIndex }) {
return <React.Fragment />;
}
export default AppEffects;
+
+function getProcessingStatusFromType(type) {
+ switch (type) {
+ case "download":
+ return ["downloadingAttachments", "Downloading"];
+ case "upload":
+ return ["uploadingAttachments", "Uploading"];
+ case "encrypt":
+ return ["encryptingAttachments", "Encrypting"];
+ default:
+ return undefined;
+ }
+}
diff --git a/apps/web/src/components/editor/plugins/picker.js b/apps/web/src/components/editor/plugins/picker.js
index 893198a67..817df028e 100644
--- a/apps/web/src/components/editor/plugins/picker.js
+++ b/apps/web/src/components/editor/plugins/picker.js
@@ -1,5 +1,6 @@
import Compressor from "compressorjs";
import { db } from "../../../common/db";
+import { showLoadingDialog } from "../../../common/dialog-controller";
import fs from "../../../interfaces/fs";
function register(editor) {
@@ -38,30 +39,39 @@ async function pickFile() {
const selectedFile = await showFilePicker({ acceptedFileTypes: "*/*" });
if (!selectedFile) return;
- const reader = selectedFile.stream().getReader();
- const { hash, type: hashType } = await fs.hashStream(reader);
- reader.releaseLock();
-
- let output = {};
- if (!db.attachments.exists(hash)) {
- output = await fs.writeEncryptedFile(selectedFile, key, hash);
- }
-
- await db.attachments.add({
- ...output,
- hash,
- hashType,
- salt: key.salt,
- filename: selectedFile.name,
- type: selectedFile.type,
+ const result = await showLoadingDialog({
+ title: `${selectedFile.name}`,
+ subtitle: "Please wait while we encrypt & attach your file.",
+ action: async () => {
+ const reader = selectedFile.stream().getReader();
+ const { hash, type: hashType } = await fs.hashStream(reader);
+ reader.releaseLock();
+
+ let output = {};
+ if (!db.attachments.exists(hash)) {
+ output = await fs.writeEncryptedFile(selectedFile, key, hash);
+ }
+
+ await db.attachments.add({
+ ...output,
+ hash,
+ hashType,
+ salt: "helloworld",
+ filename: selectedFile.name,
+ type: selectedFile.type,
+ });
+
+ return {
+ hash: hash,
+ filename: selectedFile.name,
+ type: selectedFile.type,
+ size: selectedFile.size,
+ };
+ },
});
- return {
- hash: hash,
- filename: selectedFile.name,
- type: selectedFile.type,
- size: selectedFile.size,
- };
+ if (!result.hash) throw new Error("Could not add attachment.");
+ return result;
}
async function pickImage() {
@@ -99,7 +109,7 @@ async function pickImage() {
}
async function getEncryptionKey() {
- // return { password: "helloworld" };
+ return { password: "helloworld" };
const key = await db.user.getEncryptionKey();
if (!key) throw new Error("No encryption key found. Are you logged in?");
return key; // { password: "helloworld" };
diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.js
index 831ed1bd7..9678b60a2 100644
--- a/apps/web/src/interfaces/fs.js
+++ b/apps/web/src/interfaces/fs.js
@@ -9,6 +9,7 @@ import { StreamableFS } from "streamablefs";
import NNCrypto from "./nncrypto.stub";
import hosts from "notes-core/utils/constants";
import StreamSaver from "streamsaver";
+import { sendAttachmentsProgressEvent } from "notes-core/common";
StreamSaver.mitm = "/downloader.html";
const ABYTES = 17;
@@ -26,10 +27,14 @@ async function writeEncryptedFile(file, key, hash) {
if (!localforage.supports(localforage.INDEXEDDB))
throw new Error("This browser does not support IndexedDB.");
- let offset = 0;
+ if (await streamablefs.exists(hash)) await streamablefs.deleteFile(hash);
+ let offset = 0;
+ let encrypted = 0;
const fileHandle = await streamablefs.createFile(hash, file.size, file.type);
+ sendAttachmentsProgressEvent("encrypt", 1, 0);
+
const iv = await crypto.encryptStream(
key,
{
@@ -46,11 +51,20 @@ async function writeEncryptedFile(file, key, hash) {
data: chunk,
};
},
- write: (chunk) => fileHandle.write(chunk),
+ write: (chunk) => {
+ encrypted += chunk.length - ABYTES;
+ reportProgress(
+ { total: file.size, loaded: encrypted },
+ { type: "encrypt", hash }
+ );
+ return fileHandle.write(chunk);
+ },
},
file.name
);
+ sendAttachmentsProgressEvent("encrypt", 1);
+
return {
iv: iv,
length: file.size,
@@ -115,9 +129,8 @@ async function readEncrypted(filename, key, cipherData) {
}
const reader = fileHandle.getReader();
- const plainText = new Uint8Array(
- fileHandle.file.size + fileHandle.file.chunks * ABYTES
- );
+ const ENCRYPTED_SIZE = fileHandle.file.size + fileHandle.file.chunks * ABYTES;
+ const plainText = new Uint8Array(ENCRYPTED_SIZE);
let offset = 0;
await crypto.decryptStream(
@@ -334,6 +347,7 @@ function cancellable(operation) {
return {
execute: () => operation(filename, requestOptions),
cancel: (message) => {
+ console.log("Canceled", message);
source.cancel(message);
},
};