1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/CliClient/app/import-enex.js

325 lines
8.9 KiB
JavaScript
Raw Normal View History

2017-06-06 00:00:02 +02:00
require('app-module-path').addPath(__dirname);
2017-06-24 20:06:28 +02:00
import { uuid } from 'lib/uuid.js';
2017-06-06 23:40:09 +02:00
import moment from 'moment';
2017-06-24 20:06:28 +02:00
import { promiseChain } from 'lib/promise-utils.js';
import { folderItemFilename } from 'lib/string-utils.js'
import { BaseModel } from 'lib/base-model.js';
import { Note } from 'lib/models/note.js';
2017-06-24 20:51:43 +02:00
import { Resource } from 'lib/models/resource.js';
2017-06-24 20:06:28 +02:00
import { Folder } from 'lib/models/folder.js';
2017-06-25 01:19:11 +02:00
import { enexXmlToMd } from './import-enex-md-gen.js';
2017-06-09 00:24:40 +02:00
import jsSHA from "jssha";
2017-06-06 23:40:09 +02:00
2017-06-06 23:27:03 +02:00
const Promise = require('promise');
2017-06-24 20:51:43 +02:00
const fs = require('fs-extra');
2017-06-09 00:24:40 +02:00
const stringToStream = require('string-to-stream')
2017-06-24 19:40:03 +02:00
let existingTimestamps = [];
function uniqueCreatedTimestamp(timestamp) {
if (existingTimestamps.indexOf(timestamp) < 0) {
existingTimestamps.push(timestamp);
return timestamp;
}
for (let i = 1; i <= 999; i++) {
let t = timestamp + i;
if (existingTimestamps.indexOf(t) < 0) {
existingTimestamps.push(t);
return t;
}
}
return timestamp;
}
2017-06-25 01:19:11 +02:00
function dateToTimestamp(s, zeroIfInvalid = false) {
2017-06-06 23:40:09 +02:00
let m = moment(s, 'YYYYMMDDTHHmmssZ');
if (!m.isValid()) {
2017-06-25 01:19:11 +02:00
if (zeroIfInvalid) return 0;
2017-06-06 23:40:09 +02:00
throw new Error('Invalid date: ' + s);
2017-06-06 23:27:03 +02:00
}
2017-06-24 19:40:03 +02:00
return m.toDate().getTime();
2017-06-06 23:40:09 +02:00
}
2017-06-09 00:24:40 +02:00
function extractRecognitionObjId(recognitionXml) {
const r = recognitionXml.match(/objID="(.*?)"/);
return r && r.length >= 2 ? r[1] : null;
}
2017-06-06 23:40:09 +02:00
2017-06-11 23:11:14 +02:00
function filePutContents(filePath, content) {
2017-06-25 01:19:11 +02:00
return fs.writeFile(filePath, content);
2017-06-11 23:11:14 +02:00
}
2017-06-24 19:40:03 +02:00
function removeUndefinedProperties(note) {
let output = {};
for (let n in note) {
if (!note.hasOwnProperty(n)) continue;
let v = note[n];
if (v === undefined || v === null) continue;
output[n] = v;
}
return output;
2017-06-11 23:11:14 +02:00
}
2017-06-09 00:24:40 +02:00
function createNoteId(note) {
let shaObj = new jsSHA("SHA-256", "TEXT");
shaObj.update(note.title + '_' + note.body + "_" + note.created_time + "_" + note.updated_time + "_");
let hash = shaObj.getHash("HEX");
return hash.substr(0, 32);
}
2017-06-06 23:40:09 +02:00
2017-06-24 19:40:03 +02:00
async function fuzzyMatch(note) {
let notes = await Note.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 0 AND created_time = ?', note.created_time);
if (!notes.length) return null;
if (notes.length === 1) return notes[0];
for (let i = 0; i < notes.length; i++) {
if (notes[i].title == note.title && note.title.trim() != '') return notes[i];
}
for (let i = 0; i < notes.length; i++) {
if (notes[i].body == note.body && note.body.trim() != '') return notes[i];
}
return null;
}
2017-06-24 20:51:43 +02:00
async function saveNoteToStorage(note) {
2017-06-24 19:40:03 +02:00
note = Note.filter(note);
let existingNote = await fuzzyMatch(note);
if (existingNote) {
let diff = BaseModel.diffObjects(existingNote, note);
delete diff.tags;
delete diff.resources;
delete diff.id;
// console.info('======================================');
// console.info(note);
// console.info(existingNote);
// console.info(diff);
// console.info('======================================');
if (!Object.getOwnPropertyNames(diff).length) return;
diff.id = existingNote.id;
diff.type_ = existingNote.type_;
return Note.save(diff, { autoTimestamp: false });
} else {
2017-06-24 20:51:43 +02:00
for (let i = 0; i < note.resources.length; i++) {
let resource = note.resources[i];
let toSave = Object.assign({}, resource);
delete toSave.data;
2017-06-25 01:19:11 +02:00
// The same resource sometimes appear twice in the same enex (exact same ID and file).
// In that case, just skip it - it means two different notes might be linked to the
// same resource.
let existingResource = await Resource.load(toSave.id);
if (existingResource) {
// console.warn('Trying to save: ' + JSON.stringify(toSave));
// console.warn('But duplicate: ' + JSON.stringify(existingResource));
continue;
}
2017-06-24 20:51:43 +02:00
await Resource.save(toSave, { isNew: true });
await filePutContents(Resource.fullPath(toSave), resource.data);
}
return Note.save(note, {
isNew: true,
autoTimestamp: false,
});
2017-06-24 19:40:03 +02:00
}
}
2017-06-24 20:51:43 +02:00
function importEnex(parentFolderId, filePath) {
2017-06-24 19:40:03 +02:00
let stream = fs.createReadStream(filePath);
2017-06-09 00:24:40 +02:00
return new Promise((resolve, reject) => {
let options = {};
let strict = true;
let saxStream = require('sax').createStream(strict, options);
let nodes = []; // LIFO list of nodes so that we know in which node we are in the onText event
let note = null;
let noteAttributes = null;
let noteResource = null;
let noteResourceAttributes = null;
let noteResourceRecognition = null;
let notes = [];
2017-06-24 19:40:03 +02:00
stream.on('error', (error) => {
reject(new Error(error.toString()));
});
2017-06-09 00:24:40 +02:00
function currentNodeName() {
if (!nodes.length) return null;
return nodes[nodes.length - 1].name;
}
2017-06-06 23:40:09 +02:00
2017-06-09 00:24:40 +02:00
function currentNodeAttributes() {
if (!nodes.length) return {};
return nodes[nodes.length - 1].attributes;
}
2017-06-06 23:40:09 +02:00
2017-06-09 00:24:40 +02:00
function processNotes() {
let chain = [];
while (notes.length) {
let note = notes.shift();
const contentStream = stringToStream(note.bodyXml);
chain.push(() => {
2017-06-25 01:19:11 +02:00
return enexXmlToMd(contentStream, note.resources).then((body) => {
2017-06-09 00:24:40 +02:00
delete note.bodyXml;
2017-06-11 23:11:14 +02:00
note.id = uuid.create();
2017-06-25 01:19:11 +02:00
note.parent_id = parentFolderId;
note.body = body;
2017-06-11 23:11:14 +02:00
2017-06-24 20:51:43 +02:00
return saveNoteToStorage(note);
2017-06-09 00:24:40 +02:00
});
});
}
2017-06-06 23:40:09 +02:00
2017-06-09 00:24:40 +02:00
return promiseChain(chain);
}
2017-06-07 21:21:04 +02:00
2017-06-09 00:24:40 +02:00
saxStream.on('error', function(e) {
2017-06-24 19:40:03 +02:00
reject(new Error(e.toString()));
2017-06-09 00:24:40 +02:00
})
2017-06-07 21:21:04 +02:00
2017-06-09 00:24:40 +02:00
saxStream.on('text', function(text) {
let n = currentNodeName();
if (noteAttributes) {
noteAttributes[n] = text;
} else if (noteResourceAttributes) {
noteResourceAttributes[n] = text;
} else if (noteResource) {
if (n == 'data') {
let attr = currentNodeAttributes();
noteResource.dataEncoding = attr.encoding;
}
noteResource[n] = text;
} else if (note) {
if (n == 'title') {
note.title = text;
} else if (n == 'created') {
2017-06-24 19:40:03 +02:00
note.created_time = uniqueCreatedTimestamp(dateToTimestamp(text));
2017-06-09 00:24:40 +02:00
} else if (n == 'updated') {
note.updated_time = dateToTimestamp(text);
} else if (n == 'tag') {
note.tags.push(text);
2017-06-25 01:19:11 +02:00
} else {
console.warn('Unsupported note tag: ' + n);
2017-06-09 00:24:40 +02:00
}
}
})
2017-06-06 23:40:09 +02:00
2017-06-09 00:24:40 +02:00
saxStream.on('opentag', function(node) {
let n = node.name.toLowerCase();
nodes.push(node);
if (n == 'note') {
note = {
resources: [],
tags: [],
};
} else if (n == 'resource-attributes') {
noteResourceAttributes = {};
} else if (n == 'recognition') {
if (noteResource) noteResourceRecognition = {};
} else if (n == 'note-attributes') {
noteAttributes = {};
} else if (n == 'resource') {
noteResource = {};
}
});
2017-06-06 23:40:09 +02:00
2017-06-09 00:24:40 +02:00
saxStream.on('cdata', function(data) {
let n = currentNodeName();
2017-06-06 23:40:09 +02:00
2017-06-09 00:24:40 +02:00
if (noteResourceRecognition) {
noteResourceRecognition.objID = extractRecognitionObjId(data);
} else if (note) {
if (n == 'content') {
note.bodyXml = data;
}
}
});
2017-06-06 23:27:03 +02:00
2017-06-09 00:24:40 +02:00
saxStream.on('closetag', function(n) {
nodes.pop();
if (n == 'note') {
2017-06-24 19:40:03 +02:00
note = removeUndefinedProperties(note);
2017-06-09 00:24:40 +02:00
notes.push(note);
if (notes.length >= 10) {
stream.pause();
processNotes().then(() => {
2017-06-11 23:11:14 +02:00
stream.resume();
2017-06-09 00:24:40 +02:00
}).catch((error) => {
2017-06-24 20:51:43 +02:00
console.error('Error processing note', error);
2017-06-09 00:24:40 +02:00
});
}
note = null;
} else if (n == 'recognition' && noteResource) {
noteResource.id = noteResourceRecognition.objID;
noteResourceRecognition = null;
} else if (n == 'resource-attributes') {
noteResource.filename = noteResourceAttributes['file-name'];
noteResourceAttributes = null;
} else if (n == 'note-attributes') {
note.latitude = noteAttributes.latitude;
note.longitude = noteAttributes.longitude;
note.altitude = noteAttributes.altitude;
note.author = noteAttributes.author;
2017-06-25 01:19:11 +02:00
note.is_todo = !!noteAttributes['reminder-order'];
note.todo_due = dateToTimestamp(noteAttributes['reminder-time'], true);
note.todo_completed = dateToTimestamp(noteAttributes['reminder-done-time'], true);
note.order = dateToTimestamp(noteAttributes['reminder-order'], true);
note.source = !!noteAttributes.source ? 'evernote.' + noteAttributes.source : 'evernote';
note.source_application = 'joplin.cli';
if (noteAttributes['reminder-time']) {
console.info('======================================================');
console.info(noteAttributes);
console.info('------------------------------------------------------');
console.info(note);
console.info('======================================================');
}
2017-06-09 00:24:40 +02:00
noteAttributes = null;
} else if (n == 'resource') {
let decodedData = null;
if (noteResource.dataEncoding == 'base64') {
decodedData = Buffer.from(noteResource.data, 'base64');
} else {
reject('Cannot decode resource with encoding: ' + noteResource.dataEncoding);
return;
}
2017-06-06 23:27:03 +02:00
2017-06-09 00:24:40 +02:00
let r = {
id: noteResource.id,
data: decodedData,
2017-06-24 20:51:43 +02:00
mime: noteResource.mime,
2017-06-09 00:24:40 +02:00
title: noteResource.filename,
filename: noteResource.filename,
};
2017-06-06 23:27:03 +02:00
2017-06-09 00:24:40 +02:00
note.resources.push(r);
noteResource = null;
}
});
2017-06-06 23:27:03 +02:00
2017-06-09 00:24:40 +02:00
saxStream.on('end', function() {
processNotes().then(() => { resolve(); });
});
2017-06-06 23:27:03 +02:00
2017-06-09 00:24:40 +02:00
stream.pipe(saxStream);
});
}
2017-06-06 23:27:03 +02:00
2017-06-24 19:40:03 +02:00
export { importEnex };