diff --git a/CliClient/app/app-gui.js b/CliClient/app/app-gui.js index 9fa16c99b..037cedddc 100644 --- a/CliClient/app/app-gui.js +++ b/CliClient/app/app-gui.js @@ -287,6 +287,8 @@ class AppGui { addCommandToConsole(cmd) { if (!cmd) return; + const isConfigPassword = cmd.indexOf('config ') >= 0 && cmd.indexOf('password') >= 0; + if (isConfigPassword) return; this.stdout(chalk.cyan.bold('> ' + cmd)); } diff --git a/CliClient/app/gui/StatusBarWidget.js b/CliClient/app/gui/StatusBarWidget.js index 8a3da2bf3..ae911da77 100644 --- a/CliClient/app/gui/StatusBarWidget.js +++ b/CliClient/app/gui/StatusBarWidget.js @@ -133,7 +133,8 @@ class StatusBarWidget extends BaseWidget { resolveResult = input ? input.trim() : input; // Add the command to history but only if it's longer than one character. // Below that it's usually an answer like "y"/"n", etc. - if (!isSecurePrompt && input && input.length > 1) this.history_.push(input); + const isConfigPassword = input.indexOf('config ') >= 0 && input.indexOf('password') >= 0; + if (!isSecurePrompt && input && input.length > 1 && !isConfigPassword) this.history_.push(input); } } diff --git a/ReactNativeClient/lib/fs-driver-node.js b/ReactNativeClient/lib/fs-driver-node.js index d8654b263..c6d66131b 100644 --- a/ReactNativeClient/lib/fs-driver-node.js +++ b/ReactNativeClient/lib/fs-driver-node.js @@ -140,14 +140,22 @@ class FsDriverNode extends FsDriverBase { } } - readFile(path, encoding = 'utf8') { - if (encoding === 'Buffer') return fs.readFile(path); // Returns the raw buffer - return fs.readFile(path, encoding); + async readFile(path, encoding = 'utf8') { + try { + if (encoding === 'Buffer') return await fs.readFile(path); // Returns the raw buffer + return await fs.readFile(path, encoding); + } catch (error) { + throw this.fsErrorToJsError_(error, path); + } } // Always overwrite destination async copy(source, dest) { - return fs.copy(source, dest, { overwrite: true }); + try { + return await fs.copy(source, dest, { overwrite: true }); + } catch (error) { + throw this.fsErrorToJsError_(error, source); + } } async unlink(path) { diff --git a/ReactNativeClient/lib/services/DecryptionWorker.js b/ReactNativeClient/lib/services/DecryptionWorker.js index 2e8d0f001..f7e261e9a 100644 --- a/ReactNativeClient/lib/services/DecryptionWorker.js +++ b/ReactNativeClient/lib/services/DecryptionWorker.js @@ -96,6 +96,11 @@ class DecryptionWorker { } continue; } + + if (error.code === 'masterKeyNotLoaded' && options.materKeyNotLoadedHandler === 'throw') { + throw error; + } + this.logger().warn('DecryptionWorker: error for: ' + item.id + ' (' + ItemClass.tableName() + ')', error); } } diff --git a/ReactNativeClient/lib/services/InteropService.js b/ReactNativeClient/lib/services/InteropService.js index fff4db832..daacd04cf 100644 --- a/ReactNativeClient/lib/services/InteropService.js +++ b/ReactNativeClient/lib/services/InteropService.js @@ -8,330 +8,35 @@ const Tag = require('lib/models/Tag.js'); const { basename, filename } = require('lib/path-utils.js'); const fs = require('fs-extra'); const md5 = require('md5'); +const ArrayUtils = require('lib/ArrayUtils'); const { sprintf } = require('sprintf-js'); const { shim } = require('lib/shim'); const { _ } = require('lib/locale'); const { fileExtension } = require('lib/path-utils'); const { uuid } = require('lib/uuid.js'); const { importEnex } = require('lib/import-enex'); - -async function temporaryDirectory(createIt) { - const tempDir = require('os').tmpdir() + '/' + md5(Math.random() + Date.now()); - if (createIt) await fs.mkdirp(tempDir); - return tempDir; -} - -class RawExporter { - - async init(destDir) { - this.destDir_ = destDir; - this.resourceDir_ = destDir ? destDir + '/resources' : null; - - await fs.mkdirp(this.destDir_); - await fs.mkdirp(this.resourceDir_); - } - - async processItem(ItemClass, item) { - const serialized = await ItemClass.serialize(item); - const filePath = this.destDir_ + '/' + ItemClass.systemPath(item); - await fs.writeFile(filePath, serialized); - } - - async processResource(resource, filePath) { - const destResourcePath = this.resourceDir_ + '/' + basename(filePath); - await fs.copyFile(filePath, destResourcePath, { overwrite: true }); - } - - async close() {} - -} - -class JexExporter { - - async init(destPath) { - if (await shim.fsDriver().isDirectory(destPath)) throw new Error('Path is a directory: ' + destPath); - - this.tempDir_ = await temporaryDirectory(false); - this.destPath_ = destPath; - this.rawExporter_ = new RawExporter(); - await this.rawExporter_.init(this.tempDir_); - } - - async processItem(ItemClass, item) { - return this.rawExporter_.processItem(ItemClass, item); - } - - async processResource(resource, filePath) { - return this.rawExporter_.processResource(resource, filePath); - } - - async close() { - const stats = await shim.fsDriver().readDirStats(this.tempDir_, { recursive: true }); - const filePaths = stats.map((a) => a.path); - - await require('tar').create({ - strict: true, - portable: true, - file: this.destPath_, - cwd: this.tempDir_, - }, filePaths); - - await fs.remove(this.tempDir_); - } - -} - -class RawImporter { - - async init(sourceDir, options) { - this.sourceDir_ = sourceDir; - this.options_ = options; - } - - async exec(result) { - const noteIdMap = {}; - const folderIdMap = {}; - const resourceIdMap = {}; - const tagIdMap = {}; - const createdResources = {}; - const noteTagsToCreate = []; - const destinationFolderId = this.options_.destinationFolderId; - - const replaceResourceNoteIds = (noteBody) => { - let output = noteBody; - const resourceIds = Note.linkedResourceIds(noteBody); - - for (let i = 0; i < resourceIds.length; i++) { - const id = resourceIds[i]; - if (!resourceIdMap[id]) resourceIdMap[id] = uuid.create(); - output = output.replace(new RegExp(id, 'gi'), resourceIdMap[id]); - } - - return output; - } - - const stats = await shim.fsDriver().readDirStats(this.sourceDir_); - for (let i = 0; i < stats.length; i++) { - const stat = stats[i]; - if (stat.isDirectory()) continue; - if (fileExtension(stat.path).toLowerCase() !== 'md') continue; - - const content = await shim.fsDriver().readFile(this.sourceDir_ + '/' + stat.path); - let item = await BaseItem.unserialize(content); - const itemType = item.type_; - const ItemClass = BaseItem.itemClass(item); - - delete item.type_; - - if (itemType === BaseModel.TYPE_NOTE) { - if (!folderIdMap[item.parent_id]) folderIdMap[item.parent_id] = destinationFolderId ? destinationFolderId : uuid.create(); - const noteId = uuid.create(); - noteIdMap[item.id] = noteId; - item.id = noteId; - item.parent_id = folderIdMap[item.parent_id]; - item.body = replaceResourceNoteIds(item.body); - } else if (itemType === BaseModel.TYPE_FOLDER) { - if (destinationFolderId) continue; - - if (!folderIdMap[item.id]) folderIdMap[item.id] = uuid.create(); - item.id = folderIdMap[item.id]; - item.title = await Folder.findUniqueFolderTitle(item.title); - } else if (itemType === BaseModel.TYPE_RESOURCE) { - if (!resourceIdMap[item.id]) resourceIdMap[item.id] = uuid.create(); - item.id = resourceIdMap[item.id]; - createdResources[item.id] = item; - } else if (itemType === BaseModel.TYPE_TAG) { - const tag = await Tag.loadByTitle(item.title); - if (tag) { - tagIdMap[item.id] = tag.id; - continue; - } - - const tagId = uuid.create(); - tagIdMap[item.id] = tagId; - item.id = tagId; - } else if (itemType === BaseModel.TYPE_NOTE_TAG) { - noteTagsToCreate.push(item); - continue; - } - - await ItemClass.save(item, { isNew: true, autoTimestamp: false }); - } - - for (let i = 0; i < noteTagsToCreate.length; i++) { - const noteTag = noteTagsToCreate[i]; - const newNoteId = noteIdMap[noteTag.note_id]; - const newTagId = tagIdMap[noteTag.tag_id]; - - if (!newNoteId) { - result.warnings.push(sprintf('Non-existent note %s referenced in tag %s', noteTag.note_id, noteTag.tag_id)); - continue; - } - - if (!newTagId) { - result.warnings.push(sprintf('Non-existent tag %s for note %s', noteTag.tag_id, noteTag.note_id)); - continue; - } - - noteTag.id = uuid.create(); - noteTag.note_id = newNoteId; - noteTag.tag_id = newTagId; - - await NoteTag.save(noteTag, { isNew: true }); - } - - const resourceStats = await shim.fsDriver().readDirStats(this.sourceDir_ + '/resources'); - - for (let i = 0; i < resourceStats.length; i++) { - const resourceFilePath = this.sourceDir_ + '/resources/' + resourceStats[i].path; - const oldId = Resource.pathToId(resourceFilePath); - const newId = resourceIdMap[oldId]; - if (!newId) { - result.warnings.push(sprintf('Resource file is not referenced in any note and so was not imported: %s', oldId)); - continue; - } - - const resource = createdResources[newId]; - const destPath = Resource.fullPath(resource); - await shim.fsDriver().copy(resourceFilePath, destPath); - } - - return result; - } - -} - -class JexImporter { - - async init(sourcePath, options) { - this.sourcePath_ = sourcePath; - this.options_ = options; - } - - async exec(result) { - const tempDir = await temporaryDirectory(true); - - try { - await require('tar').extract({ - strict: true, - portable: true, - file: this.sourcePath_, - cwd: tempDir, - }); - } catch (error) { - let msg = ['Cannot untar file ' + this.sourcePath_, error.message]; - if (error.data) msg.push(JSON.stringify(error.data)); - let e = new Error(msg.join(': ')); - throw e; - } - - const importer = newImporter('raw'); - await importer.init(tempDir, this.options_); - result = await importer.exec(result); - - await fs.remove(tempDir); - - return result; - } - -} - -class MdImporter { - - async init(sourcePath, options) { - this.sourcePath_ = sourcePath; - this.options_ = options; - } - - async exec(result) { - if (!this.options_.destinationFolder) throw new Error('Destination folder must be specified'); - - const parentFolderId = this.options_.destinationFolder.id; - - const filePaths = []; - if (await shim.fsDriver().isDirectory(this.sourcePath_)) { - const stats = await shim.fsDriver().readDirStats(this.sourcePath_); - for (let i = 0; i < stats.length; i++) { - const stat = stats[i]; - if (fileExtension(stat.path).toLowerCase() === 'md') { - filePaths.push(this.sourcePath_ + '/' + stat.path); - } - } - } else { - filePaths.push(this.sourcePath_); - } - - for (let i = 0; i < filePaths.length; i++) { - const path = filePaths[i]; - const stat = await shim.fsDriver().stat(path); - if (!stat) throw new Error('Cannot read ' + path); - const title = filename(path); - const body = await shim.fsDriver().readFile(path); - const note = { - parent_id: parentFolderId, - title: title, - body: body, - updated_time: stat.mtime.getTime(), - created_time: stat.birthtime.getTime(), - user_updated_time: stat.mtime.getTime(), - user_created_time: stat.birthtime.getTime(), - }; - await Note.save(note, { autoTimestamp: false }); - } - - return result; - } - -} - -class EnexImporter { - - async init(sourcePath, options) { - this.sourcePath_ = sourcePath; - this.options_ = options; - } - - async exec(result) { - let folder = this.options_.destinationFolder; - - if (!folder) { - const folderTitle = await Folder.findUniqueFolderTitle(filename(this.sourcePath_)); - folder = await Folder.save({ title: folderTitle }); - } - - await importEnex(folder.id, this.sourcePath_, this.options_); - - return result; - } - -} - -function newExporter(format) { - if (format === 'raw') { - return new RawExporter(); - } else if (format === 'jex') { - return new JexExporter(); - } else { - throw new Error('Unknown format: ' + format); - } -} - -function newImporter(format) { - if (format === 'raw') { - return new RawImporter(); - } else if (format === 'jex') { - return new JexImporter(); - } else if (format === 'md') { - return new MdImporter(); - } else if (format === 'enex') { - return new EnexImporter(); - } else { - throw new Error('Unknown format: ' + format); - } -} +const { toTitleCase } = require('lib/string-utils'); class InteropService { + newImportExportModule_(format, className) { + try { + const FormatClass = require('lib/services/' + className); + return new FormatClass(); + } catch (error) { + error.message = _('Cannot load module for format "%s": %s', format, error.message); + throw error; + } + } + + newExporter_(format) { + return this.newImportExportModule_(format, 'InteropService_Exporter_' + toTitleCase(format)); + } + + newImporter_(format) { + return this.newImportExportModule_(format, 'InteropService_Importer_' + toTitleCase(format)); + } + async import(options) { if (!await shim.fsDriver().exists(options.path)) throw new Error(_('Cannot find "%s".', options.path)); @@ -360,7 +65,7 @@ class InteropService { let result = { warnings: [] } - const importer = newImporter(options.format); + const importer = this.newImporter_(options.format); await importer.init(options.path, options); result = await importer.exec(result); @@ -406,6 +111,8 @@ class InteropService { } } + resourceIds = ArrayUtils.unique(resourceIds); + for (let i = 0; i < resourceIds.length; i++) { await queueExportItem(BaseModel.TYPE_RESOURCE, resourceIds[i]); } @@ -425,7 +132,7 @@ class InteropService { await queueExportItem(BaseModel.TYPE_TAG, exportedTagIds[i]); } - const exporter = newExporter(exportFormat); + const exporter = this.newExporter_(exportFormat); await exporter.init(exportPath); for (let i = 0; i < itemsToExport.length; i++) { @@ -443,11 +150,15 @@ class InteropService { continue; } - await exporter.processItem(ItemClass, item); + try { + if (itemType == BaseModel.TYPE_RESOURCE) { + const resourcePath = Resource.fullPath(item); + await exporter.processResource(item, resourcePath); + } - if (itemType == BaseModel.TYPE_RESOURCE) { - const resourcePath = Resource.fullPath(item); - await exporter.processResource(item, resourcePath); + await exporter.processItem(ItemClass, item); + } catch (error) { + result.warnings.push(error.message); } } diff --git a/ReactNativeClient/lib/services/InteropService_Exporter_Base.js b/ReactNativeClient/lib/services/InteropService_Exporter_Base.js new file mode 100644 index 000000000..6d0ba2bb0 --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Exporter_Base.js @@ -0,0 +1,17 @@ +class InteropService_Exporter_Base { + + async init(destDir) {} + async processItem(ItemClass, item) {} + async processResource(resource, filePath) {} + async close() {} + + async temporaryDirectory_(createIt) { + const md5 = require('md5'); + const tempDir = require('os').tmpdir() + '/' + md5(Math.random() + Date.now()); + if (createIt) await require('fs-extra').mkdirp(tempDir); + return tempDir; + } + +} + +module.exports = InteropService_Exporter_Base; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Exporter_Jex.js b/ReactNativeClient/lib/services/InteropService_Exporter_Jex.js new file mode 100644 index 000000000..07aeaf59a --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Exporter_Jex.js @@ -0,0 +1,64 @@ +const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base'); +const InteropService_Exporter_Raw = require('lib/services/InteropService_Exporter_Raw'); +const BaseItem = require('lib/models/BaseItem.js'); +const BaseModel = require('lib/BaseModel.js'); +const Resource = require('lib/models/Resource.js'); +const Folder = require('lib/models/Folder.js'); +const NoteTag = require('lib/models/NoteTag.js'); +const Note = require('lib/models/Note.js'); +const Tag = require('lib/models/Tag.js'); +const { basename, filename } = require('lib/path-utils.js'); +const fs = require('fs-extra'); +const md5 = require('md5'); +const { sprintf } = require('sprintf-js'); +const { shim } = require('lib/shim'); +const { _ } = require('lib/locale'); +const { fileExtension } = require('lib/path-utils'); +const { uuid } = require('lib/uuid.js'); +const { importEnex } = require('lib/import-enex'); + +class InteropService_Exporter_Jex extends InteropService_Exporter_Base { + + async init(destPath) { + if (await shim.fsDriver().isDirectory(destPath)) throw new Error('Path is a directory: ' + destPath); + + this.tempDir_ = await this.temporaryDirectory_(false); + this.destPath_ = destPath; + this.rawExporter_ = new InteropService_Exporter_Raw(); + await this.rawExporter_.init(this.tempDir_); + } + + async processItem(ItemClass, item) { + return this.rawExporter_.processItem(ItemClass, item); + } + + async processResource(resource, filePath) { + return this.rawExporter_.processResource(resource, filePath); + } + + async close() { + const stats = await shim.fsDriver().readDirStats(this.tempDir_, { recursive: true }); + const filePaths = stats.filter((a) => !a.isDirectory()).map((a) => a.path); + + if (!filePaths.length) throw new Error(_('There is no data to export.')); + + await require('tar').create({ + strict: true, + portable: true, + file: this.destPath_, + cwd: this.tempDir_, + }, filePaths); + + await fs.remove(this.tempDir_); + } + +} + +InteropService_Exporter_Jex.metadata = function() { + return { + format: 'jex', + fileExtension: 'jex', + }; +} + +module.exports = InteropService_Exporter_Jex; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Exporter_Raw.js b/ReactNativeClient/lib/services/InteropService_Exporter_Raw.js new file mode 100644 index 000000000..b6277ac39 --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Exporter_Raw.js @@ -0,0 +1,36 @@ +const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base'); +const { basename, filename } = require('lib/path-utils.js'); +const { shim } = require('lib/shim'); + +class InteropService_Exporter_Raw extends InteropService_Exporter_Base { + + async init(destDir) { + this.destDir_ = destDir; + this.resourceDir_ = destDir ? destDir + '/resources' : null; + + await shim.fsDriver().mkdir(this.destDir_); + await shim.fsDriver().mkdir(this.resourceDir_); + } + + async processItem(ItemClass, item) { + const serialized = await ItemClass.serialize(item); + const filePath = this.destDir_ + '/' + ItemClass.systemPath(item); + await shim.fsDriver().writeFile(filePath, serialized, 'utf-8'); + } + + async processResource(resource, filePath) { + const destResourcePath = this.resourceDir_ + '/' + basename(filePath); + await shim.fsDriver().copy(filePath, destResourcePath); + } + + async close() {} + +} + +InteropService_Exporter_Raw.metadata = function() { + return { + format: 'raw', + }; +} + +module.exports = InteropService_Exporter_Raw; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Importer_Base.js b/ReactNativeClient/lib/services/InteropService_Importer_Base.js new file mode 100644 index 000000000..6076cd6be --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Importer_Base.js @@ -0,0 +1,19 @@ +class InteropService_Importer_Base { + + async init(sourcePath, options) { + this.sourcePath_ = sourcePath; + this.options_ = options; + } + + async exec(result) {} + + async temporaryDirectory_(createIt) { + const md5 = require('md5'); + const tempDir = require('os').tmpdir() + '/' + md5(Math.random() + Date.now()); + if (createIt) await require('fs-extra').mkdirp(tempDir); + return tempDir; + } + +} + +module.exports = InteropService_Importer_Base; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Importer_Enex.js b/ReactNativeClient/lib/services/InteropService_Importer_Enex.js new file mode 100644 index 000000000..a958b3930 --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Importer_Enex.js @@ -0,0 +1,36 @@ +const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base'); +const BaseItem = require('lib/models/BaseItem.js'); +const BaseModel = require('lib/BaseModel.js'); +const Resource = require('lib/models/Resource.js'); +const Folder = require('lib/models/Folder.js'); +const NoteTag = require('lib/models/NoteTag.js'); +const Note = require('lib/models/Note.js'); +const Tag = require('lib/models/Tag.js'); +const { basename, filename } = require('lib/path-utils.js'); +const fs = require('fs-extra'); +const md5 = require('md5'); +const { sprintf } = require('sprintf-js'); +const { shim } = require('lib/shim'); +const { _ } = require('lib/locale'); +const { fileExtension } = require('lib/path-utils'); +const { uuid } = require('lib/uuid.js'); +const { importEnex } = require('lib/import-enex'); + +class InteropService_Importer_Enex extends InteropService_Importer_Base { + + async exec(result) { + let folder = this.options_.destinationFolder; + + if (!folder) { + const folderTitle = await Folder.findUniqueFolderTitle(filename(this.sourcePath_)); + folder = await Folder.save({ title: folderTitle }); + } + + await importEnex(folder.id, this.sourcePath_, this.options_); + + return result; + } + +} + +module.exports = InteropService_Importer_Enex; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Importer_Jex.js b/ReactNativeClient/lib/services/InteropService_Importer_Jex.js new file mode 100644 index 000000000..52ad938c4 --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Importer_Jex.js @@ -0,0 +1,57 @@ +const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base'); +const InteropService_Importer_Raw = require('lib/services/InteropService_Importer_Raw'); +const BaseItem = require('lib/models/BaseItem.js'); +const BaseModel = require('lib/BaseModel.js'); +const Resource = require('lib/models/Resource.js'); +const Folder = require('lib/models/Folder.js'); +const NoteTag = require('lib/models/NoteTag.js'); +const Note = require('lib/models/Note.js'); +const Tag = require('lib/models/Tag.js'); +const { basename, filename } = require('lib/path-utils.js'); +const fs = require('fs-extra'); +const md5 = require('md5'); +const { sprintf } = require('sprintf-js'); +const { shim } = require('lib/shim'); +const { _ } = require('lib/locale'); +const { fileExtension } = require('lib/path-utils'); +const { uuid } = require('lib/uuid.js'); +const { importEnex } = require('lib/import-enex'); + +class InteropService_Importer_Jex extends InteropService_Importer_Base { + + async exec(result) { + const tempDir = await this.temporaryDirectory_(true); + + try { + await require('tar').extract({ + strict: true, + portable: true, + file: this.sourcePath_, + cwd: tempDir, + }); + } catch (error) { + let msg = ['Cannot untar file ' + this.sourcePath_, error.message]; + if (error.data) msg.push(JSON.stringify(error.data)); + let e = new Error(msg.join(': ')); + throw e; + } + + const importer = new InteropService_Importer_Raw(); + await importer.init(tempDir, this.options_); + result = await importer.exec(result); + + await fs.remove(tempDir); + + return result; + } + +} + +InteropService_Importer_Jex.metadata = function() { + return { + format: 'jex', + fileExtension: 'jex', + }; +} + +module.exports = InteropService_Importer_Jex; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Importer_Md.js b/ReactNativeClient/lib/services/InteropService_Importer_Md.js new file mode 100644 index 000000000..e842619ca --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Importer_Md.js @@ -0,0 +1,69 @@ +const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base'); +const BaseItem = require('lib/models/BaseItem.js'); +const BaseModel = require('lib/BaseModel.js'); +const Resource = require('lib/models/Resource.js'); +const Folder = require('lib/models/Folder.js'); +const NoteTag = require('lib/models/NoteTag.js'); +const Note = require('lib/models/Note.js'); +const Tag = require('lib/models/Tag.js'); +const { basename, filename } = require('lib/path-utils.js'); +const fs = require('fs-extra'); +const md5 = require('md5'); +const { sprintf } = require('sprintf-js'); +const { shim } = require('lib/shim'); +const { _ } = require('lib/locale'); +const { fileExtension } = require('lib/path-utils'); +const { uuid } = require('lib/uuid.js'); +const { importEnex } = require('lib/import-enex'); + +class InteropService_Importer_Md extends InteropService_Importer_Base { + + async exec(result) { + if (!this.options_.destinationFolder) throw new Error(_('Please specify the notebook where the notes should be imported to.')); + + const parentFolderId = this.options_.destinationFolder.id; + + const filePaths = []; + if (await shim.fsDriver().isDirectory(this.sourcePath_)) { + const stats = await shim.fsDriver().readDirStats(this.sourcePath_); + for (let i = 0; i < stats.length; i++) { + const stat = stats[i]; + if (fileExtension(stat.path).toLowerCase() === 'md') { + filePaths.push(this.sourcePath_ + '/' + stat.path); + } + } + } else { + filePaths.push(this.sourcePath_); + } + + for (let i = 0; i < filePaths.length; i++) { + const path = filePaths[i]; + const stat = await shim.fsDriver().stat(path); + if (!stat) throw new Error('Cannot read ' + path); + const title = filename(path); + const body = await shim.fsDriver().readFile(path); + const note = { + parent_id: parentFolderId, + title: title, + body: body, + updated_time: stat.mtime.getTime(), + created_time: stat.birthtime.getTime(), + user_updated_time: stat.mtime.getTime(), + user_created_time: stat.birthtime.getTime(), + }; + await Note.save(note, { autoTimestamp: false }); + } + + return result; + } + +} + +InteropService_Importer_Md.metadata = function() { + return { + format: 'md', + fileExtension: 'md', + }; +} + +module.exports = InteropService_Importer_Md; \ No newline at end of file diff --git a/ReactNativeClient/lib/services/InteropService_Importer_Raw.js b/ReactNativeClient/lib/services/InteropService_Importer_Raw.js new file mode 100644 index 000000000..6894c8835 --- /dev/null +++ b/ReactNativeClient/lib/services/InteropService_Importer_Raw.js @@ -0,0 +1,139 @@ +const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base'); +const BaseItem = require('lib/models/BaseItem.js'); +const BaseModel = require('lib/BaseModel.js'); +const Resource = require('lib/models/Resource.js'); +const Folder = require('lib/models/Folder.js'); +const NoteTag = require('lib/models/NoteTag.js'); +const Note = require('lib/models/Note.js'); +const Tag = require('lib/models/Tag.js'); +const { basename, filename } = require('lib/path-utils.js'); +const fs = require('fs-extra'); +const md5 = require('md5'); +const { sprintf } = require('sprintf-js'); +const { shim } = require('lib/shim'); +const { _ } = require('lib/locale'); +const { fileExtension } = require('lib/path-utils'); +const { uuid } = require('lib/uuid.js'); +const { importEnex } = require('lib/import-enex'); + +class InteropService_Importer_Raw extends InteropService_Importer_Base { + + async exec(result) { + const noteIdMap = {}; + const folderIdMap = {}; + const resourceIdMap = {}; + const tagIdMap = {}; + const createdResources = {}; + const noteTagsToCreate = []; + const destinationFolderId = this.options_.destinationFolderId; + + const replaceResourceNoteIds = (noteBody) => { + let output = noteBody; + const resourceIds = Note.linkedResourceIds(noteBody); + + for (let i = 0; i < resourceIds.length; i++) { + const id = resourceIds[i]; + if (!resourceIdMap[id]) resourceIdMap[id] = uuid.create(); + output = output.replace(new RegExp(id, 'gi'), resourceIdMap[id]); + } + + return output; + } + + const stats = await shim.fsDriver().readDirStats(this.sourcePath_); + for (let i = 0; i < stats.length; i++) { + const stat = stats[i]; + if (stat.isDirectory()) continue; + if (fileExtension(stat.path).toLowerCase() !== 'md') continue; + + const content = await shim.fsDriver().readFile(this.sourcePath_ + '/' + stat.path); + let item = await BaseItem.unserialize(content); + const itemType = item.type_; + const ItemClass = BaseItem.itemClass(item); + + delete item.type_; + + if (itemType === BaseModel.TYPE_NOTE) { + if (!folderIdMap[item.parent_id]) folderIdMap[item.parent_id] = destinationFolderId ? destinationFolderId : uuid.create(); + const noteId = uuid.create(); + noteIdMap[item.id] = noteId; + item.id = noteId; + item.parent_id = folderIdMap[item.parent_id]; + item.body = replaceResourceNoteIds(item.body); + } else if (itemType === BaseModel.TYPE_FOLDER) { + if (destinationFolderId) continue; + + if (!folderIdMap[item.id]) folderIdMap[item.id] = uuid.create(); + item.id = folderIdMap[item.id]; + item.title = await Folder.findUniqueFolderTitle(item.title); + } else if (itemType === BaseModel.TYPE_RESOURCE) { + if (!resourceIdMap[item.id]) resourceIdMap[item.id] = uuid.create(); + item.id = resourceIdMap[item.id]; + createdResources[item.id] = item; + } else if (itemType === BaseModel.TYPE_TAG) { + const tag = await Tag.loadByTitle(item.title); + if (tag) { + tagIdMap[item.id] = tag.id; + continue; + } + + const tagId = uuid.create(); + tagIdMap[item.id] = tagId; + item.id = tagId; + } else if (itemType === BaseModel.TYPE_NOTE_TAG) { + noteTagsToCreate.push(item); + continue; + } + + await ItemClass.save(item, { isNew: true, autoTimestamp: false }); + } + + for (let i = 0; i < noteTagsToCreate.length; i++) { + const noteTag = noteTagsToCreate[i]; + const newNoteId = noteIdMap[noteTag.note_id]; + const newTagId = tagIdMap[noteTag.tag_id]; + + if (!newNoteId) { + result.warnings.push(sprintf('Non-existent note %s referenced in tag %s', noteTag.note_id, noteTag.tag_id)); + continue; + } + + if (!newTagId) { + result.warnings.push(sprintf('Non-existent tag %s for note %s', noteTag.tag_id, noteTag.note_id)); + continue; + } + + noteTag.id = uuid.create(); + noteTag.note_id = newNoteId; + noteTag.tag_id = newTagId; + + await NoteTag.save(noteTag, { isNew: true }); + } + + const resourceStats = await shim.fsDriver().readDirStats(this.sourcePath_ + '/resources'); + + for (let i = 0; i < resourceStats.length; i++) { + const resourceFilePath = this.sourcePath_ + '/resources/' + resourceStats[i].path; + const oldId = Resource.pathToId(resourceFilePath); + const newId = resourceIdMap[oldId]; + if (!newId) { + result.warnings.push(sprintf('Resource file is not referenced in any note and so was not imported: %s', oldId)); + continue; + } + + const resource = createdResources[newId]; + const destPath = Resource.fullPath(resource); + await shim.fsDriver().copy(resourceFilePath, destPath); + } + + return result; + } + +} +InteropService_Importer_Raw.metadata = function() { + return { + format: 'raw', + }; +} + +module.exports = InteropService_Importer_Raw; \ No newline at end of file