| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const noteService = require('../../services/notes'); | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  | const attributeService = require('../../services/attributes'); | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | async function uploadFile(req) { | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     const parentNoteId = req.params.parentNoteId; | 
					
						
							|  |  |  |     const file = req.file; | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     const originalName = file.originalname; | 
					
						
							|  |  |  |     const size = file.size; | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     const parentNote = await repository.getNote(parentNoteId); | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     if (!parentNote) { | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |         return [404, `Note ${parentNoteId} doesn't exist.`]; | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     const {note} = await noteService.createNewNote(parentNoteId, { | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |         title: originalName, | 
					
						
							|  |  |  |         content: file.buffer, | 
					
						
							|  |  |  |         target: 'into', | 
					
						
							|  |  |  |         isProtected: false, | 
					
						
							|  |  |  |         type: 'file', | 
					
						
							|  |  |  |         mime: file.mimetype | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     await attributeService.createLabel(note.noteId, "originalFileName", originalName); | 
					
						
							|  |  |  |     await attributeService.createLabel(note.noteId, "fileSize", size); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |         noteId: note.noteId | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function downloadFile(req, res) { | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     const note = await 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()) { | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |         res.status(401).send("Protected session not available"); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 13:33:10 +02:00
										 |  |  |     const originalFileName = await note.getLabel('originalFileName'); | 
					
						
							|  |  |  |     const fileName = originalFileName.value || note.title; | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     res.setHeader('Content-Disposition', 'file; filename="' + fileName + '"'); | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     res.setHeader('Content-Type', note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(note.content); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     uploadFile, | 
					
						
							|  |  |  |     downloadFile | 
					
						
							|  |  |  | }; |