From 2c713cdf7a69d3c1c0115d0e9e06b6da8ad01a5a Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 27 Jun 2017 20:53:40 +0000 Subject: [PATCH] Handle resource in enex files --- CliClient/app/import-enex.js | 42 ++++++++++++++++++++---------------- CliClient/app/main.js | 10 ++++++++- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/CliClient/app/import-enex.js b/CliClient/app/import-enex.js index 05774961c6..bf9089ebe8 100644 --- a/CliClient/app/import-enex.js +++ b/CliClient/app/import-enex.js @@ -65,6 +65,26 @@ async function fuzzyMatch(note) { return null; } +async function saveNoteResources(note) { + let resourcesCreated = 0; + for (let i = 0; i < note.resources.length; i++) { + let resource = note.resources[i]; + let toSave = Object.assign({}, resource); + delete toSave.data; + + // 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) continue; + + await Resource.save(toSave, { isNew: true }); + await filePutContents(Resource.fullPath(toSave), resource.data) + resourcesCreated++; + } + return resourcesCreated; +} + async function saveNoteToStorage(note, fuzzyMatching = false) { note = Note.filter(note); @@ -77,17 +97,17 @@ async function saveNoteToStorage(note, fuzzyMatching = false) { resourcesCreated: 0, }; + let resourcesCreated = await saveNoteResources(note); + result.resourcesCreated += resourcesCreated; + if (existingNote) { let diff = BaseModel.diffObjects(existingNote, note); delete diff.tags; delete diff.resources; delete diff.id; - // TODO: also save resources - if (!Object.getOwnPropertyNames(diff).length) { result.noteSkipped = true; - // TODO: also save resources return result; } @@ -96,22 +116,6 @@ async function saveNoteToStorage(note, fuzzyMatching = false) { await Note.save(diff, { autoTimestamp: false }) result.noteUpdated = true; } else { - for (let i = 0; i < note.resources.length; i++) { - let resource = note.resources[i]; - let toSave = Object.assign({}, resource); - delete toSave.data; - - // 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) continue; - - await Resource.save(toSave, { isNew: true }); - await filePutContents(Resource.fullPath(toSave), resource.data) - result.resourcesCreated++; - } - await Note.save(note, { isNew: true, autoTimestamp: false, diff --git a/CliClient/app/main.js b/CliClient/app/main.js index 09fed2742f..f8436cc1d6 100644 --- a/CliClient/app/main.js +++ b/CliClient/app/main.js @@ -365,7 +365,15 @@ commands.push({ if (folderTitle) { folder = await Folder.loadByField('title', folderTitle); - if (!folder) return cmdError(this, _('Folder does not exists: "%s"', folderTitle), end); + if (!folder) { + let ok = await cmdPromptConfirm(this, _('Folder does not exists: "%s". Create it?', folderTitle)) + if (!ok) { + end(); + return; + } + + folder = await Folder.save({ title: folderTitle }); + } } else { folderTitle = filename(filePath); folderTitle = _('Imported - %s', folderTitle);