From cf40c14a86f61e798c98f592dc72a53996e954d1 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 12 Apr 2019 01:43:15 +0100 Subject: [PATCH] All: Fixes #1405: Handle invalid resource tags that contain no data when importing ENEX --- ElectronClient/app/app.js | 3 +++ ReactNativeClient/lib/import-enex.js | 28 +++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/ElectronClient/app/app.js b/ElectronClient/app/app.js index a977fcf66..089d7fb6e 100644 --- a/ElectronClient/app/app.js +++ b/ElectronClient/app/app.js @@ -362,6 +362,9 @@ class Application extends BaseApplication { importOptions.path = path; importOptions.format = module.format; importOptions.destinationFolderId = !module.isNoteArchive && moduleSource === 'file' ? selectedFolderId : null; + importOptions.onError = (error) => { + console.warn(error); + } const service = new InteropService(); try { diff --git a/ReactNativeClient/lib/import-enex.js b/ReactNativeClient/lib/import-enex.js index 74d5d47ee..c8d89e156 100644 --- a/ReactNativeClient/lib/import-enex.js +++ b/ReactNativeClient/lib/import-enex.js @@ -95,6 +95,8 @@ async function saveNoteResources(note) { let resourcesCreated = 0; for (let i = 0; i < note.resources.length; i++) { let resource = note.resources[i]; + if (!resource.id) continue; + let toSave = Object.assign({}, resource); delete toSave.data; @@ -358,6 +360,7 @@ function importEnex(parentFolderId, filePath, importOptions = null) { noteResourceRecognition = null; } else if (n == 'resource-attributes') { noteResource.filename = noteResourceAttributes['file-name']; + if (noteResourceAttributes['source-url']) noteResource.sourceUrl = noteResourceAttributes['source-url']; noteResourceAttributes = null; } else if (n == 'note-attributes') { note.latitude = noteAttributes.latitude; @@ -389,7 +392,7 @@ function importEnex(parentFolderId, filePath, importOptions = null) { } catch (error) { importOptions.onError(error); } - } else { + } else if (noteResource.dataEncoding) { importOptions.onError(new Error('Cannot decode resource with encoding: ' + noteResource.dataEncoding)); decodedData = noteResource.data; // Just put the encoded data directly in the file so it can, potentially, be manually decoded later } @@ -400,15 +403,22 @@ function importEnex(parentFolderId, filePath, importOptions = null) { resourceId = md5(decodedData); } - let r = { - id: resourceId, - data: decodedData, - mime: noteResource.mime, - title: noteResource.filename ? noteResource.filename : '', - filename: noteResource.filename ? noteResource.filename : '', - }; + if (!resourceId || !noteResource.data) { + const debugTemp = Object.assign({}, noteResource); + debugTemp.data = debugTemp.data ? debugTemp.data.substr(0, 32) + '...' : debugTemp.data; + importOptions.onError(new Error('This resource was not added because it has no ID or no content: ' + JSON.stringify(debugTemp))); + } else { + let r = { + id: resourceId, + data: decodedData, + mime: noteResource.mime, + title: noteResource.filename ? noteResource.filename : '', + filename: noteResource.filename ? noteResource.filename : '', + }; + + note.resources.push(r); + } - note.resources.push(r); noteResource = null; } });