2017-12-02 23:41:18 -05:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-01 11:42:12 -04:00
|
|
|
const repository = require('../../services/repository');
|
2018-11-16 12:12:04 +01:00
|
|
|
const enexImportService = require('../../services/import/enex');
|
|
|
|
const opmlImportService = require('../../services/import/opml');
|
|
|
|
const tarImportService = require('../../services/import/tar');
|
2018-11-26 23:47:02 +01:00
|
|
|
const singleImportService = require('../../services/import/single');
|
2019-02-10 19:36:03 +01:00
|
|
|
const messagingService = require('../../services/messaging');
|
2018-11-26 22:27:57 +01:00
|
|
|
const cls = require('../../services/cls');
|
2018-02-26 00:07:43 -05:00
|
|
|
const path = require('path');
|
2019-01-08 20:19:41 +01:00
|
|
|
const noteCacheService = require('../../services/note_cache');
|
2019-02-10 19:53:57 +01:00
|
|
|
const log = require('../../services/log');
|
2018-05-29 20:32:13 -04:00
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
class ImportContext {
|
|
|
|
constructor(importId) {
|
|
|
|
// 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;
|
|
|
|
this.count = 0;
|
|
|
|
this.lastSentCountTs = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
async increaseCount() {
|
|
|
|
this.count++;
|
|
|
|
|
|
|
|
if (Date.now() - this.lastSentCountTs >= 1000) {
|
|
|
|
this.lastSentCountTs = Date.now();
|
|
|
|
|
|
|
|
await messagingService.sendMessageToAllClients({
|
|
|
|
importId: this.importId,
|
|
|
|
type: 'import-note-count',
|
|
|
|
count: this.count
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async importFinished(noteId) {
|
|
|
|
await messagingService.sendMessageToAllClients({
|
|
|
|
importId: this.importId,
|
|
|
|
type: 'import-finished',
|
|
|
|
noteId: noteId
|
|
|
|
});
|
|
|
|
}
|
2019-02-10 19:53:57 +01:00
|
|
|
|
|
|
|
// must remaing static
|
|
|
|
async reportError(message) {
|
|
|
|
await messagingService.sendMessageToAllClients({
|
|
|
|
type: 'import-error',
|
|
|
|
message: message
|
|
|
|
});
|
|
|
|
}
|
2019-02-10 19:36:03 +01:00
|
|
|
}
|
|
|
|
|
2018-05-29 20:32:13 -04:00
|
|
|
async function importToBranch(req) {
|
2019-02-10 19:36:03 +01:00
|
|
|
const {parentNoteId, importId} = req.params;
|
2018-05-29 20:32:13 -04:00
|
|
|
const file = req.file;
|
|
|
|
|
2018-09-10 23:41:11 +02:00
|
|
|
if (!file) {
|
2018-09-03 21:06:24 +02:00
|
|
|
return [400, "No file has been uploaded"];
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:32:13 -04:00
|
|
|
const parentNote = await repository.getNote(parentNoteId);
|
|
|
|
|
|
|
|
if (!parentNote) {
|
|
|
|
return [404, `Note ${parentNoteId} doesn't exist.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
const extension = path.extname(file.originalname).toLowerCase();
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
// running all the event handlers on imported notes (and attributes) is slow
|
|
|
|
// and may produce unintended consequences
|
|
|
|
cls.disableEntityEvents();
|
|
|
|
|
2019-01-08 20:19:41 +01:00
|
|
|
let note; // typically root of the import - client can show it after finishing the import
|
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
const importContext = new ImportContext(importId);
|
|
|
|
|
2019-02-10 19:53:57 +01: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 13:40:40 +02:00
|
|
|
}
|
2019-02-10 19:53:57 +01: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-29 20:32:13 -04:00
|
|
|
}
|
2019-01-08 20:19:41 +01: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-29 20:32:13 -04:00
|
|
|
}
|
|
|
|
|
2018-03-30 15:34:07 -04:00
|
|
|
module.exports = {
|
2018-05-29 20:32:13 -04:00
|
|
|
importToBranch
|
2018-03-30 15:34:07 -04:00
|
|
|
};
|