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: add attachments support
thecodrr1c51824b3348
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.jsindex d8f4e1b36..86c89dae9 100644--- a/packages/core/api/sync/index.js+++ b/packages/core/api/sync/index.js@@ -73,15 +73,18 @@ export default class Sync {const now = Date.now();this._isSyncing = true;- if (full) var serverResponse = await this._fetch(lastSynced, token);+ await this._uploadAttachments(token);// we prepare local data before merging so we always have correct dataconst data = await this._collector.collect(lastSynced);data.lastSynced = now;if (full) {+ var serverResponse = await this._fetch(lastSynced, token);// merge the server responseawait this._merger.merge(serverResponse, lastSynced);++ await this._downloadAttachments(token);}// check for conflicts and throw@@ -147,4 +150,41 @@ export default class Sync {return response.lastSynced;}++ async _uploadAttachments(token) {+ const attachments = this._db.attachments.pending;+ console.log("Uploading attachments", attachments);+ for (let attachment of attachments) {+ const { hash } = attachment.metadata;+ const url = await this._getPresignedURL(hash, token, "PUT");+ const uploadResult = await this._db.fs.uploadFile(hash, { url });+ if (!uploadResult) throw new Error("Failed to upload file.");+ await this._db.attachments.markAsUploaded(attachment.id);+ }+ }++ async _downloadAttachments(token) {+ const attachments = this._db.attachments.media;+ console.log("Downloading attachments", attachments);+ for (let attachment of attachments) {+ const { hash } = attachment.metadata;+ const url = `${Constants.API_HOST}/s3?name=${hash}`;+ const downloadResult = await this._db.fs.downloadFile(hash, {+ url,+ headers: { Authorization: `Bearer ${token}` },+ });+ if (!downloadResult) throw new Error("Failed to download file.");+ }+ }++ async _getPresignedURL(filename, token, verb) {+ const response = await fetch(`${Constants.API_HOST}/s3?name=${filename}`, {+ method: verb,+ headers: {+ Authorization: `Bearer ${token}`,+ },+ });+ if (response.ok) return await response.text();+ throw new Error("Couldn't get presigned url.");+ }}diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.jsindex 25c514066..e28112dca 100644--- a/packages/core/api/sync/merger.js+++ b/packages/core/api/sync/merger.js@@ -76,8 +76,16 @@ class Merger {async merge(serverResponse, lastSynced) {if (!serverResponse) return false;this._lastSynced = lastSynced;- const { notes, synced, notebooks, content, trash, vaultKey, settings } =- serverResponse;+ const {+ notes,+ synced,+ notebooks,+ content,+ trash,+ vaultKey,+ settings,+ attachments,+ } = serverResponse;if (synced || areAllEmpty(serverResponse)) return false;this.key = await this._db.user.getEncryptionKey();@@ -86,6 +94,12 @@ class Merger {await this._db.vault._setKey(await this._deserialize(vaultKey, false));}+ await this._mergeArray(+ attachments,+ (id) => this._db.attachments.attachment(id),+ (item) => this._db.attachments.add(item)+ );+await this._mergeArray(settings,() => this._db.settings.raw,diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.jsindex f94e6e17f..ea4fcc833 100644--- a/packages/core/collections/attachments.js+++ b/packages/core/collections/attachments.js@@ -33,9 +33,11 @@ export default class Attachments extends Collection {* @returns*/add(attachment, noteId) {- if (!attachment || !noteId)+ if (!attachment)return console.error("attachment or noteId cannot be null");+ if (attachment.remote) return this._collection.addItem(attachment);+if (!attachment.hash) throw new Error("Please provide attachment hash.");const oldAttachment = this.all.find(@@ -68,6 +70,7 @@ export default class Attachments extends Collection {dateUploaded: undefined,dateDeleted: undefined,};+ console.log("adding attachmentitem", attachmentItem);return this._collection.addItem(attachmentItem);}@@ -83,24 +86,48 @@ export default class Attachments extends Collection {const attachment = this.all.find((a) => a.metadata.hash === hash);if (!attachment) return;await this._initEncryptionKey();- const data = await this._db.fs.readEncrypted(attachment.hash, this.key, {- iv: attachment.iv,- salt: attachment.salt,- length: attachment.length,- alg: attachment.alg,- outputType: "base64",- });+ const data = await this._db.fs.readEncrypted(+ attachment.metadata.hash,+ this.key,+ {+ iv: attachment.iv,+ salt: attachment.salt,+ length: attachment.length,+ alg: attachment.alg,+ outputType: "base64",+ }+ );attachment.data = data;return attachment;}+ async attachment(id) {+ return this.all.find((a) => a.id === id);+ }++ markAsUploaded(id) {+ const attachment = this.all.find((a) => a.id === id);+ console.log("mark as uploaded", id, attachment);+ if (!attachment) return;+ attachment.dateUploaded = Date.now();+ return this._collection.updateItem(attachment);+ }+async save(data, type) {await this._initEncryptionKey();await this._db.fs.writeEncrypted(null, { data, type, key: this.key });}get pending() {- return this.all.filter((attachment) => !attachment.dateUploaded);+ return this.all.filter(+ (attachment) => attachment.dateUploaded <= 0 || !attachment.dateUploaded+ );+ }++ get media() {+ return this.all.filter((attachment) =>+ attachment.metadata.type.startsWith("image/")+ );}get all() {diff --git a/packages/core/collections/content.js b/packages/core/collections/content.jsindex 813e3780c..715f08535 100644--- a/packages/core/collections/content.js+++ b/packages/core/collections/content.js@@ -9,7 +9,7 @@ export default class Content extends Collection {if (content.deleted || content.migrated)return await this._collection.addItem(content);- const oldContent = await this.raw(content.id);+ const oldContent = await this.raw(content.id, false);if (content.id && oldContent) {content = {...oldContent,