From 3c57f08ef72df4a47cbf6afbe5e43096587efd6f Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 24 Jan 2023 16:55:48 +0100 Subject: [PATCH] added checksum to note_attachment --- db/migrations/0213__note_attachments.sql | 1 + src/becca/entities/bnote.js | 13 +++++++++---- src/becca/entities/bnote_attachment.js | 10 ++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/db/migrations/0213__note_attachments.sql b/db/migrations/0213__note_attachments.sql index b3bf9ec72..f6593e3bc 100644 --- a/db/migrations/0213__note_attachments.sql +++ b/db/migrations/0213__note_attachments.sql @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS "note_attachments" name TEXT not null, mime TEXT not null, isProtected INT not null DEFAULT 0, + contentCheckSum TEXT not null, utcDateModified TEXT not null, isDeleted INT not null, `deleteId` TEXT DEFAULT NULL); diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js index 2e00b7824..72c675125 100644 --- a/src/becca/entities/bnote.js +++ b/src/becca/entities/bnote.js @@ -1442,17 +1442,22 @@ class BNote extends AbstractBeccaEntity { * @returns {BNoteAttachment} */ saveNoteAttachment(name, mime, content) { - this.getNoteAttachments() + let noteAttachment = this.getNoteAttachmentByName(name); - const noteAttachment = new BNoteAttachment({ + if (noteAttachment + && noteAttachment.mime === mime + && noteAttachment.contentCheckSum === noteAttachment.calculateCheckSum(content)) { + + return noteAttachment; // no change + } + + noteAttachment = new BNoteAttachment({ noteId: this.noteId, name, mime, isProtected: this.isProtected }); - noteAttachment.save(); - noteAttachment.setContent(content); return noteAttachment; diff --git a/src/becca/entities/bnote_attachment.js b/src/becca/entities/bnote_attachment.js index e48707da0..5ec9cddab 100644 --- a/src/becca/entities/bnote_attachment.js +++ b/src/becca/entities/bnote_attachment.js @@ -33,6 +33,8 @@ class BNoteAttachment extends AbstractBeccaEntity { /** @type {boolean} */ this.isProtected = !!row.isProtected; /** @type {string} */ + this.contentCheckSum = row.contentCheckSum; + /** @type {string} */ this.utcDateModified = row.utcDateModified; } @@ -97,18 +99,22 @@ class BNoteAttachment extends AbstractBeccaEntity { sql.upsert("note_attachment_contents", "noteAttachmentId", pojo); - const hash = utils.hash(`${this.noteAttachmentId}|${pojo.content.toString()}`); + this.contentCheckSum = this.calculateCheckSum(pojo.content); entityChangesService.addEntityChange({ entityName: 'note_attachment_contents', entityId: this.noteAttachmentId, - hash: hash, + hash: this.contentCheckSum, isErased: false, utcDateChanged: this.getUtcDateChanged(), isSynced: true }); } + calculateCheckSum(content) { + return utils.hash(`${this.noteAttachmentId}|${content.toString()}`); + } + beforeSaving() { if (!this.name.match(/^[a-z0-9]+$/i)) { throw new Error(`Name must be alphanumerical, "${this.name}" given.`);