| 
									
										
										
										
											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-05 23:10:35 +01:00
										 |  |  | const {JSDOM} = require("jsdom"); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getSubRoot(note) { | 
					
						
							|  |  |  |     if (note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const parentNote = note.getParentNotes()[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (parentNote.noteId === shareRoot.SHARE_ROOT_NOTE_ID) { | 
					
						
							|  |  |  |         return note; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return getSubRoot(parentNote); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  | const NO_CONTENT = '<p>This note has no content.</p>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getChildrenList(note) { | 
					
						
							|  |  |  |     if (note.hasChildren()) { | 
					
						
							|  |  |  |         const document = new JSDOM().window.document; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const ulEl = document.createElement("ul"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const childNote of note.getChildNotes()) { | 
					
						
							|  |  |  |             const li = document.createElement("li"); | 
					
						
							|  |  |  |             const link = document.createElement("a"); | 
					
						
							|  |  |  |             link.appendChild(document.createTextNode(childNote.title)); | 
					
						
							|  |  |  |             link.setAttribute("href", childNote.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             li.appendChild(link); | 
					
						
							|  |  |  |             ulEl.appendChild(li); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return '<p>Child notes:</p>' + ulEl.outerHTML; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return ''; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getContent(note) { | 
					
						
							|  |  |  |     let content = note.getContent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (note.type === 'text') { | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |         const document = new JSDOM(content || "").window.document; | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |         const isEmpty = document.body.textContent.trim().length === 0 | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  |                 && document.querySelectorAll("img").length === 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isEmpty) { | 
					
						
							|  |  |  |             content = NO_CONTENT + getChildrenList(note); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |         else { | 
					
						
							|  |  |  |             for (const linkEl of document.querySelectorAll("a")) { | 
					
						
							|  |  |  |                 const href = linkEl.getAttribute("href"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (href?.startsWith("#")) { | 
					
						
							|  |  |  |                     const notePathSegments = href.split("/"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     linkEl.setAttribute("href", notePathSegments[notePathSegments.length - 1]); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             content = document.body.innerHTML; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (note.type === 'code') { | 
					
						
							|  |  |  |         if (!content?.trim()) { | 
					
						
							|  |  |  |             content = NO_CONTENT + getChildrenList(note); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             const document = new JSDOM().window.document; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const preEl = document.createElement('pre'); | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |             preEl.appendChild(document.createTextNode(content)); | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             content = preEl.outerHTML; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  |     else if (note.type === 'image') { | 
					
						
							|  |  |  |         content = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (note.type === 'file') { | 
					
						
							|  |  |  |         content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  |     else if (note.type === 'book') { | 
					
						
							|  |  |  |         content = getChildrenList(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         content = '<p>This note type cannot be displayed.</p>' + getChildrenList(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 22:01:06 +01:00
										 |  |  |     return content; | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  | function register(router) { | 
					
						
							|  |  |  |     router.get('/share/:noteId', (req, res, next) => { | 
					
						
							|  |  |  |         const {noteId} = req.params; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         shacaLoader.ensureLoad(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (noteId in shaca.notes) { | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |             const note = shaca.notes[noteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  |             const content = getContent(note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |             const subRoot = getSubRoot(note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             res.render("share", { | 
					
						
							|  |  |  |                 note, | 
					
						
							| 
									
										
										
										
											2021-12-05 23:10:35 +01:00
										 |  |  |                 content, | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  |                 subRoot | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2021-10-17 14:44:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             res.send("FFF"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-10-19 22:48:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     router.get('/share/api/images/:noteId/:filename', (req, res, next) => { | 
					
						
							|  |  |  |         const image = shaca.getNote(req.params.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!image) { | 
					
						
							|  |  |  |             return res.sendStatus(404); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (image.type !== 'image') { | 
					
						
							|  |  |  |             return res.sendStatus(400); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.set('Content-Type', image.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.send(image.getContent()); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-12-06 22:53:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     router.get('/share/api/notes/:noteId/:download', (req, res, next) => { | 
					
						
							|  |  |  |         const {noteId} = req.params; | 
					
						
							|  |  |  |         const note = shaca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!note) { | 
					
						
							|  |  |  |             return res.status(404).send(`Note ${noteId} doesn't exist.`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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-10-17 14:44:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     register | 
					
						
							|  |  |  | } |