1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Fixed issue with enex import and added progress info

This commit is contained in:
Laurent Cozic 2017-06-26 19:03:01 +00:00
parent 5c5b0b2696
commit 09c1d58358
4 changed files with 63 additions and 20 deletions

View File

@ -132,10 +132,17 @@ async function saveNoteToStorage(note, fuzzyMatching = false) {
function importEnex(parentFolderId, filePath, importOptions = null) { function importEnex(parentFolderId, filePath, importOptions = null) {
if (!importOptions) importOptions = {}; if (!importOptions) importOptions = {};
if (!('fuzzyMatching' in importOptions)) importOptions.fuzzyMatching = false; if (!('fuzzyMatching' in importOptions)) importOptions.fuzzyMatching = false;
if (!('onProgress' in importOptions)) importOptions.onProgress = function(state) {};
let stream = fs.createReadStream(filePath); if (!('onError' in importOptions)) importOptions.onError = function(error) {};
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let progressState = {
loaded: 0,
imported: 0,
};
let stream = fs.createReadStream(filePath);
let options = {}; let options = {};
let strict = true; let strict = true;
let saxStream = require('sax').createStream(strict, options); let saxStream = require('sax').createStream(strict, options);
@ -147,9 +154,10 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
let noteResourceAttributes = null; let noteResourceAttributes = null;
let noteResourceRecognition = null; let noteResourceRecognition = null;
let notes = []; let notes = [];
let processingNotes = false;
stream.on('error', (error) => { stream.on('error', (error) => {
reject(new Error(error.toString())); importOptions.onError(error);
}); });
function currentNodeName() { function currentNodeName() {
@ -162,7 +170,11 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
return nodes[nodes.length - 1].attributes; return nodes[nodes.length - 1].attributes;
} }
function processNotes() { async function processNotes() {
if (processingNotes) return;
processingNotes = true;
stream.pause();
let chain = []; let chain = [];
while (notes.length) { while (notes.length) {
let note = notes.shift(); let note = notes.shift();
@ -176,16 +188,22 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
note.body = body; note.body = body;
return saveNoteToStorage(note, importOptions.fuzzyMatching); return saveNoteToStorage(note, importOptions.fuzzyMatching);
}).then(() => {
progressState.imported++;
importOptions.onProgress(progressState);
}); });
}); });
} }
return promiseChain(chain); return promiseChain(chain).then(() => {
stream.resume();
processingNotes = false;
});
} }
saxStream.on('error', function(e) { saxStream.on('error', (error) => {
reject(new Error(e.toString())); importOptions.onError(error);
}) });
saxStream.on('text', function(text) { saxStream.on('text', function(text) {
let n = currentNodeName(); let n = currentNodeName();
@ -253,13 +271,14 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
if (n == 'note') { if (n == 'note') {
note = removeUndefinedProperties(note); note = removeUndefinedProperties(note);
progressState.loaded++;
importOptions.onProgress(progressState);
notes.push(note); notes.push(note);
if (notes.length >= 10) { if (notes.length >= 10) {
stream.pause(); processNotes().catch((error) => {
processNotes().then(() => { importOptions.onError(error);
stream.resume();
}).catch((error) => {
console.error('Error processing note', error);
}); });
} }
note = null; note = null;
@ -293,10 +312,14 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
} else if (n == 'resource') { } else if (n == 'resource') {
let decodedData = null; let decodedData = null;
if (noteResource.dataEncoding == 'base64') { if (noteResource.dataEncoding == 'base64') {
decodedData = Buffer.from(noteResource.data, 'base64'); try {
decodedData = Buffer.from(noteResource.data, 'base64');
} catch (error) {
importOptions.onError(error);
}
} else { } else {
reject('Cannot decode resource with encoding: ' + noteResource.dataEncoding); importOptions.onError(new Error('Cannot decode resource with encoding: ' + noteResource.dataEncoding));
return; decodedData = noteResource.data; // Just put the encoded data directly in the file so it can, potentially, be manually decoded later
} }
let r = { let r = {
@ -313,7 +336,15 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
}); });
saxStream.on('end', function() { saxStream.on('end', function() {
processNotes().then(() => { resolve(); }); // Wait till there is no more notes to process.
let iid = setInterval(() => {
if (notes.length) {
processNotes();
} else {
clearInterval(iid);
resolve();
}
}, 500);
}); });
stream.pipe(saxStream); stream.pipe(saxStream);

View File

@ -344,14 +344,25 @@ commands.push({
return; return;
} }
let redrawnCalled = false;
let options = { let options = {
fuzzyMatching: args.options['fuzzy-matching'] === true, fuzzyMatching: args.options['fuzzy-matching'] === true,
onProgress: (progressState) => {
let line = sprintf(_('Found: %d. Imported: %d.', progressState.loaded, progressState.imported));
redrawnCalled = true;
vorpal.ui.redraw(line);
},
onError: (error) => {
let s = error.trace ? error.trace : error.toString();
this.log(s);
},
} }
folder = !folder ? await Folder.save({ title: folderTitle }) : folder; folder = !folder ? await Folder.save({ title: folderTitle }) : folder;
this.log(_('Importing notes...')); this.log(_('Importing notes...'));
await importEnex(folder.id, filePath, options); await importEnex(folder.id, filePath, options);
this.log(_('The notes have been imported into "%s"', folderTitle)); if (redrawnCalled) vorpal.ui.redraw.done();
this.log(_('Done.'));
end(); end();
}, },

View File

@ -3,4 +3,5 @@ CLIENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
rm -f "$CLIENT_DIR/app/lib" rm -f "$CLIENT_DIR/app/lib"
ln -s "$CLIENT_DIR/../lib" "$CLIENT_DIR/app" ln -s "$CLIENT_DIR/../lib" "$CLIENT_DIR/app"
npm run build npm run build
cp "$CLIENT_DIR/package.json" "$CLIENT_DIR/build"

View File

@ -2,4 +2,4 @@
set -e set -e
CLIENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" CLIENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
bash $CLIENT_DIR/build.sh bash $CLIENT_DIR/build.sh
NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestJoplin NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes