2017-12-03 10:48:22 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-11-25 03:58:38 +08:00
|
|
|
const tarExportService = require('../../services/export/tar');
|
|
|
|
const singleExportService = require('../../services/export/single');
|
2018-11-16 19:12:04 +08:00
|
|
|
const opmlExportService = require('../../services/export/opml');
|
2018-03-31 21:07:58 +08:00
|
|
|
const repository = require("../../services/repository");
|
2019-02-11 05:30:55 +08:00
|
|
|
const messagingService = require("../../services/messaging");
|
|
|
|
const log = require("../../services/log");
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2019-02-11 05:30:55 +08:00
|
|
|
class ExportContext {
|
|
|
|
constructor(exportId) {
|
|
|
|
// exportId is to distinguish between different export events - it is possible (though not recommended)
|
|
|
|
// to have multiple exports going at the same time
|
|
|
|
this.exportId = exportId;
|
|
|
|
// count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress
|
|
|
|
this.progressCount = 0;
|
|
|
|
this.lastSentCountTs = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
async increaseProgressCount() {
|
|
|
|
this.progressCount++;
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2019-02-11 05:56:14 +08:00
|
|
|
if (Date.now() - this.lastSentCountTs >= 500) {
|
2019-02-11 05:30:55 +08:00
|
|
|
this.lastSentCountTs = Date.now();
|
|
|
|
|
|
|
|
await messagingService.sendMessageToAllClients({
|
|
|
|
exportId: this.exportId,
|
|
|
|
type: 'export-progress-count',
|
|
|
|
progressCount: this.progressCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async exportFinished() {
|
|
|
|
await messagingService.sendMessageToAllClients({
|
|
|
|
exportId: this.exportId,
|
|
|
|
type: 'export-finished'
|
|
|
|
});
|
2018-09-03 15:40:22 +08:00
|
|
|
}
|
2019-02-11 05:30:55 +08:00
|
|
|
|
2019-02-17 06:58:42 +08:00
|
|
|
// must remaing non-static
|
2019-02-11 05:30:55 +08:00
|
|
|
async reportError(message) {
|
|
|
|
await messagingService.sendMessageToAllClients({
|
|
|
|
type: 'export-error',
|
|
|
|
message: message
|
|
|
|
});
|
2018-11-16 19:12:04 +08:00
|
|
|
}
|
2019-02-11 05:30:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function exportBranch(req, res) {
|
2019-02-17 06:33:40 +08:00
|
|
|
const {branchId, type, format, version, exportId} = req.params;
|
2019-02-11 05:30:55 +08:00
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
|
|
|
|
const exportContext = new ExportContext(exportId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (type === 'subtree' && (format === 'html' || format === 'markdown')) {
|
|
|
|
await tarExportService.exportToTar(exportContext, branch, format, res);
|
|
|
|
}
|
|
|
|
else if (type === 'single') {
|
|
|
|
await singleExportService.exportSingleNote(exportContext, branch, format, res);
|
|
|
|
}
|
|
|
|
else if (format === 'opml') {
|
2019-02-17 06:33:40 +08:00
|
|
|
await opmlExportService.exportToOpml(exportContext, branch, version, res);
|
2019-02-11 05:30:55 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return [404, "Unrecognized export format " + format];
|
|
|
|
}
|
2018-09-03 05:39:10 +08:00
|
|
|
}
|
2019-02-11 05:30:55 +08:00
|
|
|
catch (e) {
|
|
|
|
const message = "Export failed with following error: '" + e.message + "'. More details might be in the logs.";
|
|
|
|
exportContext.reportError(message);
|
|
|
|
|
|
|
|
log.error(message + e.stack);
|
|
|
|
|
|
|
|
res.status(500).send(message);
|
2018-05-28 00:26:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 03:34:07 +08:00
|
|
|
module.exports = {
|
2018-11-25 03:58:38 +08:00
|
|
|
exportBranch
|
2018-03-31 03:34:07 +08:00
|
|
|
};
|