| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  | const shaca = require("./shaca/shaca"); | 
					
						
							|  |  |  | const shacaLoader = require("./shaca/shaca_loader"); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | const shareRoot = require("./share_root"); | 
					
						
							| 
									
										
										
										
											2021-12-22 09:10:38 +01:00
										 |  |  | const contentRenderer = require("./content_renderer.js"); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 20:54:48 +01:00
										 |  |  | function getSharedSubTreeRoot(note) { | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |     if (note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) { | 
					
						
							| 
									
										
										
										
											2021-12-23 20:54:48 +01:00
										 |  |  |         // share root itself is not shared
 | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 20:54:48 +01:00
										 |  |  |     // every path leads to share root, but which one to choose?
 | 
					
						
							|  |  |  |     // for sake of simplicity URLs are not note paths
 | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |     const parentNote = note.getParentNotes()[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (parentNote.noteId === shareRoot.SHARE_ROOT_NOTE_ID) { | 
					
						
							|  |  |  |         return note; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 20:54:48 +01:00
										 |  |  |     return getSharedSubTreeRoot(parentNote); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function register(router) { | 
					
						
							| 
									
										
										
										
											2021-12-22 10:57:02 +01:00
										 |  |  |     router.get('/share/:shareId', (req, res, next) => { | 
					
						
							|  |  |  |         const {shareId} = req.params; | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         shacaLoader.ensureLoad(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 10:57:02 +01:00
										 |  |  |         const note = shaca.aliasToNote[shareId] || shaca.notes[shareId]; | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 10:57:02 +01:00
										 |  |  |         if (note) { | 
					
						
							| 
									
										
										
										
											2021-12-27 20:48:14 +01:00
										 |  |  |             const {header, content, isEmpty} = contentRenderer.getContent(note); | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 20:54:48 +01:00
										 |  |  |             const subRoot = getSharedSubTreeRoot(note); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 09:10:38 +01:00
										 |  |  |             res.render("share/page", { | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |                 note, | 
					
						
							| 
									
										
										
										
											2021-12-27 20:48:14 +01:00
										 |  |  |                 header, | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  |                 content, | 
					
						
							| 
									
										
										
										
											2021-12-27 20:48:14 +01:00
										 |  |  |                 isEmpty, | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |                 subRoot | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2021-12-22 09:10:38 +01:00
										 |  |  |             res.status(404).render("share/404"); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 13:23:09 +01:00
										 |  |  |     router.get('/share/api/notes/:noteId', (req, res, next) => { | 
					
						
							|  |  |  |         const {noteId} = req.params; | 
					
						
							|  |  |  |         const note = shaca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 13:23:09 +01:00
										 |  |  |         if (!note) { | 
					
						
							|  |  |  |             return res.status(404).send(`Note ${noteId} not found`); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 13:23:09 +01:00
										 |  |  |         res.json(note.getPojoWithAttributes()); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 09:36:38 +01:00
										 |  |  |     router.get('/share/api/notes/:noteId/download', (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |         const {noteId} = req.params; | 
					
						
							|  |  |  |         const note = shaca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!note) { | 
					
						
							| 
									
										
										
										
											2022-01-01 13:23:09 +01:00
										 |  |  |             return res.status(404).send(`Note ${noteId} not found`); | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const utils = require("../services/utils"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const filename = utils.formatDownloadTitle(note.title, note.type, note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); | 
					
						
							|  |  |  |         res.setHeader('Content-Type', note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.send(note.getContent()); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-12-27 20:48:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 13:23:09 +01:00
										 |  |  |     router.get('/share/api/images/:noteId/:filename', (req, res, next) => { | 
					
						
							|  |  |  |         const image = shaca.getNote(req.params.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!image) { | 
					
						
							|  |  |  |             return res.status(404).send(`Note ${noteId} not found`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (image.type !== 'image') { | 
					
						
							|  |  |  |             return res.status(400).send("Requested note is not an image"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.set('Content-Type', image.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.send(image.getContent()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // used for PDF viewing
 | 
					
						
							| 
									
										
										
										
											2021-12-24 21:36:31 +00:00
										 |  |  |     router.get('/share/api/notes/:noteId/view', (req, res, next) => { | 
					
						
							|  |  |  |         const {noteId} = req.params; | 
					
						
							|  |  |  |         const note = shaca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!note) { | 
					
						
							| 
									
										
										
										
											2022-01-01 13:23:09 +01:00
										 |  |  |             return res.status(404).send(`Note ${noteId} not found`); | 
					
						
							| 
									
										
										
										
											2021-12-24 21:36:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); | 
					
						
							|  |  |  |         res.setHeader('Content-Type', note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.send(note.getContent()); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     register | 
					
						
							|  |  |  | } |