Notes/src/routes/api/clipper.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

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');
const appInfo = require('../../services/app_info');
2019-08-26 20:21:43 +02:00
const ws = require('../../services/ws.js');
2019-06-23 11:25:15 +02:00
const log = require('../../services/log');
const utils = require('../../services/utils');
2019-06-23 11:25:15 +02:00
const path = require('path');
2019-08-19 20:12:00 +02:00
const Attribute = require('../../entities/attribute');
2019-06-22 19:49:48 +02:00
async function findClippingNote(todayNote, pageUrl) {
const notes = await todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl);
for (const note of notes) {
if (await note.getOwnedLabelValue('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) {
2019-11-16 11:09:52 +01:00
clippingNote = (await noteService.createNewNote({
parentNoteId: todayNote.noteId,
title: title,
content: ''
})).note;
await clippingNote.setLabel('clipType', 'clippings');
await clippingNote.setLabel('pageUrl', pageUrl);
}
const rewrittenContent = await addImagesToNote(images, clippingNote, content);
await clippingNote.setContent(await clippingNote.getContent() + '<p>' + rewrittenContent + '</p>');
return {
noteId: clippingNote.noteId
};
}
2019-06-22 19:49:48 +02:00
async function createNote(req) {
2019-07-19 21:03:47 +02:00
const {title, content, pageUrl, images, clipType} = req.body;
2019-06-22 19:49:48 +02:00
const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate());
2019-11-16 11:09:52 +01:00
const {note} = await noteService.createNewNote({
parentNoteId: todayNote.noteId,
title,
content
});
2019-06-22 19:49:48 +02:00
2019-07-19 21:03:47 +02:00
await note.setLabel('clipType', clipType);
if (pageUrl) {
await note.setLabel('pageUrl', pageUrl);
2019-07-06 16:48:06 +02:00
}
2019-06-23 11:25:15 +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-11-08 22:34:30 +01:00
const {note: imageNote, url} = await imageService.saveImage(note.noteId, buffer, filename, true);
2019-06-23 11:25:15 +02:00
2019-08-19 20:12:00 +02:00
await new Attribute({
2019-07-06 16:48:06 +02:00
noteId: note.noteId,
2019-08-19 20:12:00 +02:00
type: 'relation',
value: imageNote.noteId,
name: 'imageLink'
2019-07-06 16:48:06 +02:00
}).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
return rewrittenContent;
2019-06-22 19:49:48 +02:00
}
2019-06-23 13:25:00 +02:00
async function openNote(req) {
if (utils.isElectron()) {
2019-08-26 20:21:43 +02:00
ws.sendMessageToAllClients({
type: 'open-note',
noteId: req.params.noteId
});
return {
result: 'ok'
};
}
else {
return {
result: 'open-in-browser'
}
}
2019-06-22 19:49:48 +02:00
}
async function handshake() {
return {
appName: "trilium",
2019-07-07 22:27:06 +02:00
protocolVersion: appInfo.clipperProtocolVersion
}
2019-06-22 19:49:48 +02:00
}
module.exports = {
createNote,
addClipping,
2019-06-23 13:25:00 +02:00
openNote,
handshake
2019-06-22 19:49:48 +02:00
};