|
|
|
|
@ -208,37 +208,8 @@ class BNote extends AbstractBeccaEntity {
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** @returns {*} */
|
|
|
|
|
getContent(silentNotFoundError = false) {
|
|
|
|
|
const row = sql.getRow(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
|
|
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
|
if (silentNotFoundError) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw new Error(`Cannot find note content for noteId '${this.noteId}', blobId '${this.blobId}'.`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let content = row.content;
|
|
|
|
|
|
|
|
|
|
if (this.isProtected) {
|
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
|
|
|
|
content = content === null ? null : protectedSessionService.decrypt(content);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
content = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.isStringNote()) {
|
|
|
|
|
return content === null
|
|
|
|
|
? ""
|
|
|
|
|
: content.toString("UTF-8");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
getContent() {
|
|
|
|
|
return this._getContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @returns {{contentLength, dateModified, utcDateModified}} */
|
|
|
|
|
@ -252,22 +223,6 @@ class BNote extends AbstractBeccaEntity {
|
|
|
|
|
WHERE blobId = ?`, [this.blobId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get dateCreatedObj() {
|
|
|
|
|
return this.dateCreated === null ? null : dayjs(this.dateCreated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get utcDateCreatedObj() {
|
|
|
|
|
return this.utcDateCreated === null ? null : dayjs.utc(this.utcDateCreated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get dateModifiedObj() {
|
|
|
|
|
return this.dateModified === null ? null : dayjs(this.dateModified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get utcDateModifiedObj() {
|
|
|
|
|
return this.utcDateModified === null ? null : dayjs.utc(this.utcDateModified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @returns {*} */
|
|
|
|
|
getJsonContent() {
|
|
|
|
|
const content = this.getContent();
|
|
|
|
|
@ -279,77 +234,32 @@ class BNote extends AbstractBeccaEntity {
|
|
|
|
|
return JSON.parse(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isHot() {
|
|
|
|
|
_isHot() {
|
|
|
|
|
return ['text', 'code', 'relationMap', 'canvas', 'mermaid'].includes(this.type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setContent(content, ignoreMissingProtectedSession = false) {
|
|
|
|
|
if (content === null || content === undefined) {
|
|
|
|
|
throw new Error(`Cannot set null content to note '${this.noteId}'`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.isStringNote()) {
|
|
|
|
|
content = content.toString();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
content = Buffer.isBuffer(content) ? content : Buffer.from(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.isProtected) {
|
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
|
|
|
|
content = protectedSessionService.encrypt(content);
|
|
|
|
|
}
|
|
|
|
|
else if (!ignoreMissingProtectedSession) {
|
|
|
|
|
throw new Error(`Cannot update content of noteId '${this.noteId}' since we're out of protected session.`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setContent(content) {
|
|
|
|
|
this._setContent(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let newBlobId;
|
|
|
|
|
let blobNeedsInsert;
|
|
|
|
|
setJsonContent(content) {
|
|
|
|
|
this.setContent(JSON.stringify(content, null, '\t'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.isHot()) {
|
|
|
|
|
newBlobId = this.blobId || utils.randomBlobId();
|
|
|
|
|
blobNeedsInsert = true;
|
|
|
|
|
} else {
|
|
|
|
|
newBlobId = utils.hashedBlobId(content);
|
|
|
|
|
blobNeedsInsert = !sql.getValue('SELECT 1 FROM blobs WHERE blobId = ?', [newBlobId]);
|
|
|
|
|
}
|
|
|
|
|
get dateCreatedObj() {
|
|
|
|
|
return this.dateCreated === null ? null : dayjs(this.dateCreated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (blobNeedsInsert) {
|
|
|
|
|
const pojo = {
|
|
|
|
|
blobId: this.blobId,
|
|
|
|
|
content: content,
|
|
|
|
|
dateModified: dateUtils.localNowDateTime(),
|
|
|
|
|
utcDateModified: dateUtils.utcNowDateTime()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sql.upsert("blobs", "blobId", pojo);
|
|
|
|
|
|
|
|
|
|
const hash = utils.hash(`${this.blobId}|${pojo.content.toString()}`);
|
|
|
|
|
|
|
|
|
|
entityChangesService.addEntityChange({
|
|
|
|
|
entityName: 'blobs',
|
|
|
|
|
entityId: this.blobId,
|
|
|
|
|
hash: hash,
|
|
|
|
|
isErased: false,
|
|
|
|
|
utcDateChanged: pojo.utcDateModified,
|
|
|
|
|
isSynced: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
eventService.emit(eventService.ENTITY_CHANGED, {
|
|
|
|
|
entityName: 'blobs',
|
|
|
|
|
entity: this
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
get utcDateCreatedObj() {
|
|
|
|
|
return this.utcDateCreated === null ? null : dayjs.utc(this.utcDateCreated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newBlobId !== this.blobId) {
|
|
|
|
|
this.blobId = newBlobId;
|
|
|
|
|
this.save();
|
|
|
|
|
}
|
|
|
|
|
get dateModifiedObj() {
|
|
|
|
|
return this.dateModified === null ? null : dayjs(this.dateModified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setJsonContent(content) {
|
|
|
|
|
this.setContent(JSON.stringify(content, null, '\t'));
|
|
|
|
|
get utcDateModifiedObj() {
|
|
|
|
|
return this.utcDateModified === null ? null : dayjs.utc(this.utcDateModified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @returns {boolean} true if this note is the root of the note tree. Root note has "root" noteId */
|
|
|
|
|
|