added ImportContext

This commit is contained in:
zadam
2019-02-10 19:36:03 +01:00
parent 5baa251944
commit e4c78f3887
8 changed files with 118 additions and 58 deletions

View File

@@ -19,7 +19,7 @@ function parseDate(text) {
let note = {};
let resource;
async function importEnex(file, parentNote) {
async function importEnex(importContext, file, parentNote) {
const saxStream = sax.createStream(true);
const xmlBuilder = new xml2js.Builder({ headless: true });
const parser = new xml2js.Parser({ explicitArray: true });
@@ -218,6 +218,8 @@ async function importEnex(file, parentNote) {
mime: 'text/html'
})).note;
importContext.increaseCount();
const noteContent = await noteEntity.getNoteContent();
for (const resource of resources) {
@@ -232,39 +234,40 @@ async function importEnex(file, parentNote) {
}
const createResourceNote = async () => {
const resourceNote = (await noteService.createNote(noteEntity.noteId, resource.title, resource.content, {
attributes: resource.attributes,
type: 'file',
mime: resource.mime
})).note;
const resourceNote = (await noteService.createNote(noteEntity.noteId, resource.title, resource.content, {
attributes: resource.attributes,
type: 'file',
mime: resource.mime
})).note;
const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;
importContext.increaseCount();
noteContent.content = noteContent.content.replace(mediaRegex, resourceLink);
const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;
noteContent.content = noteContent.content.replace(mediaRegex, resourceLink);
};
if (["image/jpeg", "image/png", "image/gif"].includes(resource.mime)) {
try {
const originalName = "image." + resource.mime.substr(6);
try {
const originalName = "image." + resource.mime.substr(6);
const { url } = await imageService.saveImage(resource.content, originalName, noteEntity.noteId);
const {url} = await imageService.saveImage(resource.content, originalName, noteEntity.noteId);
const imageLink = `<img src="${url}">`;
const imageLink = `<img src="${url}">`;
noteContent.content = noteContent.content.replace(mediaRegex, imageLink);
noteContent.content = noteContent.content.replace(mediaRegex, imageLink);
if (!noteContent.content.includes(imageLink)) {
// if there wasn't any match for the reference, we'll add the image anyway
// otherwise image would be removed since no note would include it
noteContent.content += imageLink;
if (!noteContent.content.includes(imageLink)) {
// if there wasn't any match for the reference, we'll add the image anyway
// otherwise image would be removed since no note would include it
noteContent.content += imageLink;
}
} catch (e) {
log.error("error when saving image from ENEX file: " + e);
await createResourceNote();
}
} catch (e) {
log.error("error when saving image from ENEX file: " + e);
} else {
await createResourceNote();
}
}
else {
await createResourceNote();
}
}
@@ -295,7 +298,12 @@ async function importEnex(file, parentNote) {
return new Promise((resolve, reject) =>
{
// resolve only when we parse the whole document AND saving of all notes have been finished
saxStream.on("end", () => { Promise.all(saveNotePromises).then(() => resolve(rootNote)) });
saxStream.on("end", () => { Promise.all(saveNotePromises).then(() => {
importContext.importFinished(rootNote.noteId);
resolve(rootNote);
});
});
const bufferStream = new stream.PassThrough();
bufferStream.end(file.buffer);

View File

@@ -3,7 +3,13 @@
const noteService = require('../../services/notes');
const parseString = require('xml2js').parseString;
async function importOpml(fileBuffer, parentNote) {
/**
* @param {ImportContext} importContext
* @param {Buffer} fileBuffer
* @param {Note} parentNote
* @return {Promise<*[]|*>}
*/
async function importOpml(importContext, fileBuffer, parentNote) {
const xml = await new Promise(function(resolve, reject)
{
parseString(fileBuffer, function (err, result) {
@@ -24,7 +30,7 @@ async function importOpml(fileBuffer, parentNote) {
let returnNote = null;
for (const outline of outlines) {
const note = await importOutline(outline, parentNote.noteId);
const note = await importOutline(importContext, outline, parentNote.noteId);
// first created note will be activated after import
returnNote = returnNote || note;
@@ -41,9 +47,11 @@ function toHtml(text) {
return '<p>' + text.replace(/(?:\r\n|\r|\n)/g, '</p><p>') + '</p>';
}
async function importOutline(outline, parentNoteId) {
async function importOutline(importContext, outline, parentNoteId) {
const {note} = await noteService.createNote(parentNoteId, outline.$.title, toHtml(outline.$.text));
importContext.increaseCount();
for (const childOutline of (outline.outline || [])) {
await importOutline(childOutline, note.noteId);
}

View File

@@ -4,7 +4,7 @@ const noteService = require('../../services/notes');
const commonmark = require('commonmark');
const path = require('path');
async function importMarkdown(file, parentNote) {
async function importMarkdown(importContext, file, parentNote) {
const markdownContent = file.buffer.toString("UTF-8");
const reader = new commonmark.Parser();
@@ -20,10 +20,13 @@ async function importMarkdown(file, parentNote) {
mime: 'text/html'
});
importContext.increaseCount();
importContext.importFinished(note.noteId);
return note;
}
async function importHtml(file, parentNote) {
async function importHtml(importContext, file, parentNote) {
const title = getFileNameWithoutExtension(file.originalname);
const content = file.buffer.toString("UTF-8");
@@ -32,6 +35,9 @@ async function importHtml(file, parentNote) {
mime: 'text/html'
});
importContext.increaseCount();
importContext.importFinished(note.noteId);
return note;
}

View File

@@ -6,7 +6,6 @@ const utils = require('../../services/utils');
const log = require('../../services/log');
const repository = require('../../services/repository');
const noteService = require('../../services/notes');
const messagingService = require('../../services/messaging');
const Branch = require('../../entities/branch');
const tar = require('tar-stream');
const stream = require('stream');
@@ -17,7 +16,13 @@ const mimeTypes = require('mime-types');
let importNoteCount;
let lastSentCountTs = Date.now();
async function importTar(fileBuffer, importRootNote) {
/**
* @param {ImportContext} importContext
* @param {Buffer} fileBuffer
* @param {Note} importRootNote
* @return {Promise<*>}
*/
async function importTar(importContext, fileBuffer, importRootNote) {
importNoteCount = 0;
// maps from original noteId (in tar file) to newly generated noteId
@@ -337,13 +342,7 @@ async function importTar(fileBuffer, importRootNote) {
log.info("Ignoring tar import entry with type " + header.type);
}
importNoteCount++;
if (Date.now() - lastSentCountTs >= 1000) {
lastSentCountTs = Date.now();
messagingService.importNoteCount(importNoteCount);
}
importContext.increaseCount();
next(); // ready for next entry
});
@@ -379,7 +378,7 @@ async function importTar(fileBuffer, importRootNote) {
}
}
messagingService.importFinished(firstNote);
importContext.importFinished();
resolve(firstNote);
});