2023-11-22 19:34:48 +01:00
|
|
|
const becca = require('../becca/becca.js');
|
|
|
|
const NotFoundError = require('../errors/not_found_error.js');
|
|
|
|
const protectedSessionService = require('./protected_session.js');
|
2024-02-16 21:38:09 +02:00
|
|
|
const utils = require('./utils');
|
2023-05-05 16:37:39 +02:00
|
|
|
|
2023-07-25 22:27:15 +02:00
|
|
|
function getBlobPojo(entityName, entityId) {
|
2023-05-05 16:37:39 +02:00
|
|
|
const entity = becca.getEntity(entityName, entityId);
|
|
|
|
if (!entity) {
|
|
|
|
throw new NotFoundError(`Entity ${entityName} '${entityId}' was not found.`);
|
|
|
|
}
|
|
|
|
|
2023-06-14 22:21:22 +02:00
|
|
|
const blob = becca.getBlob(entity);
|
2023-12-30 00:34:46 +01:00
|
|
|
if (!blob) {
|
|
|
|
throw new NotFoundError(`Blob ${entity.blobId} for ${entityName} '${entityId}' was not found.`);
|
|
|
|
}
|
2023-05-05 16:37:39 +02:00
|
|
|
|
|
|
|
const pojo = blob.getPojo();
|
|
|
|
|
|
|
|
if (!entity.hasStringContent()) {
|
|
|
|
pojo.content = null;
|
2023-06-14 22:21:22 +02:00
|
|
|
} else {
|
|
|
|
pojo.content = processContent(pojo.content, entity.isProtected, true);
|
2023-05-05 16:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pojo;
|
|
|
|
}
|
|
|
|
|
2023-06-14 22:21:22 +02:00
|
|
|
function processContent(content, isProtected, isStringContent) {
|
|
|
|
if (isProtected) {
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
|
|
|
content = content === null ? null : protectedSessionService.decrypt(content);
|
|
|
|
} else {
|
|
|
|
content = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isStringContent) {
|
|
|
|
return content === null ? "" : content.toString("utf-8");
|
|
|
|
} else {
|
|
|
|
// see https://github.com/zadam/trilium/issues/3523
|
|
|
|
// IIRC a zero-sized buffer can be returned as null from the database
|
|
|
|
if (content === null) {
|
|
|
|
// this will force de/encryption
|
|
|
|
content = Buffer.alloc(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 23:22:08 +02:00
|
|
|
function calculateContentHash({blobId, content}) {
|
|
|
|
return utils.hash(`${blobId}|${content.toString()}`);
|
|
|
|
}
|
|
|
|
|
2023-05-05 16:37:39 +02:00
|
|
|
module.exports = {
|
2023-06-14 22:21:22 +02:00
|
|
|
getBlobPojo,
|
2023-07-27 23:22:08 +02:00
|
|
|
processContent,
|
|
|
|
calculateContentHash
|
2023-05-05 22:21:51 +02:00
|
|
|
};
|