2018-02-23 21:32:19 +02:00
|
|
|
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');
|
2018-02-26 20:43:50 +02:00
|
|
|
const { basename, filename } = require('lib/path-utils.js');
|
2018-02-23 21:32:19 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const md5 = require('md5');
|
2018-02-26 21:25:54 +02:00
|
|
|
const ArrayUtils = require('lib/ArrayUtils');
|
2018-02-23 21:32:19 +02:00
|
|
|
const { sprintf } = require('sprintf-js');
|
2018-02-25 23:08:32 +02:00
|
|
|
const { shim } = require('lib/shim');
|
2018-02-26 21:16:01 +02:00
|
|
|
const { _ } = require('lib/locale');
|
2018-02-25 19:01:16 +02:00
|
|
|
const { fileExtension } = require('lib/path-utils');
|
|
|
|
const { uuid } = require('lib/uuid.js');
|
2018-02-26 21:16:01 +02:00
|
|
|
const { importEnex } = require('lib/import-enex');
|
2018-02-26 21:25:54 +02:00
|
|
|
const { toTitleCase } = require('lib/string-utils');
|
2018-02-25 19:01:16 +02:00
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
class InteropService {
|
2018-02-25 19:01:16 +02:00
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
newImportExportModule_(format, className) {
|
2018-02-26 21:16:01 +02:00
|
|
|
try {
|
2018-02-26 21:25:54 +02:00
|
|
|
const FormatClass = require('lib/services/' + className);
|
|
|
|
return new FormatClass();
|
2018-02-26 21:16:01 +02:00
|
|
|
} catch (error) {
|
2018-02-26 21:25:54 +02:00
|
|
|
error.message = _('Cannot load module for format "%s": %s', format, error.message);
|
|
|
|
throw error;
|
2018-02-26 21:16:01 +02:00
|
|
|
}
|
2018-02-25 19:01:16 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
newExporter_(format) {
|
|
|
|
return this.newImportExportModule_(format, 'InteropService_Exporter_' + toTitleCase(format));
|
2018-02-26 20:43:50 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
newImporter_(format) {
|
|
|
|
return this.newImportExportModule_(format, 'InteropService_Importer_' + toTitleCase(format));
|
2018-02-26 21:16:01 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 21:32:19 +02:00
|
|
|
async import(options) {
|
2018-02-26 21:16:01 +02:00
|
|
|
if (!await shim.fsDriver().exists(options.path)) throw new Error(_('Cannot find "%s".', options.path));
|
|
|
|
|
2018-02-25 19:01:16 +02:00
|
|
|
options = Object.assign({}, {
|
|
|
|
format: 'auto',
|
|
|
|
destinationFolderId: null,
|
2018-02-26 20:43:50 +02:00
|
|
|
destinationFolder: null,
|
2018-02-25 19:01:16 +02:00
|
|
|
}, options);
|
|
|
|
|
|
|
|
if (options.format === 'auto') {
|
2018-02-26 21:16:01 +02:00
|
|
|
const ext = fileExtension(options.path).toLowerCase();
|
|
|
|
if (ext === 'jex') {
|
2018-02-25 19:01:16 +02:00
|
|
|
options.format = 'jex';
|
2018-02-26 21:16:01 +02:00
|
|
|
} else if (ext === 'enex') {
|
|
|
|
options.format = 'enex';
|
2018-02-25 19:01:16 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Cannot automatically detect source format from path: ' + options.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.destinationFolderId) {
|
|
|
|
const folder = await Folder.load(options.destinationFolderId);
|
|
|
|
if (!folder) throw new Error('Notebook not found: ' + options.destinationFolderId);
|
2018-02-26 20:43:50 +02:00
|
|
|
options.destinationFolder = folder;
|
2018-02-25 19:01:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = { warnings: [] }
|
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
const importer = this.newImporter_(options.format);
|
2018-02-26 20:43:50 +02:00
|
|
|
await importer.init(options.path, options);
|
|
|
|
result = await importer.exec(result);
|
2018-02-25 19:01:16 +02:00
|
|
|
|
|
|
|
return result;
|
2018-02-23 21:32:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async export(options) {
|
|
|
|
const exportPath = options.path ? options.path : null;
|
|
|
|
const sourceFolderIds = options.sourceFolderIds ? options.sourceFolderIds : [];
|
|
|
|
const sourceNoteIds = options.sourceNoteIds ? options.sourceNoteIds : [];
|
2018-02-25 19:01:16 +02:00
|
|
|
const exportFormat = options.format ? options.format : 'jex';
|
2018-02-25 23:08:32 +02:00
|
|
|
const result = { warnings: [] }
|
2018-02-23 21:32:19 +02:00
|
|
|
const itemsToExport = [];
|
|
|
|
|
|
|
|
const queueExportItem = (itemType, itemOrId) => {
|
|
|
|
itemsToExport.push({
|
|
|
|
type: itemType,
|
|
|
|
itemOrId: itemOrId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let exportedNoteIds = [];
|
|
|
|
let resourceIds = [];
|
|
|
|
const folderIds = await Folder.allIds();
|
|
|
|
|
|
|
|
for (let folderIndex = 0; folderIndex < folderIds.length; folderIndex++) {
|
|
|
|
const folderId = folderIds[folderIndex];
|
|
|
|
if (sourceFolderIds.length && sourceFolderIds.indexOf(folderId) < 0) continue;
|
|
|
|
|
|
|
|
if (!sourceNoteIds.length) await queueExportItem(BaseModel.TYPE_FOLDER, folderId);
|
|
|
|
|
|
|
|
const noteIds = await Folder.noteIds(folderId);
|
|
|
|
|
|
|
|
for (let noteIndex = 0; noteIndex < noteIds.length; noteIndex++) {
|
|
|
|
const noteId = noteIds[noteIndex];
|
|
|
|
if (sourceNoteIds.length && sourceNoteIds.indexOf(noteId) < 0) continue;
|
|
|
|
const note = await Note.load(noteId);
|
|
|
|
await queueExportItem(BaseModel.TYPE_NOTE, note);
|
|
|
|
exportedNoteIds.push(noteId);
|
|
|
|
|
|
|
|
const rids = Note.linkedResourceIds(note.body);
|
|
|
|
resourceIds = resourceIds.concat(rids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
resourceIds = ArrayUtils.unique(resourceIds);
|
|
|
|
|
2018-02-23 21:32:19 +02:00
|
|
|
for (let i = 0; i < resourceIds.length; i++) {
|
|
|
|
await queueExportItem(BaseModel.TYPE_RESOURCE, resourceIds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const noteTags = await NoteTag.all();
|
|
|
|
|
|
|
|
let exportedTagIds = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < noteTags.length; i++) {
|
|
|
|
const noteTag = noteTags[i];
|
|
|
|
if (exportedNoteIds.indexOf(noteTag.note_id) < 0) continue;
|
|
|
|
await queueExportItem(BaseModel.TYPE_NOTE_TAG, noteTag.id);
|
|
|
|
exportedTagIds.push(noteTag.tag_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < exportedTagIds.length; i++) {
|
|
|
|
await queueExportItem(BaseModel.TYPE_TAG, exportedTagIds[i]);
|
|
|
|
}
|
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
const exporter = this.newExporter_(exportFormat);
|
2018-02-23 21:32:19 +02:00
|
|
|
await exporter.init(exportPath);
|
|
|
|
|
|
|
|
for (let i = 0; i < itemsToExport.length; i++) {
|
|
|
|
const itemType = itemsToExport[i].type;
|
|
|
|
const ItemClass = BaseItem.getClassByItemType(itemType);
|
|
|
|
const itemOrId = itemsToExport[i].itemOrId;
|
|
|
|
const item = typeof itemOrId === 'object' ? itemOrId : await ItemClass.load(itemOrId);
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
if (itemType === BaseModel.TYPE_RESOURCE) {
|
|
|
|
result.warnings.push(sprintf('A resource that does not exist is referenced in a note. The resource was skipped. Resource ID: %s', itemOrId));
|
|
|
|
} else {
|
|
|
|
result.warnings.push(sprintf('Cannot find item with type "%s" and ID %s. Item was skipped.', ItemClass.tableName(), JSON.stringify(itemOrId)));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
try {
|
|
|
|
if (itemType == BaseModel.TYPE_RESOURCE) {
|
|
|
|
const resourcePath = Resource.fullPath(item);
|
|
|
|
await exporter.processResource(item, resourcePath);
|
|
|
|
}
|
2018-02-23 21:32:19 +02:00
|
|
|
|
2018-02-26 21:25:54 +02:00
|
|
|
await exporter.processItem(ItemClass, item);
|
|
|
|
} catch (error) {
|
|
|
|
result.warnings.push(error.message);
|
2018-02-23 21:32:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await exporter.close();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = InteropService;
|