| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-11 11:56:36 +02:00
										 |  |  | const excalidrawToSvg = require("excalidraw-to-svg"); | 
					
						
							| 
									
										
										
										
											2018-11-05 12:52:50 +01:00
										 |  |  | const imageService = require('../../services/image'); | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  | const becca = require('../../becca/becca'); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function returnImage(req, res) { | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const image = becca.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
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-04-11 11:56:36 +02:00
										 |  |  |     else if (!["image", "canvas-note"].includes(image.type)){ | 
					
						
							| 
									
										
										
										
											2018-11-08 10:30:35 +01:00
										 |  |  |         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 18:21:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-11 11:56:36 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * special "image" type. the canvas-note is actually type application/json but can be | 
					
						
							|  |  |  |      * rendered on the fly to svg. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (image.type === 'canvas-note') { | 
					
						
							|  |  |  |         // render the svg in node.js using excalidraw and jsdom
 | 
					
						
							|  |  |  |         const content = image.getContent(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             const data = JSON.parse(content) | 
					
						
							|  |  |  |             const excalidrawData = { | 
					
						
							|  |  |  |                 type: "excalidraw", | 
					
						
							|  |  |  |                 version: 2, | 
					
						
							|  |  |  |                 source: "trilium", | 
					
						
							|  |  |  |                 elements: data.elements, | 
					
						
							|  |  |  |                 appState: data.appState, | 
					
						
							|  |  |  |                 files: data.files, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             excalidrawToSvg(excalidrawData) | 
					
						
							|  |  |  |                 .then(svg => { | 
					
						
							|  |  |  |                     const svgHtml = svg.outerHTML; | 
					
						
							|  |  |  |                     res.set('Content-Type', "image/svg+xml"); | 
					
						
							|  |  |  |                     res.set("Cache-Control", "no-cache, no-store, must-revalidate"); | 
					
						
							|  |  |  |                     res.send(svgHtml); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |         } catch(err) { | 
					
						
							| 
									
										
										
										
											2022-04-11 21:38:22 +02:00
										 |  |  |             res.status(500).send("there was an error parsing excalidraw to svg"); | 
					
						
							| 
									
										
										
										
											2022-04-11 11:56:36 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         res.set('Content-Type', image.mime); | 
					
						
							|  |  |  |         res.set("Cache-Control", "no-cache, no-store, must-revalidate"); | 
					
						
							|  |  |  |         res.send(image.getContent()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-05 23:54:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 22:02:38 +02:00
										 |  |  |     if (!["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 21:48:46 +01:00
										 |  |  |     const {url} = imageService.saveImage(noteId, file.buffer, file.originalname, true, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateImage(req) { | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |     const {noteId} = req.params; | 
					
						
							|  |  |  |     const {file} = req; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							|  |  |  |         return [404, `Note ${noteId} doesn't exist.`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 23:10:45 +02:00
										 |  |  |     if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) { | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |         return { | 
					
						
							|  |  |  |             uploaded: false, | 
					
						
							|  |  |  |             message: "Unknown image type: " + file.mimetype | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     imageService.updateImage(noteId, file.buffer, file.originalname); | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { uploaded: true }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:07:41 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     returnImage, | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |     uploadImage, | 
					
						
							|  |  |  |     updateImage | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |