mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			121 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "use strict";
 | |
| 
 | |
| const noteService = require('../../services/notes');
 | |
| const dateNoteService = require('../../services/date_notes');
 | |
| const dateUtils = require('../../services/date_utils');
 | |
| const imageService = require('../../services/image');
 | |
| const messagingService = require('../../services/messaging');
 | |
| const log = require('../../services/log');
 | |
| const path = require('path');
 | |
| const Link = require('../../entities/link');
 | |
| 
 | |
| async function createNote(req) {
 | |
|     const {title, html, url, images} = req.body;
 | |
| 
 | |
|     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate());
 | |
| 
 | |
|     const {note} = await noteService.createNote(todayNote.noteId, title, html, {
 | |
|         attributes: [
 | |
|             {
 | |
|                 type: 'label',
 | |
|                 name: 'sourceUrl',
 | |
|                 value: url
 | |
|             }
 | |
|         ]
 | |
|     });
 | |
| 
 | |
|     let rewrittenHtml = html;
 | |
| 
 | |
|     for (const {src, dataUrl, imageId} of images) {
 | |
|         const filename = path.basename(src);
 | |
| 
 | |
|         if (!dataUrl.startsWith("data:image")) {
 | |
|             log.info("Image could not be recognized as data URL:", dataUrl.substr(0, Math.min(100, dataUrl.length)));
 | |
|             continue;
 | |
|         }
 | |
| 
 | |
|         const buffer = Buffer.from(dataUrl.split(",")[1], 'base64');
 | |
| 
 | |
|         const {note: imageNote, url} = await imageService.saveImage(buffer, filename, note.noteId, true);
 | |
| 
 | |
|         await new Link({
 | |
|             noteId: note.noteId,
 | |
|             targetNoteId: imageNote.noteId,
 | |
|             type: 'image'
 | |
|         }).save();
 | |
| 
 | |
|         console.log(`Replacing ${imageId} with ${url}`);
 | |
| 
 | |
|         rewrittenHtml = rewrittenHtml.replace(imageId, url);
 | |
|     }
 | |
| 
 | |
|     console.log("Done", rewrittenHtml);
 | |
| 
 | |
|     await note.setContent(rewrittenHtml);
 | |
| 
 | |
|     return {
 | |
|         noteId: note.noteId
 | |
|     };
 | |
| }
 | |
| 
 | |
| async function createImage(req) {
 | |
|     let {dataUrl, title, sourceUrl, pageUrl} = req.body;
 | |
| 
 | |
|     if (!dataUrl) {
 | |
|         dataUrl = sourceUrl;
 | |
|         sourceUrl = null;
 | |
|     }
 | |
| 
 | |
|     if (!dataUrl.startsWith("data:image/")) {
 | |
|         const message = "Unrecognized prefix: " + dataUrl.substr(0, Math.min(dataUrl.length, 100));
 | |
|         log.info(message);
 | |
| 
 | |
|         return [400, message];
 | |
|     }
 | |
| 
 | |
|     if (!title && sourceUrl) {
 | |
|         title = path.basename(sourceUrl);
 | |
|     }
 | |
| 
 | |
|     if (!title) {
 | |
|         title = "clipped image";
 | |
|     }
 | |
| 
 | |
|     const buffer = Buffer.from(dataUrl.split(",")[1], 'base64');
 | |
| 
 | |
|     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate());
 | |
| 
 | |
|     const {note} = await imageService.saveImage(buffer, title, todayNote.noteId, true);
 | |
| 
 | |
|     if (sourceUrl) {
 | |
|         await note.setLabel('sourceUrl', sourceUrl);
 | |
|     }
 | |
| 
 | |
|     if (pageUrl) {
 | |
|         await note.setLabel('pageUrl', pageUrl);
 | |
|     }
 | |
| 
 | |
|     return {
 | |
|         noteId: note.noteId
 | |
|     };
 | |
| }
 | |
| 
 | |
| async function openNote(req) {
 | |
|     messagingService.sendMessageToAllClients({
 | |
|         type: 'open-note',
 | |
|         noteId: req.params.noteId
 | |
|     });
 | |
| }
 | |
| 
 | |
| async function ping(req, res) {
 | |
|     console.log("PING!!!!");
 | |
| 
 | |
|     res.status(200).send("TriliumClipperServer");
 | |
| }
 | |
| 
 | |
| module.exports = {
 | |
|     createNote,
 | |
|     createImage,
 | |
|     openNote,
 | |
|     ping
 | |
| }; |