save note in clipper

This commit is contained in:
zadam
2019-07-06 16:48:06 +02:00
parent 093dfb4a39
commit 976684a3a8
5 changed files with 39 additions and 41 deletions

View File

@@ -10,48 +10,44 @@ const path = require('path');
const Link = require('../../entities/link');
async function createNote(req) {
const {title, html, url, images} = req.body;
const {title, content, 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
}
]
});
const {note} = await noteService.createNote(todayNote.noteId, title, content);
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);
if (url) {
await note.setLabel('sourceUrl', url);
}
console.log("Done", rewrittenHtml);
let rewrittenContent = content;
await note.setContent(rewrittenHtml);
if (images) {
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}`);
rewrittenContent = rewrittenContent.replace(imageId, url);
}
}
await note.setContent(rewrittenContent);
return {
noteId: note.noteId
@@ -105,6 +101,8 @@ async function openNote(req) {
type: 'open-note',
noteId: req.params.noteId
});
return {};
}
async function ping(req, res) {