@@ -15,7 +15,7 @@ import log from "../services/log.js";
1515import type SNote from "./shaca/entities/snote.js" ;
1616import type SBranch from "./shaca/entities/sbranch.js" ;
1717import type SAttachment from "./shaca/entities/sattachment.js" ;
18- import utils from "../services/utils.js" ;
18+ import utils , { safeExtractMessageAndStackFromError } from "../services/utils.js" ;
1919import options from "../services/options.js" ;
2020
2121function getSharedSubTreeRoot ( note : SNote ) : { note ?: SNote ; branch ?: SBranch } {
@@ -115,7 +115,14 @@ function renderImageAttachment(image: SNote, res: Response, attachmentName: stri
115115 svgString = content ;
116116 } else {
117117 // backwards compatibility, before attachments, the SVG was stored in the main note content as a separate key
118- const contentSvg = image . getJsonContentSafely ( ) ?. svg ;
118+ const possibleSvgContent = image . getJsonContentSafely ( ) ;
119+
120+ const contentSvg = ( typeof possibleSvgContent === "object"
121+ && possibleSvgContent !== null
122+ && "svg" in possibleSvgContent
123+ && typeof possibleSvgContent . svg === "string" )
124+ ? possibleSvgContent . svg
125+ : null ;
119126
120127 if ( contentSvg ) {
121128 svgString = contentSvg ;
@@ -184,8 +191,9 @@ function register(router: Router) {
184191 res . send ( ejsResult ) ;
185192 useDefaultView = false ; // Rendering went okay, don't use default view
186193 }
187- } catch ( e : any ) {
188- log . error ( `Rendering user provided share template (${ templateId } ) threw exception ${ e . message } with stacktrace: ${ e . stack } ` ) ;
194+ } catch ( e : unknown ) {
195+ const [ errMessage , errStack ] = safeExtractMessageAndStackFromError ( e ) ;
196+ log . error ( `Rendering user provided share template (${ templateId } ) threw exception ${ errMessage } with stacktrace: ${ errStack } ` ) ;
189197 }
190198 }
191199 }
@@ -195,7 +203,7 @@ function register(router: Router) {
195203 }
196204 }
197205
198- router . get ( "/share/" , ( req , res , next ) => {
206+ router . get ( "/share/" , ( req , res ) => {
199207 if ( req . path . substr ( - 1 ) !== "/" ) {
200208 res . redirect ( "../share/" ) ;
201209 return ;
@@ -211,7 +219,7 @@ function register(router: Router) {
211219 renderNote ( shaca . shareRootNote , req , res ) ;
212220 } ) ;
213221
214- router . get ( "/share/:shareId" , ( req , res , next ) => {
222+ router . get ( "/share/:shareId" , ( req , res ) => {
215223 shacaLoader . ensureLoad ( ) ;
216224
217225 const { shareId } = req . params ;
@@ -221,7 +229,7 @@ function register(router: Router) {
221229 renderNote ( note , req , res ) ;
222230 } ) ;
223231
224- router . get ( "/share/api/notes/:noteId" , ( req , res , next ) => {
232+ router . get ( "/share/api/notes/:noteId" , ( req , res ) => {
225233 shacaLoader . ensureLoad ( ) ;
226234 let note : SNote | boolean ;
227235
@@ -234,7 +242,7 @@ function register(router: Router) {
234242 res . json ( note . getPojo ( ) ) ;
235243 } ) ;
236244
237- router . get ( "/share/api/notes/:noteId/download" , ( req , res , next ) => {
245+ router . get ( "/share/api/notes/:noteId/download" , ( req , res ) => {
238246 shacaLoader . ensureLoad ( ) ;
239247
240248 let note : SNote | boolean ;
@@ -256,7 +264,7 @@ function register(router: Router) {
256264 } ) ;
257265
258266 // :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
259- router . get ( "/share/api/images/:noteId/:filename" , ( req , res , next ) => {
267+ router . get ( "/share/api/images/:noteId/:filename" , ( req , res ) => {
260268 shacaLoader . ensureLoad ( ) ;
261269
262270 let image : SNote | boolean ;
@@ -282,7 +290,7 @@ function register(router: Router) {
282290 } ) ;
283291
284292 // :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename
285- router . get ( "/share/api/attachments/:attachmentId/image/:filename" , ( req , res , next ) => {
293+ router . get ( "/share/api/attachments/:attachmentId/image/:filename" , ( req , res ) => {
286294 shacaLoader . ensureLoad ( ) ;
287295
288296 let attachment : SAttachment | boolean ;
@@ -300,7 +308,7 @@ function register(router: Router) {
300308 }
301309 } ) ;
302310
303- router . get ( "/share/api/attachments/:attachmentId/download" , ( req , res , next ) => {
311+ router . get ( "/share/api/attachments/:attachmentId/download" , ( req , res ) => {
304312 shacaLoader . ensureLoad ( ) ;
305313
306314 let attachment : SAttachment | boolean ;
@@ -322,7 +330,7 @@ function register(router: Router) {
322330 } ) ;
323331
324332 // used for PDF viewing
325- router . get ( "/share/api/notes/:noteId/view" , ( req , res , next ) => {
333+ router . get ( "/share/api/notes/:noteId/view" , ( req , res ) => {
326334 shacaLoader . ensureLoad ( ) ;
327335
328336 let note : SNote | boolean ;
@@ -340,19 +348,18 @@ function register(router: Router) {
340348 } ) ;
341349
342350 // Used for searching, require noteId so we know the subTreeRoot
343- router . get ( "/share/api/notes" , ( req , res , next ) => {
351+ router . get ( "/share/api/notes" , ( req , res ) => {
344352 shacaLoader . ensureLoad ( ) ;
345353
346354 const ancestorNoteId = req . query . ancestorNoteId ?? "_share" ;
347- let note ;
348355
349356 if ( typeof ancestorNoteId !== "string" ) {
350357 res . status ( 400 ) . json ( { message : "'ancestorNoteId' parameter is mandatory." } ) ;
351358 return ;
352359 }
353360
354361 // This will automatically return if no ancestorNoteId is provided and there is no shareIndex
355- if ( ! ( note = checkNoteAccess ( ancestorNoteId , req , res ) ) ) {
362+ if ( ! checkNoteAccess ( ancestorNoteId , req , res ) ) {
356363 return ;
357364 }
358365
0 commit comments