| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | const image = require('../../services/image'); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | const multer = require('multer')(); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | const wrap = require('express-promise-wrap').wrap; | 
					
						
							| 
									
										
										
										
											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-01-29 17:41:59 -05:00
										 |  |  |     const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!image) { | 
					
						
							|  |  |  |         return res.status(404).send({}); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     res.set('Content-Type', 'image/' + image.format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(image.data); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | async function uploadImage(req, res) { | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											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-01-29 17:41:59 -05:00
										 |  |  |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							| 
									
										
										
										
											2018-01-07 09:22:55 -05:00
										 |  |  |         return res.status(404).send(`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)) { | 
					
						
							|  |  |  |         return res.status(400).send("Unknown image type: " + file.mimetype); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     const {fileName, imageId} = await image.saveImage(file, sourceId, noteId); | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({ | 
					
						
							|  |  |  |         uploaded: true, | 
					
						
							| 
									
										
										
										
											2018-01-07 08:24:04 -05:00
										 |  |  |         url: `/api/images/${imageId}/${fileName}` | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05: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 | 
					
						
							|  |  |  | }; |