mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	save image
This commit is contained in:
		| @@ -55,24 +55,41 @@ async function createNote(req) { | |||||||
|     return {}; |     return {}; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function createScreenshot(req) { | async function createImage(req) { | ||||||
|     console.log(req.body); |     let {dataUrl, title, sourceUrl, pageUrl} = req.body; | ||||||
|  |  | ||||||
|     const {imageDataUrl, title, url} = req.body; |     if (!dataUrl) { | ||||||
|  |         dataUrl = sourceUrl; | ||||||
|     const prefix = "data:image/png;base64,"; |         sourceUrl = null; | ||||||
|  |  | ||||||
|     if (imageDataUrl.startsWith(prefix)) { |  | ||||||
|         const buffer = Buffer.from(imageDataUrl.substr(prefix.length), 'base64'); |  | ||||||
|  |  | ||||||
|         const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); |  | ||||||
|  |  | ||||||
|         const {note} = await imageService.saveImage(buffer, title + ".png", todayNote.noteId, true); |  | ||||||
|  |  | ||||||
|         await note.setLabel('sourceUrl', url); |  | ||||||
|     } |     } | ||||||
|     else { |  | ||||||
|         console.log("Unrecognized prefix"); |     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 {}; |     return {}; | ||||||
| @@ -86,6 +103,6 @@ async function ping(req, res) { | |||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     createNote, |     createNote, | ||||||
|     createScreenshot, |     createImage, | ||||||
|     ping |     ping | ||||||
| }; | }; | ||||||
| @@ -225,7 +225,7 @@ function register(app) { | |||||||
|     apiRoute(POST, '/api/login/protected', loginApiRoute.loginToProtectedSession); |     apiRoute(POST, '/api/login/protected', loginApiRoute.loginToProtectedSession); | ||||||
|  |  | ||||||
|     route(POST, '/api/clipper/notes', [], clipperRoute.createNote, apiResultHandler); |     route(POST, '/api/clipper/notes', [], clipperRoute.createNote, apiResultHandler); | ||||||
|     route(POST, '/api/clipper/screenshot', [], clipperRoute.createScreenshot, apiResultHandler); |     route(POST, '/api/clipper/image', [], clipperRoute.createImage, apiResultHandler); | ||||||
|     route(GET, '/api/clipper/ping', [], clipperRoute.ping); |     route(GET, '/api/clipper/ping', [], clipperRoute.ping); | ||||||
|  |  | ||||||
|     app.use('', router); |     app.use('', router); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user