Skip to content

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

core: hash base64 before saving it as attachment to prevent dups

Abdullah Attaeaaecbd5e51d
diff --git a/apps/web/src/interfaces/fs.js b/apps/web/src/interfaces/fs.js
index d662c7759..d0a0cf1b0 100644
--- a/apps/web/src/interfaces/fs.js
+++ b/apps/web/src/interfaces/fs.js
@@ -122,6 +122,15 @@ async function writeEncryptedBase64(metadata) {
};
}
+/**
+ *
+ * @param {string} data the base64 data
+ * @returns
+ */
+function hashBase64(data) {
+ return hashBuffer(Buffer.from(data, "base64"));
+}
+
/**
*
* @param {import("hash-wasm/dist/lib/util").IDataType} data
@@ -469,11 +478,13 @@ const FS = {
deleteFile,
saveFile,
exists,
- hashBuffer,
- hashStream,
writeEncryptedFile,
clearFileStorage,
- getUploadedFileSize
+ getUploadedFileSize,
+
+ hashBase64,
+ hashBuffer,
+ hashStream
};
export default FS;
diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js
index da1243de1..311c12450 100644
--- a/packages/core/collections/attachments.js
+++ b/packages/core/collections/attachments.js
@@ -283,7 +283,11 @@ export default class Attachments extends Collection {
return this._collection.updateItem(attachment);
}
- async save(data, type, mimeType) {
+ async save(data, mimeType) {
+ const { hash } = await this._db.fs.hashBase64(data);
+ const attachment = this.attachment(hash);
+ if (attachment) return attachment;
+
const key = await this._db.attachments.generateKey();
const metadata = await this._db.fs.writeEncryptedBase64(
data,
diff --git a/packages/core/content-types/tiptap.js b/packages/core/content-types/tiptap.js
index a262da387..7628ebcd6 100644
--- a/packages/core/content-types/tiptap.js
+++ b/packages/core/content-types/tiptap.js
@@ -161,7 +161,7 @@ export class Tiptap {
try {
const { data, mime } = dataurl.toObject(image.src);
if (!data) continue;
- const storeResult = await store(data, "base64", mime);
+ const storeResult = await store(data, mime);
if (!storeResult) continue;
images[image.id] = { ...storeResult, mime };
diff --git a/packages/core/database/fs.js b/packages/core/database/fs.js
index 3ac421fae..0ec15e724 100644
--- a/packages/core/database/fs.js
+++ b/packages/core/database/fs.js
@@ -108,4 +108,12 @@ export default class FileStorage {
clear() {
return this.fs.clearFileStorage();
}
+
+ /**
+ * @param {string} data
+ * @returns {Promise<{hash: string, type: string}>}
+ */
+ hashBase64(data) {
+ return this.fs.hashBase64(data);
+ }
}