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 direct content attachment downloading
thecodrrd176a2e0dd8a
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.jsindex 4d9ddafc4..1d283ec05 100644--- a/packages/core/api/sync/index.js+++ b/packages/core/api/sync/index.js@@ -83,8 +83,6 @@ export default class Sync {var serverResponse = await this._fetch(lastSynced, token);// merge the server responseawait this._merger.merge(serverResponse, lastSynced);-- // await this._downloadAttachments(token);}// check for conflicts and throwdiff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.jsindex ad91c213b..4f3c0cd28 100644--- a/packages/core/collections/attachments.js+++ b/packages/core/collections/attachments.js@@ -102,8 +102,20 @@ export default class Attachments extends Collection {return await this._collection.updateItem(attachment);}- get(noteId) {- return this.file.filter((attachment) =>+ /**+ * Get specified type of attachments of a note+ * @param {string} noteId+ * @param {"files"|"images"|"all"} type+ * @returns+ */+ ofNote(noteId, type) {+ let attachments = [];++ if (type === "files") attachments = this.files;+ else if (type === "images") attachments = this.images;+ else if (type === "all") attachments = this.all;++ return attachments.filter((attachment) =>hasItem(attachment.noteIds, noteId));}@@ -113,6 +125,10 @@ export default class Attachments extends Collection {return !!attachment;}+ /**+ * @param {string} hash+ * @returns {Promise<string>} dataurl formatted string+ */async read(hash) {const attachment = this.all.find((a) => a.metadata.hash === hash);if (!attachment) return;@@ -128,7 +144,7 @@ export default class Attachments extends Collection {outputType: "base64",});- return { data, ...attachment };+ return dataurl.fromObject({ type: attachment.metadata.type, data });}attachment(hashOrId) {@@ -154,35 +170,40 @@ export default class Attachments extends Collection {);}- async download(noteId) {- const attachments = this.media.filter((attachment) =>+ async downloadImages(noteId) {+ const attachments = this.images.filter((attachment) =>hasItem(attachment.noteIds, noteId));console.log("Downloading attachments", attachments);for (let i = 0; i < attachments.length; i++) {- sendAttachmentsProgressEvent("download", attachments.length, i);-const { hash } = attachments[i].metadata;+ await this._downloadMedia(hash, {+ total: attachments.length,+ current: i,+ groupId: noteId,+ });+ }+ }- const isDownloaded = await this._db.fs.downloadFile(noteId, hash);- if (!isDownloaded) continue;+ async _downloadMedia(hash, { total, current, groupId }) {+ sendAttachmentsProgressEvent("download", total, current);+ try {+ const isDownloaded = await this._db.fs.downloadFile(groupId, hash);+ if (!isDownloaded) return;- const attachment = await this.read(hash);- if (!attachment) continue;+ const src = await this.read(hash);+ if (!src) return;EV.publish(EVENTS.mediaAttachmentDownloaded, {hash,- src: dataurl.fromObject({- type: attachment.metadata.type,- data: attachment.data,- }),+ src,});- }- sendAttachmentsProgressEvent("download", attachments.length);- }- get deleted() {- return this.all.filter((attachment) => attachment.dateDeleted > 0);+ return src;+ } finally {+ if (1 + current === total)+ sendAttachmentsProgressEvent("download", total);+ }}async cleanup() {@@ -205,13 +226,17 @@ export default class Attachments extends Collection {);}- get media() {+ get deleted() {+ return this.all.filter((attachment) => attachment.dateDeleted > 0);+ }++ get images() {return this.all.filter((attachment) =>attachment.metadata.type.startsWith("image/"));}- get file() {+ get files() {return this.all.filter((attachment) => !attachment.metadata.type.startsWith("image/"));diff --git a/packages/core/collections/content.js b/packages/core/collections/content.jsindex d03f9651c..19011dce7 100644--- a/packages/core/collections/content.js+++ b/packages/core/collections/content.js@@ -41,10 +41,10 @@ export default class Content extends Collection {return content.data;}- async raw(id, withAttachments = false) {+ async raw(id) {const content = await this._collection.getItem(id);if (!content) return;- return withAttachments ? await this.insertAttachments(content) : content;+ return content;}remove(id) {@@ -60,14 +60,32 @@ export default class Content extends Collection {return this._collection.getItems(this._collection.indexer.indices);}- async insertAttachments(contentItem) {+ insertMedia(contentItem) {+ return this._insert(contentItem, this._db.attachments.read);+ }++ insertPlaceholders(contentItem, placeholder) {+ return this._insert(contentItem, () => placeholder);+ }++ async downloadMedia(groupId, contentItem) {const content = getContentFromData(contentItem.type, contentItem.data);- contentItem.data = await content.insertAttachments((hash) => {- return this._db.attachments.read(hash);+ contentItem.data = await content.insertMedia((hash, { total, current }) => {+ return this._db.attachments._downloadMedia(hash, {+ total,+ current,+ groupId,+ });});return contentItem;}+ async _insert(contentItem, getData) {+ const content = getContentFromData(contentItem.type, contentItem.data);+ contentItem.data = await content.insertMedia(getData);+ return contentItem;+ }+/**** @param {any} contentItemdiff --git a/packages/core/content-types/tiny.js b/packages/core/content-types/tiny.jsindex 1b74cc6f3..b610115a6 100644--- a/packages/core/content-types/tiny.js+++ b/packages/core/content-types/tiny.js@@ -54,7 +54,7 @@ class Tiny {return tokens.some((token) => lowercase.indexOf(token) > -1);}- async insertAttachments(get) {+ async insertMedia(getData) {if (!("HTMLParser" in global)) return;let doc = HTMLParser.createElement("div");@@ -67,16 +67,12 @@ class Tiny {case "IMG": {const hash = getDatasetAttribute(attachment, "hash");- const attachmentItem = await get(hash);- if (!attachmentItem) continue;-- attachment.setAttribute(- "src",- dataurl.fromObject({- data: attachmentItem.data,- type: attachmentItem.metadata.type,- })- );+ const src = await getData(hash, {+ total: attachmentElements.length,+ current: i,+ });+ if (!src) continue;+ attachment.setAttribute("src", src);break;}}