@@ -15,7 +15,7 @@ import log from "../services/log.js";
15
15
import type SNote from "./shaca/entities/snote.js" ;
16
16
import type SBranch from "./shaca/entities/sbranch.js" ;
17
17
import type SAttachment from "./shaca/entities/sattachment.js" ;
18
- import utils from "../services/utils.js" ;
18
+ import utils , { safeExtractMessageAndStackFromError } from "../services/utils.js" ;
19
19
import options from "../services/options.js" ;
20
20
21
21
function getSharedSubTreeRoot ( note : SNote ) : { note ?: SNote ; branch ?: SBranch } {
@@ -115,7 +115,14 @@ function renderImageAttachment(image: SNote, res: Response, attachmentName: stri
115
115
svgString = content ;
116
116
} else {
117
117
// 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 ;
119
126
120
127
if ( contentSvg ) {
121
128
svgString = contentSvg ;
@@ -184,8 +191,9 @@ function register(router: Router) {
184
191
res . send ( ejsResult ) ;
185
192
useDefaultView = false ; // Rendering went okay, don't use default view
186
193
}
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 } ` ) ;
189
197
}
190
198
}
191
199
}
@@ -195,7 +203,7 @@ function register(router: Router) {
195
203
}
196
204
}
197
205
198
- router . get ( "/share/" , ( req , res , next ) => {
206
+ router . get ( "/share/" , ( req , res ) => {
199
207
if ( req . path . substr ( - 1 ) !== "/" ) {
200
208
res . redirect ( "../share/" ) ;
201
209
return ;
@@ -211,7 +219,7 @@ function register(router: Router) {
211
219
renderNote ( shaca . shareRootNote , req , res ) ;
212
220
} ) ;
213
221
214
- router . get ( "/share/:shareId" , ( req , res , next ) => {
222
+ router . get ( "/share/:shareId" , ( req , res ) => {
215
223
shacaLoader . ensureLoad ( ) ;
216
224
217
225
const { shareId } = req . params ;
@@ -221,7 +229,7 @@ function register(router: Router) {
221
229
renderNote ( note , req , res ) ;
222
230
} ) ;
223
231
224
- router . get ( "/share/api/notes/:noteId" , ( req , res , next ) => {
232
+ router . get ( "/share/api/notes/:noteId" , ( req , res ) => {
225
233
shacaLoader . ensureLoad ( ) ;
226
234
let note : SNote | boolean ;
227
235
@@ -234,7 +242,7 @@ function register(router: Router) {
234
242
res . json ( note . getPojo ( ) ) ;
235
243
} ) ;
236
244
237
- router . get ( "/share/api/notes/:noteId/download" , ( req , res , next ) => {
245
+ router . get ( "/share/api/notes/:noteId/download" , ( req , res ) => {
238
246
shacaLoader . ensureLoad ( ) ;
239
247
240
248
let note : SNote | boolean ;
@@ -256,7 +264,7 @@ function register(router: Router) {
256
264
} ) ;
257
265
258
266
// :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 ) => {
260
268
shacaLoader . ensureLoad ( ) ;
261
269
262
270
let image : SNote | boolean ;
@@ -282,7 +290,7 @@ function register(router: Router) {
282
290
} ) ;
283
291
284
292
// :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 ) => {
286
294
shacaLoader . ensureLoad ( ) ;
287
295
288
296
let attachment : SAttachment | boolean ;
@@ -300,7 +308,7 @@ function register(router: Router) {
300
308
}
301
309
} ) ;
302
310
303
- router . get ( "/share/api/attachments/:attachmentId/download" , ( req , res , next ) => {
311
+ router . get ( "/share/api/attachments/:attachmentId/download" , ( req , res ) => {
304
312
shacaLoader . ensureLoad ( ) ;
305
313
306
314
let attachment : SAttachment | boolean ;
@@ -322,7 +330,7 @@ function register(router: Router) {
322
330
} ) ;
323
331
324
332
// 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 ) => {
326
334
shacaLoader . ensureLoad ( ) ;
327
335
328
336
let note : SNote | boolean ;
@@ -340,19 +348,18 @@ function register(router: Router) {
340
348
} ) ;
341
349
342
350
// 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 ) => {
344
352
shacaLoader . ensureLoad ( ) ;
345
353
346
354
const ancestorNoteId = req . query . ancestorNoteId ?? "_share" ;
347
- let note ;
348
355
349
356
if ( typeof ancestorNoteId !== "string" ) {
350
357
res . status ( 400 ) . json ( { message : "'ancestorNoteId' parameter is mandatory." } ) ;
351
358
return ;
352
359
}
353
360
354
361
// 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 ) ) {
356
363
return ;
357
364
}
358
365
0 commit comments