mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-24 04:00:59 +01:00
core: hash base64 before saving it as attachment to prevent dups
This commit is contained in:
committed by
Abdullah Atta
parent
29608099db
commit
eaaecbd5e5
@@ -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;
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user