62 lines
1.8 KiB
TypeScript
Raw Normal View History

import becca from "../becca/becca.js";
import NotFoundError from "../errors/not_found_error.js";
import protectedSessionService from "./protected_session.js";
import { hash } from "./utils.js";
import type { Blob } from "./blob-interface.js";
2023-05-05 16:37:39 +02:00
function getBlobPojo(entityName: string, entityId: string, opts?: { preview: boolean }) {
// TODO: Unused opts.
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.`);
}
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;
} else {
2024-03-30 10:49:40 +02:00
pojo.content = processContent(pojo.content, !!entity.isProtected, true);
2023-05-05 16:37:39 +02:00
}
return pojo;
}
2024-02-16 23:52:11 +02:00
function processContent(content: Buffer | string | null, isProtected: boolean, isStringContent: boolean) {
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;
}
}
2025-01-09 18:07:02 +02:00
function calculateContentHash({ blobId, content }: Blob) {
return hash(`${blobId}|${content.toString()}`);
2023-07-27 23:22:08 +02:00
}
export default {
getBlobPojo,
2023-07-27 23:22:08 +02:00
processContent,
calculateContentHash
2023-05-05 22:21:51 +02:00
};