2018-11-16 19:12:04 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const noteService = require('../../services/notes');
|
|
|
|
const parseString = require('xml2js').parseString;
|
2019-02-26 04:22:57 +08:00
|
|
|
const protectedSessionService = require('../protected_session');
|
2018-11-16 19:12:04 +08:00
|
|
|
|
2019-02-11 02:36:03 +08:00
|
|
|
/**
|
2019-10-18 03:11:35 +08:00
|
|
|
* @param {TaskContext} taskContext
|
2019-02-11 02:36:03 +08:00
|
|
|
* @param {Buffer} fileBuffer
|
|
|
|
* @param {Note} parentNote
|
|
|
|
* @return {Promise<*[]|*>}
|
|
|
|
*/
|
2019-10-18 03:11:35 +08:00
|
|
|
async function importOpml(taskContext, fileBuffer, parentNote) {
|
2018-11-16 19:12:04 +08:00
|
|
|
const xml = await new Promise(function(resolve, reject)
|
|
|
|
{
|
2018-11-16 21:36:50 +08:00
|
|
|
parseString(fileBuffer, function (err, result) {
|
2018-11-16 19:12:04 +08:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-02-17 05:13:29 +08:00
|
|
|
if (!['1.0', '1.1', '2.0'].includes(xml.opml.$.version)) {
|
|
|
|
return [400, 'Unsupported OPML version ' + xml.opml.$.version + ', 1.0, 1.1 or 2.0 expected instead.'];
|
|
|
|
}
|
|
|
|
|
|
|
|
const opmlVersion = parseInt(xml.opml.$.version);
|
|
|
|
|
|
|
|
async function importOutline(outline, parentNoteId) {
|
|
|
|
let title, content;
|
|
|
|
|
|
|
|
if (opmlVersion === 1) {
|
|
|
|
title = outline.$.title;
|
|
|
|
content = toHtml(outline.$.text);
|
|
|
|
}
|
|
|
|
else if (opmlVersion === 2) {
|
|
|
|
title = outline.$.text;
|
|
|
|
content = outline.$._note; // _note is already HTML
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("Unrecognized OPML version " + opmlVersion);
|
|
|
|
}
|
|
|
|
|
2019-11-16 18:09:52 +08:00
|
|
|
const {note} = await noteService.createNewNote({
|
|
|
|
parentNoteId,
|
|
|
|
title,
|
|
|
|
content,
|
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
|
2019-02-26 04:22:57 +08:00
|
|
|
});
|
2019-02-17 05:13:29 +08:00
|
|
|
|
2019-10-18 03:11:35 +08:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-17 05:13:29 +08:00
|
|
|
|
|
|
|
for (const childOutline of (outline.outline || [])) {
|
|
|
|
await importOutline(childOutline, note.noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
2018-11-16 19:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const outlines = xml.opml.body[0].outline || [];
|
|
|
|
let returnNote = null;
|
|
|
|
|
|
|
|
for (const outline of outlines) {
|
2019-02-17 05:13:29 +08:00
|
|
|
const note = await importOutline(outline, parentNote.noteId);
|
2018-11-16 19:12:04 +08:00
|
|
|
|
|
|
|
// first created note will be activated after import
|
|
|
|
returnNote = returnNote || note;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnNote;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toHtml(text) {
|
|
|
|
if (!text) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<p>' + text.replace(/(?:\r\n|\r|\n)/g, '</p><p>') + '</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
importOpml
|
|
|
|
};
|