A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: refactor attachment downloading & send chunksize
thecodrrbd2f88cad1a4
diff --git a/apps/web/packages/nncrypto/src/encryption.ts b/apps/web/packages/nncrypto/src/encryption.tsindex 04a218ec1..8b8274fd8 100644--- a/apps/web/packages/nncrypto/src/encryption.ts+++ b/apps/web/packages/nncrypto/src/encryption.ts@@ -10,7 +10,6 @@ import {from_base64,base64_variants,StateAddress,- from_string,} from "libsodium-wrappers";import KeyUtils from "./keyutils";import {diff --git a/apps/web/src/common/attachments.js b/apps/web/src/common/attachments.jsnew file mode 100644index 000000000..a8426aa6a--- /dev/null+++ b/apps/web/src/common/attachments.js@@ -0,0 +1,23 @@+import FS from "../interfaces/fs";+import { db } from "./db";++export async function downloadAttachment(hash) {+ const attachment = db.attachments.attachment(hash);+ if (!attachment) return;+ const downloadResult = await db.fs.downloadFile(+ attachment.metadata.hash,+ attachment.metadata.hash,+ attachment.chunkSize+ );+ if (!downloadResult) throw new Error("Failed to download file.");++ const key = await db.attachments.decryptKey(attachment.key);+ if (!key) throw new Error("Invalid key for attachment.");++ await FS.saveFile(attachment.metadata.hash, {+ key,+ iv: attachment.iv,+ name: attachment.metadata.filename,+ type: attachment.metadata.type,+ });+}diff --git a/apps/web/src/components/editor/plugins/picker.js b/apps/web/src/components/editor/plugins/picker.jsindex 06c1b645b..8c8a3ff3a 100644--- a/apps/web/src/components/editor/plugins/picker.js+++ b/apps/web/src/components/editor/plugins/picker.js@@ -13,35 +13,39 @@ function register(editor) {editor.ui.registry.addButton("attachment", {icon: "attachment",tooltip: "Attach a file",- onAction: () => insertFile(editor),+ onAction: () => insertAttachment(editor, "*/*"),});editor.ui.registry.addButton("image", {icon: "image",tooltip: "Insert image",- onAction: () => insertImage(editor),+ onAction: () => insertAttachment(editor, "image/*"),});}-async function insertImage(editor) {- const image = await pickImage();- if (!image) return;- editor.execCommand("mceAttachImage", image);-}--async function insertFile(editor) {- const file = await pickFile();- if (!file) return;- editor.execCommand("mceAttachFile", file);+async function insertAttachment(editor, type) {+ const selectedFile = await showFilePicker({+ acceptedFileTypes: type || "*/*",+ });+ if (selectedFile.type.startsWith("image/")) {+ const image = await pickImage(selectedFile);+ editor.execCommand("mceAttachImage", image);+ } else {+ const file = await pickFile(selectedFile);+ editor.execCommand("mceAttachFile", file);+ }}(function init() {global.tinymce.PluginManager.add("picker", register);})();-async function pickFile() {+/**+ * @param {File} selectedFile+ * @returns+ */+async function pickFile(selectedFile) {try {- const selectedFile = await showFilePicker({ acceptedFileTypes: "*/*" });if (selectedFile.size > FILE_SIZE_LIMIT)throw new Error("File too big. You cannot add files over 500 MB.");if (!selectedFile) return;@@ -101,11 +105,14 @@ async function pickFile() {}}-async function pickImage() {+/**+ * @param {File} selectedImage+ * @returns+ */+async function pickImage(selectedImage) {try {- const selectedImage = await showFilePicker({- acceptedFileTypes: "image/*",- });+ if (selectedImage.size > IMAGE_SIZE_LIMIT)+ throw new Error("Image too big. You cannot add images over 50 MB.");if (!selectedImage) return;const key = await getEncryptionKey();@@ -184,9 +191,6 @@ function compressImage(file, type) {* @param {Blob} result*/async success(result) {- if (result.size > IMAGE_SIZE_LIMIT)- reject("Image too big. You cannot add images over 50 MB.");-const buffer = await result.arrayBuffer();const base64 = Buffer.from(buffer).toString("base64");resolve({diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.jsindex 5289234ef..e8bad27ef 100644--- a/apps/web/src/components/editor/tinymce.js+++ b/apps/web/src/components/editor/tinymce.js@@ -43,8 +43,7 @@ import { showToast } from "../../utils/toast";import { useIsUserPremium } from "../../hooks/use-is-user-premium";import { AppEventManager, AppEvents } from "../../common";import { EV, EVENTS } from "notes-core/common";-import { db } from "../../common/db";-import FS from "../../interfaces/fs";+import { downloadAttachment } from "../../common/attachments";const markdownPatterns = [{ start: "```", replacement: "<pre></pre>" },@@ -249,16 +248,7 @@ function TinyMCE(props) {},extended_valid_elements: `img[*|src=/placeholder.svg]`,attachmenthandler_download_attachment: async (hash) => {- const attachment = db.attachments.attachment(hash);- if (!attachment) return;- await db.fs.downloadFile(hash, hash);- const key = await db.attachments.decryptKey(attachment.key);- await FS.saveFile(hash, {- key,- iv: attachment.iv,- name: attachment.metadata.filename,- type: attachment.metadata.type,- });+ await downloadAttachment(hash);},}}onBeforeExecCommand={async (command) => {diff --git a/apps/web/src/components/properties/index.js b/apps/web/src/components/properties/index.jsindex c2365a2cc..a43575851 100644--- a/apps/web/src/components/properties/index.js+++ b/apps/web/src/components/properties/index.js@@ -9,9 +9,9 @@ import { AnimatedFlex } from "../animated";import Toggle from "./toggle";import { navigate } from "../../navigation";import IconTag from "../icon-tag";-import FS from "../../interfaces/fs";import { formatBytes, truncateFilename } from "../../utils/filename";import ScrollContainer from "../scroll-container";+import { downloadAttachment } from "../../common/attachments";const tools = [{ key: "pinned", icon: Icon.Pin, label: "Pin" },@@ -303,22 +303,7 @@ function Properties({ noteId }) {m={1}sx={{ ":hover": { bg: "hover" }, opacity: 0 }}onClick={async () => {- if (- await db.fs.downloadFile(- attachment.metadata.hash,- attachment.metadata.hash- )- ) {- const key = await db.attachments.decryptKey(- attachment.key- );- await FS.saveFile(attachment.metadata.hash, {- key,- iv: attachment.iv,- name: attachment.metadata.filename,- type: attachment.metadata.type,- });- }+ await downloadAttachment(attachment.metadata.hash);}}><Icon.Download size={16} />diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.jsindex 751591d94..49bb9d121 100644--- a/apps/web/src/interfaces/fs.js+++ b/apps/web/src/interfaces/fs.js@@ -67,6 +67,7 @@ async function writeEncryptedFile(file, key, hash) {sendAttachmentsProgressEvent("encrypt", hash, 1);return {+ chunkSize: CHUNK_SIZE,iv: iv,length: file.size,salt: key.salt,@@ -265,7 +266,7 @@ function reportProgress(ev, { type, hash }) {}async function downloadFile(filename, requestOptions) {- const { url, headers, cancellationToken } = requestOptions;+ const { url, headers, chunkSize, cancellationToken } = requestOptions;if (await streamablefs.exists(filename)) return true;try {@@ -284,7 +285,7 @@ async function downloadFile(filename, requestOptions) {});if (!isSuccessStatusCode(response.status)) return false;- const distributor = new ChunkDistributor(ENCRYPTED_CHUNK_SIZE);+ const distributor = new ChunkDistributor(chunkSize + ABYTES);distributor.fill(new Uint8Array(response.data));distributor.close();@@ -417,6 +418,8 @@ class ChunkDistributor {}close() {+ if (!this.lastChunk)+ throw new Error("No data available in this distributor.");this.lastChunk.data = this.lastChunk.data.slice(0, this.lastChunk.length);this.lastChunk.final = true;this.done = true;