added checksum to note_attachment

pull/255/head
zadam 2023-01-24 16:55:48 +07:00
parent a7b103e07a
commit 3c57f08ef7
3 changed files with 18 additions and 6 deletions

@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS "note_attachments"
name TEXT not null, name TEXT not null,
mime TEXT not null, mime TEXT not null,
isProtected INT not null DEFAULT 0, isProtected INT not null DEFAULT 0,
contentCheckSum TEXT not null,
utcDateModified TEXT not null, utcDateModified TEXT not null,
isDeleted INT not null, isDeleted INT not null,
`deleteId` TEXT DEFAULT NULL); `deleteId` TEXT DEFAULT NULL);

@ -1442,17 +1442,22 @@ class BNote extends AbstractBeccaEntity {
* @returns {BNoteAttachment} * @returns {BNoteAttachment}
*/ */
saveNoteAttachment(name, mime, content) { 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, noteId: this.noteId,
name, name,
mime, mime,
isProtected: this.isProtected isProtected: this.isProtected
}); });
noteAttachment.save();
noteAttachment.setContent(content); noteAttachment.setContent(content);
return noteAttachment; return noteAttachment;

@ -33,6 +33,8 @@ class BNoteAttachment extends AbstractBeccaEntity {
/** @type {boolean} */ /** @type {boolean} */
this.isProtected = !!row.isProtected; this.isProtected = !!row.isProtected;
/** @type {string} */ /** @type {string} */
this.contentCheckSum = row.contentCheckSum;
/** @type {string} */
this.utcDateModified = row.utcDateModified; this.utcDateModified = row.utcDateModified;
} }
@ -97,18 +99,22 @@ class BNoteAttachment extends AbstractBeccaEntity {
sql.upsert("note_attachment_contents", "noteAttachmentId", pojo); sql.upsert("note_attachment_contents", "noteAttachmentId", pojo);
const hash = utils.hash(`${this.noteAttachmentId}|${pojo.content.toString()}`); this.contentCheckSum = this.calculateCheckSum(pojo.content);
entityChangesService.addEntityChange({ entityChangesService.addEntityChange({
entityName: 'note_attachment_contents', entityName: 'note_attachment_contents',
entityId: this.noteAttachmentId, entityId: this.noteAttachmentId,
hash: hash, hash: this.contentCheckSum,
isErased: false, isErased: false,
utcDateChanged: this.getUtcDateChanged(), utcDateChanged: this.getUtcDateChanged(),
isSynced: true isSynced: true
}); });
} }
calculateCheckSum(content) {
return utils.hash(`${this.noteAttachmentId}|${content.toString()}`);
}
beforeSaving() { beforeSaving() {
if (!this.name.match(/^[a-z0-9]+$/i)) { if (!this.name.match(/^[a-z0-9]+$/i)) {
throw new Error(`Name must be alphanumerical, "${this.name}" given.`); throw new Error(`Name must be alphanumerical, "${this.name}" given.`);