Notes/src/becca/entities/battachment.js

110 lines
3.4 KiB
JavaScript
Raw Normal View History

"use strict";
const utils = require('../../services/utils');
const dateUtils = require('../../services/date_utils');
const becca = require('../becca');
const AbstractBeccaEntity = require("./abstract_becca_entity");
/**
2023-03-16 12:17:55 +01:00
* Attachment represent data related/attached to the note. Conceptually similar to attributes, but intended for
* larger amounts of data and generally not accessible to the user.
*
* @extends AbstractBeccaEntity
*/
2023-03-16 12:17:55 +01:00
class BAttachment extends AbstractBeccaEntity {
static get entityName() { return "attachments"; }
static get primaryKeyName() { return "attachmentId"; }
static get hashedProperties() { return ["attachmentId", "parentId", "role", "mime", "title", "blobId",
"utcDateScheduledForDeletionSince", "utcDateModified"]; }
constructor(row) {
super();
2023-03-16 12:11:00 +01:00
if (!row.parentId?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'noteId' must be given to initialize a Attachment entity");
2023-03-16 12:11:00 +01:00
} else if (!row.role?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'role' must be given to initialize a Attachment entity");
2023-03-16 12:11:00 +01:00
} else if (!row.mime?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'mime' must be given to initialize a Attachment entity");
2023-03-16 12:11:00 +01:00
} else if (!row.title?.trim()) {
2023-03-16 12:17:55 +01:00
throw new Error("'title' must be given to initialize a Attachment entity");
}
/** @type {string} */
2023-03-16 16:37:31 +01:00
this.attachmentId = row.attachmentId;
2023-03-16 12:11:00 +01:00
/** @type {string} either noteId or noteRevisionId to which this attachment belongs */
this.parentId = row.parentId;
/** @type {string} */
2023-03-16 12:11:00 +01:00
this.role = row.role;
/** @type {string} */
this.mime = row.mime;
2023-03-16 12:11:00 +01:00
/** @type {string} */
this.title = row.title;
/** @type {boolean} */
this.isProtected = !!row.isProtected;
/** @type {string} */
this.utcDateScheduledForDeletionSince = row.utcDateScheduledForDeletionSince;
/** @type {string} */
this.utcDateModified = row.utcDateModified;
}
getNote() {
2023-03-16 12:11:00 +01:00
return becca.notes[this.parentId];
}
/** @returns {boolean} true if the note has string content (not binary) */
isStringNote() {
return utils.isStringNote(this.type, this.mime);
}
/** @returns {*} */
getContent() {
return this._getContent();
}
2023-03-16 16:37:31 +01:00
/**
* @param content
* @param {object} [opts]
* @param {object} [opts.forceSave=false] - will also save this BAttachment entity
*/
setContent(content, opts) {
this._setContent(content, opts);
}
calculateCheckSum(content) {
2023-03-16 12:17:55 +01:00
return utils.hash(`${this.attachmentId}|${content.toString()}`);
}
beforeSaving() {
if (!this.name.match(/^[a-z0-9]+$/i)) {
throw new Error(`Name must be alphanumerical, "${this.name}" given.`);
}
super.beforeSaving();
this.utcDateModified = dateUtils.utcNowDateTime();
}
getPojo() {
return {
2023-03-16 12:17:55 +01:00
attachmentId: this.attachmentId,
2023-03-16 12:11:00 +01:00
parentId: this.parentId,
name: this.name,
mime: this.mime,
isProtected: !!this.isProtected,
2023-03-16 11:02:07 +01:00
contentCheckSum: this.contentCheckSum, // FIXME
isDeleted: false,
utcDateModified: this.utcDateModified
};
}
getPojoToSave() {
const pojo = this.getPojo();
delete pojo.content; // not getting persisted
return pojo;
}
}
2023-03-16 12:17:55 +01:00
module.exports = BAttachment;