| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const protectedSessionService = require('../../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | const noteRevisionService = require('../../services/note_revisions'); | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateFile(req) { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     const {noteId} = req.params; | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     const file = req.file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     if (!note) { | 
					
						
							|  |  |  |         return [404, `Note ${noteId} doesn't exist.`]; | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     noteRevisionService.createNoteRevision(note); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     note.mime = file.mimetype.toLowerCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setContent(file.buffer); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setLabel('originalFileName', file.originalname); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     noteRevisionService.protectNoteRevisions(note); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         uploaded: true | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function downloadNoteFile(noteId, res, contentDisposition = true) { | 
					
						
							|  |  |  |     const note = repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  |         return res.status(404).send(`Note ${noteId} doesn't exist.`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     if (note.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  |         return res.status(401).send("Protected session not available"); | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     if (contentDisposition) { | 
					
						
							|  |  |  |         // (one) reason we're not using the originFileName (available as label) is that it's not
 | 
					
						
							|  |  |  |         // available for older note revisions and thus would be inconsistent
 | 
					
						
							| 
									
										
										
										
											2020-05-12 10:28:31 +02:00
										 |  |  |         const filename = utils.formatDownloadTitle(note.title, note.type, note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     res.setHeader('Content-Type', note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     res.send(note.getContent()); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function downloadFile(req, res) { | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     return downloadNoteFile(noteId, res); | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function openFile(req, res) { | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     return downloadNoteFile(noteId, res, false); | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     updateFile, | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     openFile, | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  |     downloadFile, | 
					
						
							|  |  |  |     downloadNoteFile | 
					
						
							| 
									
										
										
										
											2020-05-12 10:28:31 +02:00
										 |  |  | }; |