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

This commit is contained in:
Abdullah Atta
2023-03-22 09:31:33 +05:00
committed by Abdullah Atta
parent 29608099db
commit eaaecbd5e5
4 changed files with 28 additions and 5 deletions

View File

@@ -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;

View File

@@ -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,

View File

@@ -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 };

View File

@@ -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);
}
}