Skip to content

Build your personal knowledge base with Trilium Notes

added checksum to note_attachment

zadam3c57f08ef72d
diff --git a/db/migrations/0213__note_attachments.sql b/db/migrations/0213__note_attachments.sql
index b3bf9ec727..f6593e3bce 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 2e00b78244..72c675125d 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 e48707da0f..5ec9cddabb 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.`);