| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 12:52:50 +01:00
										 |  |  | const imageService = require('../../services/image'); | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  | const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | async function returnImage(req, res) { | 
					
						
							| 
									
										
										
										
											2018-11-08 10:30:35 +01:00
										 |  |  |     const image = await repository.getNote(req.params.noteId); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!image) { | 
					
						
							| 
									
										
										
										
											2018-03-30 19:31:22 -04:00
										 |  |  |         return res.sendStatus(404); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-08 10:30:35 +01:00
										 |  |  |     else if (image.type !== 'image') { | 
					
						
							|  |  |  |         return res.sendStatus(400); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  |     else if (image.data === null) { | 
					
						
							|  |  |  |         res.set('Content-Type', 'image/png'); | 
					
						
							|  |  |  |         return res.send(fs.readFileSync(RESOURCE_DIR + '/db/image-deleted.png')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 10:30:35 +01:00
										 |  |  |     res.set('Content-Type', image.mime); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 21:29:23 +01:00
										 |  |  |     res.send((await note.getNoteContent()).content); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 19:31:22 -04:00
										 |  |  | async function uploadImage(req) { | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     const noteId = req.query.noteId; | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  |     const file = req.file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |     const note = await repository.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							| 
									
										
										
										
											2018-03-30 19:31:22 -04:00
										 |  |  |         return [404, `Note ${noteId} doesn't exist.`]; | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 09:22:55 -05:00
										 |  |  |     if (!["image/png", "image/jpeg", "image/gif"].includes(file.mimetype)) { | 
					
						
							| 
									
										
										
										
											2018-03-30 19:31:22 -04:00
										 |  |  |         return [400, "Unknown image type: " + file.mimetype]; | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 12:52:50 +01:00
										 |  |  |     const {url} = await imageService.saveImage(file.buffer, file.originalname, noteId); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 19:31:22 -04:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  |         uploaded: true, | 
					
						
							| 
									
										
										
										
											2018-11-05 12:52:50 +01:00
										 |  |  |         url | 
					
						
							| 
									
										
										
										
											2018-03-30 19:31:22 -04:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     returnImage, | 
					
						
							|  |  |  |     uploadImage | 
					
						
							|  |  |  | }; |