| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-15 21:08:31 +02:00
										 |  |  |     else if (image.isDeleted || image.data === null) { | 
					
						
							| 
									
										
										
										
											2018-01-07 14:07:59 -05:00
										 |  |  |         res.set('Content-Type', 'image/png'); | 
					
						
							|  |  |  |         return res.send(fs.readFileSync(RESOURCE_DIR + '/db/image-deleted.png')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-25 11:28:44 +01:00
										 |  |  |     image.mime = image.mime.replace("image/svg", "image/svg+xml"); | 
					
						
							| 
									
										
										
										
											2018-11-08 10:30:35 +01:00
										 |  |  |     res.set('Content-Type', image.mime); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  |     res.send(await image.getContent()); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |     const {noteId} = req.query; | 
					
						
							|  |  |  |     const {file} = req; | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 15:22:05 +02:00
										 |  |  |     if (!["image/png", "image/jpeg", "image/gif", "image/webp"].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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |     const {url} = await imageService.saveImage(noteId, file.buffer, file.originalname, true); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  | async function updateImage(req) { | 
					
						
							|  |  |  |     const {noteId} = req.params; | 
					
						
							|  |  |  |     const {file} = req; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const note = await repository.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							|  |  |  |         return [404, `Note ${noteId} doesn't exist.`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!["image/png", "image/jpeg", "image/gif", "image/webp"].includes(file.mimetype)) { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             uploaded: false, | 
					
						
							|  |  |  |             message: "Unknown image type: " + file.mimetype | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await imageService.updateImage(noteId, file.buffer, file.originalname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { uploaded: true }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     returnImage, | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |     uploadImage, | 
					
						
							|  |  |  |     updateImage | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | }; |