added checksum to note_attachment

This commit is contained in:
zadam 2023-01-24 16:55:48 +01:00
parent a7b103e07a
commit 3c57f08ef7
3 changed files with 18 additions and 6 deletions

View File

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

View File

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

View File

@ -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.`);