| 
									
										
										
										
											2023-05-05 16:37:39 +02:00
										 |  |  | const becca = require('../becca/becca'); | 
					
						
							|  |  |  | const NotFoundError = require("../errors/not_found_error"); | 
					
						
							| 
									
										
										
										
											2023-06-14 22:21:22 +02:00
										 |  |  | const protectedSessionService = require("./protected_session"); | 
					
						
							| 
									
										
										
										
											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-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-05-05 16:37:39 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2023-06-14 22:21:22 +02:00
										 |  |  |     getBlobPojo, | 
					
						
							|  |  |  |     processContent | 
					
						
							| 
									
										
										
										
											2023-05-05 22:21:51 +02:00
										 |  |  | }; |