trilium/src/routes/api/import.js

118 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-12-03 12:41:18 +08:00
"use strict";
2018-04-01 23:42:12 +08:00
const repository = require('../../services/repository');
const enexImportService = require('../../services/import/enex');
const opmlImportService = require('../../services/import/opml');
const tarImportService = require('../../services/import/tar');
2018-11-27 06:47:02 +08:00
const singleImportService = require('../../services/import/single');
2019-02-11 02:36:03 +08:00
const messagingService = require('../../services/messaging');
const cls = require('../../services/cls');
const path = require('path');
const noteCacheService = require('../../services/note_cache');
2019-02-11 02:53:57 +08:00
const log = require('../../services/log');
2018-05-30 08:32:13 +08:00
2019-02-11 02:36:03 +08:00
class ImportContext {
2019-02-12 06:45:58 +08:00
constructor(importId, safeImport) {
2019-02-11 02:36:03 +08:00
// importId is to distinguish between different import events - it is possible (though not recommended)
// to have multiple imports going at the same time
this.importId = importId;
2019-02-12 06:45:58 +08:00
this.safeImport = safeImport;
2019-02-11 05:30:55 +08:00
// // count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress
2019-02-11 05:56:14 +08:00
this.progressCount = 0;
2019-02-11 02:36:03 +08:00
this.lastSentCountTs = Date.now();
}
2019-02-11 05:30:55 +08:00
async increaseProgressCount() {
2019-02-11 05:56:14 +08:00
this.progressCount++;
2019-02-11 02:36:03 +08:00
2019-02-11 05:56:14 +08:00
if (Date.now() - this.lastSentCountTs >= 500) {
2019-02-11 02:36:03 +08:00
this.lastSentCountTs = Date.now();
await messagingService.sendMessageToAllClients({
importId: this.importId,
2019-02-11 05:30:55 +08:00
type: 'import-progress-count',
2019-02-11 05:56:14 +08:00
progressCount: this.progressCount
2019-02-11 02:36:03 +08:00
});
}
}
async importFinished(noteId) {
await messagingService.sendMessageToAllClients({
importId: this.importId,
type: 'import-finished',
noteId: noteId
});
}
2019-02-11 02:53:57 +08:00
2019-02-17 06:58:42 +08:00
// must remaing non-static
2019-02-11 02:53:57 +08:00
async reportError(message) {
await messagingService.sendMessageToAllClients({
type: 'import-error',
message: message
});
}
2019-02-11 02:36:03 +08:00
}
2018-05-30 08:32:13 +08:00
async function importToBranch(req) {
2019-02-12 06:45:58 +08:00
let {parentNoteId, importId, safeImport} = req.params;
safeImport = safeImport !== '0';
2018-05-30 08:32:13 +08:00
const file = req.file;
2018-09-11 05:41:11 +08:00
if (!file) {
2018-09-04 03:06:24 +08:00
return [400, "No file has been uploaded"];
}
2018-05-30 08:32:13 +08:00
const parentNote = await repository.getNote(parentNoteId);
if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}
const extension = path.extname(file.originalname).toLowerCase();
// running all the event handlers on imported notes (and attributes) is slow
// and may produce unintended consequences
cls.disableEntityEvents();
let note; // typically root of the import - client can show it after finishing the import
2019-02-12 06:45:58 +08:00
const importContext = new ImportContext(importId, safeImport);
2019-02-11 02:36:03 +08:00
2019-02-11 02:53:57 +08:00
try {
if (extension === '.tar') {
note = await tarImportService.importTar(importContext, file.buffer, parentNote);
} else if (extension === '.opml') {
note = await opmlImportService.importOpml(importContext, file.buffer, parentNote);
} else if (extension === '.md') {
note = await singleImportService.importMarkdown(importContext, file, parentNote);
} else if (extension === '.html' || extension === '.htm') {
note = await singleImportService.importHtml(importContext, file, parentNote);
} else if (extension === '.enex') {
note = await enexImportService.importEnex(importContext, file, parentNote);
} else {
return [400, `Unrecognized extension ${extension}, must be .tar or .opml`];
}
2018-09-03 19:40:40 +08:00
}
2019-02-11 02:53:57 +08:00
catch (e) {
const message = "Import failed with following error: '" + e.message + "'. More details might be in the logs.";
importContext.reportError(message);
log.error(message + e.stack);
return [500, message];
2018-05-30 08:32:13 +08:00
}
// import has deactivated note events so note cache is not updated
// instead we force it to reload (can be async)
noteCacheService.load();
return note;
2018-05-30 08:32:13 +08:00
}
2018-03-31 03:34:07 +08:00
module.exports = {
2018-05-30 08:32:13 +08:00
importToBranch
2018-03-31 03:34:07 +08:00
};