| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:05:09 -04:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  | const noteCacheService = require('../../services/note_cache'); | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | const protectedSessionService = require('../../services/protected_session'); | 
					
						
							|  |  |  | const utils = require('../../services/utils'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | async function getNoteRevisions(req) { | 
					
						
							| 
									
										
										
										
											2019-09-08 09:17:16 +02:00
										 |  |  |     return await repository.getEntities(`
 | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |         SELECT * FROM note_revisions  | 
					
						
							|  |  |  |         WHERE noteId = ? AND isErased = 0 | 
					
						
							|  |  |  |         ORDER BY utcDateCreated DESC`, [req.params.noteId]);
 | 
					
						
							| 
									
										
										
										
											2019-09-08 09:17:16 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  | async function getNoteRevision(req) { | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  |     const noteRevision = await repository.getNoteRevision(req.params.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2019-09-08 09:17:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  |     if (noteRevision.type === 'file') { | 
					
						
							|  |  |  |         if (noteRevision.isStringNote()) { | 
					
						
							|  |  |  |             await noteRevision.getContent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteRevision.content = noteRevision.content.substr(0, 10000); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |         await noteRevision.getContent(); | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |         if (noteRevision.content && noteRevision.type === 'image') { | 
					
						
							|  |  |  |             noteRevision.content = noteRevision.content.toString('base64'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-08 23:09:57 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 20:00:56 +01:00
										 |  |  |     return noteRevision; | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {NoteRevision} noteRevision | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function getRevisionFilename(noteRevision) { | 
					
						
							|  |  |  |     let filename = noteRevision.title || "untitled"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteRevision.type === 'text') { | 
					
						
							|  |  |  |         filename += '.html'; | 
					
						
							|  |  |  |     } else if (['relation-map', 'search'].includes(noteRevision.type)) { | 
					
						
							|  |  |  |         filename += '.json'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const extension = path.extname(filename); | 
					
						
							|  |  |  |     const date = noteRevision.dateCreated | 
					
						
							|  |  |  |         .substr(0, 19) | 
					
						
							|  |  |  |         .replace(' ', '_') | 
					
						
							|  |  |  |         .replace(/[^0-9_]/g, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (extension) { | 
					
						
							|  |  |  |         filename = filename.substr(0, filename.length - extension.length) | 
					
						
							|  |  |  |             + '-' + date + extension; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         filename += '-' + date; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return filename; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function downloadNoteRevision(req, res) { | 
					
						
							|  |  |  |     const noteRevision = await repository.getNoteRevision(req.params.noteRevisionId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteRevision.noteId !== req.params.noteId) { | 
					
						
							|  |  |  |         return res.status(400).send(`Note revision ${req.params.noteRevisionId} does not belong to note ${req.params.noteId}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteRevision.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |         return res.status(401).send("Protected session not available"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const filename = getRevisionFilename(noteRevision); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | 
					
						
							|  |  |  |     res.setHeader('Content-Type', noteRevision.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(await noteRevision.getContent()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 16:51:51 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {NoteRevision} noteRevision | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  | async function eraseOneNoteRevision(noteRevision) { | 
					
						
							|  |  |  |     noteRevision.isErased = true; | 
					
						
							|  |  |  |     noteRevision.title = null; | 
					
						
							|  |  |  |     await noteRevision.setContent(null); | 
					
						
							|  |  |  |     await noteRevision.save(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function eraseAllNoteRevisions(req) { | 
					
						
							|  |  |  |     const noteRevisionsToErase = await repository.getEntities( | 
					
						
							|  |  |  |         'SELECT * FROM note_revisions WHERE isErased = 0 AND noteId = ?', | 
					
						
							|  |  |  |         [req.params.noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const noteRevision of noteRevisionsToErase) { | 
					
						
							|  |  |  |         await eraseOneNoteRevision(noteRevision); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function eraseNoteRevision(req) { | 
					
						
							|  |  |  |     const noteRevision = await repository.getNoteRevision(req.params.noteRevisionId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (noteRevision && !noteRevision.isErased) { | 
					
						
							|  |  |  |         await eraseOneNoteRevision(noteRevision); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  | async function getEditedNotesOnDate(req) { | 
					
						
							|  |  |  |     const date = req.params.date; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const notes = await repository.getEntities(`
 | 
					
						
							|  |  |  |         select distinct notes.* | 
					
						
							|  |  |  |         from notes | 
					
						
							|  |  |  |         left join note_revisions using (noteId) | 
					
						
							|  |  |  |         where substr(notes.dateCreated, 0, 11) = ? | 
					
						
							|  |  |  |            or substr(notes.dateModified, 0, 11) = ? | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |            or substr(note_revisions.dateLastEdited, 0, 11) = ?`, [date, date, date]);
 | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const note of notes) { | 
					
						
							|  |  |  |         const notePath = noteCacheService.getNotePath(note.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         note.notePath = notePath ? notePath.notePath : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return notes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2019-09-07 10:11:59 +02:00
										 |  |  |     getNoteRevisions, | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  |     getNoteRevision, | 
					
						
							| 
									
										
										
										
											2019-11-09 08:53:13 +01:00
										 |  |  |     downloadNoteRevision, | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |     getEditedNotesOnDate, | 
					
						
							|  |  |  |     eraseAllNoteRevisions, | 
					
						
							|  |  |  |     eraseNoteRevision | 
					
						
							| 
									
										
										
										
											2018-03-30 13:56:46 -04:00
										 |  |  | }; |