| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const notes = require('../../services/notes'); | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | const labels = require('../../services/labels'); | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  | const protected_session = 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 11:42:12 -04:00
										 |  |  |     const {note} = await notes.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-04-01 11:42:12 -04:00
										 |  |  |     await labels.createLabel(note.noteId, "original_file_name", originalName); | 
					
						
							|  |  |  |     await labels.createLabel(note.noteId, "file_size", 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 11:42:12 -04:00
										 |  |  |     if (note.isProtected && !protected_session.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |         res.status(401).send("Protected session not available"); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     const labelMap = await note.getLabelMap(); | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     const fileName = labelMap.original_file_name ? labelMap.original_file_name : 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 | 
					
						
							|  |  |  | }; |