| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const noteService = require('../../services/notes'); | 
					
						
							|  |  |  | const dateNoteService = require('../../services/date_notes'); | 
					
						
							|  |  |  | const dateUtils = require('../../services/date_utils'); | 
					
						
							|  |  |  | const imageService = require('../../services/image'); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | const appInfo = require('../../services/app_info'); | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  | const messagingService = require('../../services/messaging'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const Link = require('../../entities/link'); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | async function findClippingNote(todayNote, pageUrl) { | 
					
						
							|  |  |  |     const notes = await todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const note of notes) { | 
					
						
							|  |  |  |         if (await note.getLabelValue('clipType') === 'clippings') { | 
					
						
							|  |  |  |             return note; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function addClipping(req) { | 
					
						
							|  |  |  |     const {title, content, pageUrl, images} = req.body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let clippingNote = await findClippingNote(todayNote, pageUrl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!clippingNote) { | 
					
						
							|  |  |  |         clippingNote = (await noteService.createNote(todayNote.noteId, title, '')).note; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await clippingNote.setLabel('clipType', 'clippings'); | 
					
						
							|  |  |  |         await clippingNote.setLabel('pageUrl', pageUrl); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const rewrittenContent = await addImagesToNote(images, clippingNote, content); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-07 11:15:55 +02:00
										 |  |  |     await clippingNote.setContent(await clippingNote.getContent() + '<p>' + rewrittenContent + '</p>'); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         noteId: clippingNote.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | async function createNote(req) { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     const {title, content, pageUrl, images} = req.body; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     const {note} = await noteService.createNote(todayNote.noteId, title, content); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     await note.setLabel('clipType', 'note'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pageUrl) { | 
					
						
							|  |  |  |         await note.setLabel('pageUrl', pageUrl); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     const rewrittenContent = await addImagesToNote(images, note, content); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await note.setContent(rewrittenContent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         noteId: note.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function addImagesToNote(images, note, content) { | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     let rewrittenContent = content; | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     if (images) { | 
					
						
							|  |  |  |         for (const {src, dataUrl, imageId} of images) { | 
					
						
							|  |  |  |             const filename = path.basename(src); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             if (!dataUrl.startsWith("data:image")) { | 
					
						
							|  |  |  |                 log.info("Image could not be recognized as data URL:", dataUrl.substr(0, Math.min(100, dataUrl.length))); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             const buffer = Buffer.from(dataUrl.split(",")[1], 'base64'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             const {note: imageNote, url} = await imageService.saveImage(buffer, filename, note.noteId, true); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             await new Link({ | 
					
						
							|  |  |  |                 noteId: note.noteId, | 
					
						
							|  |  |  |                 targetNoteId: imageNote.noteId, | 
					
						
							|  |  |  |                 type: 'image' | 
					
						
							|  |  |  |             }).save(); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             console.log(`Replacing ${imageId} with ${url}`); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             rewrittenContent = rewrittenContent.replace(imageId, url); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     return rewrittenContent; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  | async function createImage(req) { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     let {dataUrl, title, imageUrl, pageUrl} = req.body; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     if (!dataUrl) { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |         // this is image inlined into the src attribute so there isn't any source image URL
 | 
					
						
							|  |  |  |         dataUrl = imageUrl; | 
					
						
							|  |  |  |         imageUrl = null; | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     if (!dataUrl.startsWith("data:image/")) { | 
					
						
							|  |  |  |         const message = "Unrecognized prefix: " + dataUrl.substr(0, Math.min(dataUrl.length, 100)); | 
					
						
							|  |  |  |         log.info(message); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |         return [400, message]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     if (!title && imageUrl) { | 
					
						
							|  |  |  |         title = path.basename(imageUrl); | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!title) { | 
					
						
							|  |  |  |         title = "clipped image"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     const buffer = Buffer.from(dataUrl.split(",")[1], 'base64'); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const {note} = await imageService.saveImage(buffer, title, todayNote.noteId, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     await note.setLabel('clipType', 'image'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (imageUrl) { | 
					
						
							|  |  |  |         await note.setLabel('imageUrl', imageUrl); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (pageUrl) { | 
					
						
							|  |  |  |         await note.setLabel('pageUrl', pageUrl); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 09:10:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         noteId: note.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function openNote(req) { | 
					
						
							|  |  |  |     messagingService.sendMessageToAllClients({ | 
					
						
							|  |  |  |         type: 'open-note', | 
					
						
							|  |  |  |         noteId: req.params.noteId | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | async function handshake() { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         appName: "trilium", | 
					
						
							|  |  |  |         appVersion: appInfo.appVersion | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     createNote, | 
					
						
							| 
									
										
										
										
											2019-06-23 12:16:26 +02:00
										 |  |  |     createImage, | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     addClipping, | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  |     openNote, | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     handshake | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | }; |